pythonflaskswagger

Exposing API Documentation Using Flask and Swagger


I have build a small service with flask and already wrote a swagger yaml file to describe it's API. How can I expose the swagger file through the flask app?

I didn't mean to expose the file itself (send_from_directory) but to create new endpoint that will show it as swagger-ui (interactive, if possible)


Solution

  • OK, this is what I did.

    I used flasgger to and wrap my app with flasgger.Swagger. than I added 2 endpoints:

    1. /_api serves the YAML file (with send_from_directory)
    2. /api redirects to the flasgger api /apidocs/index.html?url=/api I'm sure it can be done better, but I failed to find it.

    github: https://github.com/eplaut/python-butler/blob/ad55292eb7c2b76c6554fa8becab9d0c1c7b5862/butler/butler.py#L119