Generate Swagger specification from Python code without annotations
<p>I am searching for a way to generate a Swagger specification (the JSON and Swagger-UI) from the definition of a Python service. I have found many options (normally Flask-based), but all of them use annotations, which I cannot properly handle from within the code, e.g. define 10 equivalent services with different routes in a loop (the handler could be currified in this example, first function call to obtain it).</p>

<p>Is there any way to do this without annotations? I would like to generate the method in the API and its documentation by calling a function (or a method, or a constructor of a class) with the values corresponding to the implementation and the documentation of that API method.</p>

<p>I suggest looking into <a href="http://apispec.readthedocs.io/en/latest/quickstart.html" rel="nofollow">apispec</a>.</p>

<p>apispec is a pluggable API specification generator.</p>

<p>Currently supports the OpenAPI 2.0 specification (f.k.a. Swagger 2.0)</p>

<p><strong><em>apispec</em></strong></p>

<pre><code>from apispec import APISpec

spec = APISpec(
    title='Gisty',
    version='1.0.0',
    info=dict(description='A minimal gist API')
)

spec.definition('Gist', properties={
    'id': {'type': 'integer', 'format': 'int64'},
    'content': 'type': 'string'},
})

spec.add_path(
    path='/gist/{gist_id}',
    operations=dict(
        get=dict(
            responses={
                '200': {
                    'schema': {'$ref': '#/definitions/Gist'}
                }
            }
        )
    )
)
</code></pre>

python web-services python-3.x swagger-ui swagger-2.0