Flask-Swagger

0.2.14 · deprecated · verified Thu Apr 16

Flask-Swagger is a Python library designed to extract Swagger 2.0 specifications from Flask projects. Its latest version is 0.2.14, released in February 2021. The project appears to be in maintenance mode with infrequent updates, and lacks support for newer Python versions (3.9+) and OpenAPI 3.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate Flask-Swagger into a Flask application to generate Swagger 2.0 API specifications. It sets up a `/spec` endpoint to serve the JSON specification and a simple `/hello/<name>` endpoint with docstring-based Swagger annotations.

from flask import Flask, jsonify
from flask_swagger import swagger

app = Flask(__name__)

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

@app.route('/hello/<name>')
def hello(name):
    """
    Hello World Endpoint
    This is a test endpoint returning a greeting.
    ---
    parameters:
      - name: name
        in: path
        type: string
        required: true
        default: World
    responses:
      200:
        description: A greeting message
        schema:
          type: string
          example: Hello, John!
    """
    return f"Hello, {name}!"

# To run the app:
# if __name__ == '__main__':
#     app.run(debug=True)

view raw JSON →