Skip to main content

Extract swagger specs from your flask project

Project description

A swagger 2.0 spec extractor for flask

Install:

pip install flask-swagger

flask-swagger provides a method (swagger) that inspects the flask app for endpoints that contain YAML docstrings with swagger 2.0 Operation objects.

class UserAPI(MethodView):

    def post(self):
        """
        Create a new user
        ---
        tags:
          - users
        parameters:
          - in: body
            name: body
            schema:
              name: User
              required:
                - email
                - name
              properties:
                email:
                  type: string
                  description: email for user
                name:
                  type: string
                  description: name for user
        responses:
          201:
            description: User created
        """
        return {}

flask-swagger supports docstrings in MethodView classes and regular flask view functions.

Following YAML conventions, flask-swagger searches for ---, everything preceding is provided as summary (first line) and description (following lines) for the endpoint while everything after is parsed as a swagger Operation object.

In order to support inline definition of Schema objects in Operation and Response items, flask-swagger veers a little off from the standard. We add (and require) a name field for the inline Schema which is then used to correctly place the Schema object in the Definitions object.

To expose your swagger specification to the world you provide a flask route that does something along these lines

from flask import Flask, jsonify
from flask_swagger import swagger

app = Flask(__name__)

@app.route("/spec")
def spec():
    return jsonify(swagger(app))

Note that the swagger specification returned by swagger(app) is as minimal as it can be. It’s your job to override and add to the specification as you see fit.

@app.route("/spec")
def spec():
    swag = swagger(app)
    swag['info']['version'] = "1.0"
    swag['info']['title'] = "My API"
    return jsonify(swag)

Swagger-UI

Swagger-UI is the reason we embarked on this mission to begin with, flask-swagger does not however include Swagger-UI. Simply follow the awesome documentation over at https://github.com/swagger-api/swagger-ui and point your swaggerUi.url to your new flask-swagger endpoint and enjoy.

Acknowledgments

flask-swagger builds on ideas and code from flask-sillywalk and flask-restful-swagger

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-swagger-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file flask-swagger-0.1.1.tar.gz.

File metadata

  • Download URL: flask-swagger-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flask-swagger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5a51292f20a9477ab9d198f5586d3eeb8981dbe9044f69a75c9863cd53b77fe4
MD5 f8c3ec4fafc6e0d7ffb7ad3133df8ed5
BLAKE2b-256 6c545273c414db0b864b2ceb0aadb1ffe8ba3edd2bdd033733d939c667bb4773

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page