Flask-OpenAPI3-Swagger
Flask-OpenAPI3-Swagger is a Python library that provides the Swagger UI for Flask-OpenAPI3, enabling interactive API documentation directly within Flask applications. It acts as an optional plugin for Flask-OpenAPI3, automatically providing Swagger UI integration once installed. The library is currently in version 5.31.0 and follows a regular release cadence as part of the wider Flask-OpenAPI3 ecosystem.
Warnings
- gotcha Swagger UI does not display routes or documentation: This often occurs when the `Info` object is not correctly provided during the `OpenAPI` app initialization, or if blueprints are not properly registered with the `OpenAPI` instance. Ensure your routes have docstrings formatted correctly for OpenAPI parsing.
- gotcha Arguments passed by other decorators (e.g., dependency injection, authentication) are removed from views by `flask-openapi3`: `flask-openapi3`'s internal request validation might incorrectly strip `kwargs` intended for decorated views, breaking functionality like `flask-login` or custom dependency injection.
- gotcha Swagger UI not accessible or incorrect version loaded: If `OPENAPI_SWAGGER_UI_PATH` or `OPENAPI_SWAGGER_UI_VERSION` (or similar configuration settings for `flask-openapi3`) are not set correctly, the Swagger UI might not be found at the expected URL or might load an outdated CDN version.
- breaking The parent library `flask-openapi3` might be renamed to `flask-openapi`: There's an open discussion regarding renaming `flask-openapi3` to `flask-openapi`. If this change is implemented, it would likely result in breaking changes for imports and potentially package names for dependent libraries like `flask-openapi3-swagger`.
Install
-
pip install flask-openapi3-swagger -
pip install 'flask-openapi3[swagger]'
Imports
- Swagger UI integration
The presence of `flask-openapi3-swagger` on the Python path enables Swagger UI when initializing `flask_openapi3.OpenAPI`.
- OpenAPI, Info, Tag
from flask_openapi3 import OpenAPI, Info, Tag
Quickstart
from flask import Flask
from flask_openapi3 import OpenAPI, Info, Tag
import os
info = Info(title="Flask API", version="1.0.0", description="A simple Flask OpenAPI3 application.")
tags = [
Tag(name="hello", description="Hello world endpoints"),
]
app = OpenAPI(__name__, info=info, tags=tags)
# Configure Swagger UI path (optional, default is /openapi/swagger)
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
@app.get("/hello", tags=["hello"])
def hello():
"""Say Hello
Gets a greeting message.
---
responses:
200:
description: A greeting message.
"""
return {"message": "Hello, World!"}
if __name__ == "__main__":
# Run with `flask run` or `python app.py`
# Access Swagger UI at http://127.0.0.1:5000/swagger-ui/
app.run(debug=True)