Flask-Swagger-UI
Flask-Swagger-UI is a Python library that provides a simple Flask blueprint to integrate Swagger UI into your Flask applications. It currently bundles Swagger UI version 5.32.1. While the project is not actively maintained, it receives occasional updates to keep the bundled Swagger UI version current. Users requiring more active development or specific new features might consider forking the repository.
Warnings
- gotcha The `flask-swagger-ui` project is not actively maintained, though it receives occasional updates primarily for bundling the latest Swagger UI. Users requiring more active feature development or prompt bug fixes may need to consider forking the repository.
- gotcha When using a local `swagger.json` or `openapi.json` file as the `API_URL`, ensure that Flask is configured to serve static files from the directory where your API definition resides. Common issues include 'Fetch error: Not Found' if the static path is incorrect or the file isn't accessible via the web server.
- gotcha The `get_swaggerui_blueprint` factory function supports overriding Swagger UI configuration options that can be JSON serialized via the `config` dictionary parameter. However, it does *not* support Swagger UI plugins or function parameters for advanced customization.
- gotcha `flask-swagger-ui` provides a standalone blueprint for Swagger UI. It is different from the built-in Swagger UI functionality offered by `Flask-RESTPlus` (now `Flask-RESTX`), which integrates Swagger documentation directly with API definitions.
Install
-
pip install flask-swagger-ui
Imports
- get_swaggerui_blueprint
from flask_swagger_ui import get_swaggerui_blueprint
Quickstart
from flask import Flask
from flask_swagger_ui import get_swaggerui_blueprint
app = Flask(__name__)
SWAGGER_URL = '/api/docs' # URL for exposing Swagger UI (without trailing '/')
API_URL = 'https://petstore.swagger.io/v2/swagger.json' # Our API url (can of course be a local resource)
# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL, # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/'
API_URL,
config={
'app_name': 'Test application'
}
)
app.register_blueprint(swaggerui_blueprint)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)