Swagger UI for Python web framework, such as Tornado, Flask, Quart, Sanic and Falcon.
Project description
swagger-ui-py
Seamless Swagger UI integration for Python web frameworks. A unified, framework-agnostic API for adding interactive OpenAPI documentation to your Python applications with automatic framework detection.
Status: Production-ready, actively maintained Python: 3.9, 3.10, 3.11, 3.12 Swagger UI: v5.25.3 Swagger Editor: v4.14.6
Key Features
- Framework Agnostic - Single API works across 9+ Python frameworks
- Auto-Detection - Automatically detects your framework, zero configuration needed
- Multiple Config Sources - YAML/JSON files, remote URLs, Python dicts, or strings
- Swagger Editor - Optional inline spec editor for live documentation editing
- Static Assets - Automatic CSS, JS, images, and icons serving
- Customization - Custom CSS, Swagger UI parameters, OAuth2 configuration
- Well-Tested - 50+ test cases across all supported frameworks
Supported Frameworks
Check supported frameworks:
python3 -c "from swagger_ui import supported_list; print(supported_list)"
# Output: ['flask', 'tornado', 'sanic', 'aiohttp', 'quart', 'starlette', 'falcon', 'bottle', 'chalice']
Quick Start
Installation
pip install swagger-ui-py
Basic Example (Flask)
from flask import Flask
from swagger_ui import api_doc
app = Flask(__name__)
# Auto-detects Flask and registers routes
api_doc(app, config_path='./openapi.yaml')
if __name__ == '__main__':
app.run(debug=True)
# Visit: http://localhost:5000/api/doc
Basic Example (Tornado)
import tornado.ioloop
from tornado.web import Application
from swagger_ui import api_doc
app = Application()
# Auto-detects Tornado and registers routes
api_doc(app, config_path='./openapi.yaml', url_prefix='/api/doc')
if __name__ == '__main__':
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
# Visit: http://localhost:8888/api/doc
Configuration Options
1. Configuration Sources (in priority order)
Option A: YAML/JSON File
api_doc(app, config_path='./openapi.yaml')
Option B: Remote URL (requires CORS)
api_doc(app, config_url='https://petstore.swagger.io/v2/swagger.json')
Option C: Python Dict
config = {
"openapi": "3.0.1",
"info": {"title": "My API", "version": "1.0.0"},
"paths": { ... }
}
api_doc(app, config=config)
Option D: JSON/YAML String
spec_string = '{"openapi":"3.0.1","info":{"title":"My API"},...}'
api_doc(app, config_spec=spec_string)
Option E: External Endpoint
api_doc(app, config_rel_url='/swagger.json') # App provides endpoint
2. Common Parameters
api_doc(
app,
config_path='./openapi.yaml', # Config source
url_prefix='/api/doc', # Internal route path (default: '/api/doc')
base_url=None, # External URL for assets (defaults to url_prefix)
title='API Documentation', # HTML page title
editor=False, # Enable spec editor
custom_css='https://cdn.../style.css', # Custom CSS
host_inject=True, # Auto-inject request host
)
3. Reverse Proxy Support
When running behind a reverse proxy with a path prefix (e.g., ingress /service/* → backend), use base_url to set the external URL path for static assets:
# Proxy routes /service/docs/* to backend /docs/*
api_doc(
app,
config_path='./openapi.yaml',
url_prefix='/docs', # Internal routes registered at /docs
base_url='/service/docs', # Assets linked as /service/docs/static/*
)
url_prefix- Used for internal route registration (app-side)base_url- Used for asset URLs in HTML templates (browser-side, defaults tourl_prefix)
4. Swagger UI Customization
parameters = {
"deepLinking": "true",
"displayRequestDuration": "true",
"layout": "\"StandaloneLayout\"",
"plugins": "[SwaggerUIBundle.plugins.DownloadUrl]",
}
api_doc(app, config_path='./openapi.yaml', parameters=parameters)
See Swagger UI Parameters for all options.
5. OAuth2 Configuration
oauth2_config = {
"clientId": "\"your-client-id\"",
"clientSecret": "\"your-secret\"",
"realm": "\"your-realm\"",
"appName": "\"your-app\"",
"scopeSeparator": "\" \"",
"scopes": "\"openid profile\"",
}
api_doc(app, config_path='./openapi.yaml', oauth2_config=oauth2_config)
See OAuth2 Configuration for details.
6. Legacy Framework-Specific APIs
Still supported for backward compatibility:
from swagger_ui import flask_api_doc, tornado_api_doc, sanic_api_doc
# Same as api_doc(app, ...) but explicit
flask_api_doc(app, config_path='./openapi.yaml')
tornado_api_doc(app, config_path='./openapi.yaml')
sanic_api_doc(app, config_path='./openapi.yaml')
Routes Created
The library automatically creates these routes (at url_prefix=/api/doc):
GET /api/doc- Interactive Swagger UI documentationGET /api/doc/swagger.json- OpenAPI specification (JSON)GET /api/doc/editor- Swagger Editor (ifeditor=True)GET /api/doc/static/{path}- Static assets (CSS, JS, images)
Creating OpenAPI Specifications
For details on writing OpenAPI specifications:
- OpenAPI 3.0 Guide
- OpenAPI 3.1 Spec
- Swagger Editor - Interactive spec editor
Versions
To update to newer versions:
tox -e update
# or
python tools/update.py --ui-version=v5.25.3 --editor-version=v4.14.6
Extending the Library
Adding Support for New Frameworks
To add support for a new framework:
- Create
swagger_ui/handlers/{framework}.pyfollowing the handler interface - Implement
handler(doc)andmatch(doc)functions - Add tests in
test/{framework}_test.py - Update README with framework name
- Auto-discovery will handle the rest
See Code Standards for detailed guidelines.
Custom Parameters & Configuration
Pass custom parameters and OAuth2 configuration through the api_doc() function for full control over Swagger UI behavior.
Documentation
Full documentation available in the ./docs directory:
- Project Overview & PDR - Vision, goals, requirements
- Codebase Summary - Module structure and responsibilities
- Code Standards - Coding conventions and patterns
- System Architecture - Design patterns and data flow
- Project Roadmap - Planned improvements and timeline
Development
Setup
# Clone repository
git clone https://github.com/PWZER/swagger-ui-py.git
cd swagger-ui-py
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
make pytest
# Format code
make format
# Build wheel
make whl
Testing
make pytest # Run all tests
make format-check # Check code style
make format # Auto-format code
Building & Release
make whl # Build wheel
make upload # Upload to PyPI
Troubleshooting
Framework not detected:
- Ensure framework is installed
- Try explicit
app_typeparameter:api_doc(app, app_type='flask', ...)
Config not loading:
- Check file path exists and is readable
- Validate YAML/JSON format using Swagger Editor
- Check CORS headers if using remote URL
Routes not registered:
- Ensure
api_doc()called before app starts - Check
url_prefixdoesn't conflict with existing routes - Verify framework-specific setup (e.g.,
app.register_blueprint()for Flask)
For more help:
- Check examples/ directory for working samples
- Review GitHub Issues
- Read framework-specific documentation
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure tests pass:
make pytest - Format code:
make format - Submit a pull request
See Code Standards for contribution guidelines.
License
Licensed under the Apache License 2.0. See LICENSE file for details.
Support
- Issues: GitHub Issues
- Project: PWZER/swagger-ui-py
- PyPI: swagger-ui-py
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swagger_ui_python-1.0.1.tar.gz.
File metadata
- Download URL: swagger_ui_python-1.0.1.tar.gz
- Upload date:
- Size: 6.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c09dc924ed8b8d65f0532a4e32eb7e7d87ca1f8bed32971eeda229740b7095f
|
|
| MD5 |
1722c72938d82084ccd8edb80871b0bc
|
|
| BLAKE2b-256 |
942e3ae2c1407d08c9f4cc52570f1f8dbbf02ba50c1d90721423cdfb701726c1
|
Provenance
The following attestation bundles were made for swagger_ui_python-1.0.1.tar.gz:
Publisher:
release.yml on SkyTik/swagger-ui-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swagger_ui_python-1.0.1.tar.gz -
Subject digest:
5c09dc924ed8b8d65f0532a4e32eb7e7d87ca1f8bed32971eeda229740b7095f - Sigstore transparency entry: 842940060
- Sigstore integration time:
-
Permalink:
SkyTik/swagger-ui-py@50757b81c89d5c2987383165c5b7cb755800381b -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/SkyTik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@50757b81c89d5c2987383165c5b7cb755800381b -
Trigger Event:
push
-
Statement type:
File details
Details for the file swagger_ui_python-1.0.1-py3-none-any.whl.
File metadata
- Download URL: swagger_ui_python-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
283878088e35cc6f762af67c236ea25a01d56aac171c47f6918ab1af69437e75
|
|
| MD5 |
373c67298ec343d2afa8ea3d773c567f
|
|
| BLAKE2b-256 |
f304739bd38e0a45b3d9f00413c139dc18f9e0291f0fa3bb5332f1b04953bc96
|
Provenance
The following attestation bundles were made for swagger_ui_python-1.0.1-py3-none-any.whl:
Publisher:
release.yml on SkyTik/swagger-ui-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swagger_ui_python-1.0.1-py3-none-any.whl -
Subject digest:
283878088e35cc6f762af67c236ea25a01d56aac171c47f6918ab1af69437e75 - Sigstore transparency entry: 842940062
- Sigstore integration time:
-
Permalink:
SkyTik/swagger-ui-py@50757b81c89d5c2987383165c5b7cb755800381b -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/SkyTik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@50757b81c89d5c2987383165c5b7cb755800381b -
Trigger Event:
push
-
Statement type: