Skip to main content

This plugin updates the FastAPI app to host latest Swagger UI distribution.

Project description

FastAPI Swagger Plugin

This plugin updates the FastAPI app to include a latest Swagger UI distribution. Also, it works locally and does not depend on the @tiangolo domains and cdns.

Why?

The FastAPI already includes a Swagger UI. However, author updates swagger not so often. Moreover he uses his own hosts for the resources, and they are not always available (especially in Russia). This plugin allows to use the latest Swagger UI distribution and host it inside your app.

Usage

Installation

pip install fastapi-swagger

Basic Usage

from fastapi import FastAPI
from fastapi_swagger import patch_fastapi

app = FastAPI(docs_url=None, swagger_ui_oauth2_redirect_url=None)  # docs url will be at /docs

patch_fastapi(app)

How it works?

How it was before: FastAPI uses the get_swagger_ui_html function to render the Swagger UI under the hood.

def get_swagger_ui_html(
        *,
        openapi_url: str,
        title: str,
        swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
        swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
        swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
        oauth2_redirect_url: Optional[str] = None,
        init_oauth: Optional[Dict[str, Any]] = None,
        swagger_ui_parameters: Optional[Dict[str, Any]] = None,
) -> HTMLResponse:
    ...

How it is now:

Actually, we just copy the Swagger UI distribution from the GitHub releases to the fastapi_swagger package, and serve it from your app. Patch creates several additional routes with the Swagger UI resources and one route for docs page (btw, using same get_swagger_ui_html function).

def patch_fastapi(
        app: FastAPI,
        docs_url: str = "/docs",
        *,
        title: Optional[str],
        swagger_js_url: str = "/swagger/swagger-ui-bundle.js",  # relative path from app root
        swagger_css_url: str = "/swagger/swagger-ui.css",  # relative path from app root
        swagger_favicon_url: str = "/swagger/favicon-32x32.png",  # relative path from app root
        oauth2_redirect_url: Optional[str] = None,
        init_oauth: Optional[Dict[str, Any]] = None,
        swagger_ui_parameters: Optional[Dict[str, Any]] = None,
):
    ...


patch_fastapi(app)
# Now there are additional routes /swagger/swagger-ui-bundle.js, /swagger/swagger-ui.css, /swagger/favicon-32x32.png and /docs
# They all are not dependent on the external resources.

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

fastapi_swagger-0.4.41.tar.gz (448.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_swagger-0.4.41-py3-none-any.whl (452.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_swagger-0.4.41.tar.gz.

File metadata

  • Download URL: fastapi_swagger-0.4.41.tar.gz
  • Upload date:
  • Size: 448.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_swagger-0.4.41.tar.gz
Algorithm Hash digest
SHA256 56ad8a9538b35cb8977b609b80d58b74c1b10c96ef1ed1cb6f41e556ba766ffe
MD5 d0e3a750a6708916c7207564f267eccd
BLAKE2b-256 ae0f60cde869d1ccbdbc410ddc3264cfe2012ea48202a5f7f308d0e3f6333752

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_swagger-0.4.41.tar.gz:

Publisher: listen-swagger-changes.yaml on dantetemplar/fastapi-swagger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_swagger-0.4.41-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_swagger-0.4.41-py3-none-any.whl
Algorithm Hash digest
SHA256 ae51ee9141242acfb1431fa880433f572ccf47e8ee94aeee2ea637a0a4065eb1
MD5 7d0a1d2332a11a8e5edb6bb02df1d665
BLAKE2b-256 1effafaf33830a350842b04d5df72415dc0aed6af794bcec25633d719d6fe34b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_swagger-0.4.41-py3-none-any.whl:

Publisher: listen-swagger-changes.yaml on dantetemplar/fastapi-swagger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page