Flasgger
Flasgger is a Python library that integrates Swagger UI into Flask applications, automatically extracting Swagger/OpenAPI specifications from docstrings within your Flask project's endpoints. The current version is 0.9.7.1, with development continuing through beta releases and stable updates to address compatibility and new features.
Warnings
- breaking Flasgger versions 0.9.5 and newer officially dropped support for Python 2. Projects still on Python 2 must use Flasgger 0.9.4 or older.
- breaking Incompatibility with Flask 2.3+ was resolved in Flasgger 0.9.7b1 (and subsequent releases like 0.9.7.1). Older Flasgger versions may experience issues with Flask's JSON encoder changes.
- gotcha When migrating to OpenAPI 3.0.0, Flasgger (from 0.9.6b1) will move top-level `#/definitions` to `#/components/schemas`. Existing Swagger 2.0 specifications might require updates to match the OpenAPI 3 structure.
- gotcha Flasgger 0.9.4 updated its supported versions for `apispec` (to 2.0.2) and `jsonschema` (to >=3.0.1). Ensure these dependencies are compatible with other libraries in your project to avoid conflicts.
Install
-
pip install flasgger
Imports
- Swagger
from flasgger import Swagger
- swag_from
from flasgger import swag_from
Quickstart
from flask import Flask, jsonify
from flasgger import Swagger
app = Flask(__name__)
swagger = Swagger(app)
@app.route("/hello")
def hello_world():
"""
Hello World! endpoint.
---
responses:
200:
description: A simple hello world message
"""
return jsonify(hello="world")
if __name__ == "__main__":
app.run(debug=True)