Skip to main content

FastAPI Standalone Docs

GitHub Workflow Status Code style: black PyPI PyPI - Python Version License: MIT

Host FastAPI Swagger UI and ReDoc without any third party CDNs. Handy if you want to be able to use the docs offline or want to avoid loading content from 3rd party CDNs due to privacy reasons.

This library was heavily inspired by fastapi-offline-swagger-ui, but we wanted a way to take it into use with less boilerplate code and also have it hosted on PyPI.

Installation

The package is available on PyPI:

pip install fastapi-standalone-docs

Usage

Import StandaloneDocs and pass in the FastAPI application to it:

from fastapi import FastAPI
from fastapi_standalone_docs import StandaloneDocs

app = FastAPI()
StandaloneDocs(app=app)

The static files will by default be served from subpaths of the docs_url and redoc_url as you've specified in your FastAPI app. The library doesn't make any assumption on any particular path being reserved for the library. And if you disable either of the two UIs, the corresponding files won't either be served unnecessarily.

This example would move the Swagger UI and the static files needed for it to the path /swagger and disable the ReDoc interface and the static files needed by it:

from fastapi import FastAPI
from fastapi_standalone_docs import StandaloneDocs

app = FastAPI(docs_url="/swagger", redoc_url=None)
StandaloneDocs(app=app)

By default, the library will use the FastAPI favicons, and they're served from {docs_url}/fastapi/favicon.png and {redoc_url}/fastapi/favicon.png. You can use your own favicons like this, which will also disable the FastAPI icons:

from fastapi import FastAPI
from fastapi_standalone_docs import StandaloneDocs

app = FastAPI()
StandaloneDocs(
    app=app,
    redoc_favicon_url="/favicon.png",
    swagger_favicon_url="/favicon.png",
)

To make the docs truly stand alone the Google Fonts are by default disabled (they are used in the ReDoc interface). If you want to enable them just set with_google_fonts=True, like this:

from fastapi import FastAPI
from fastapi_standalone_docs import StandaloneDocs

app = FastAPI()
StandaloneDocs(
    app=app,
    with_google_fonts=True,
)

Disclaimer

Note! If you create multiple FastAPI apps in the same runtime (quite unlikely use-case), using StandaloneDocs on one of them will affect the other ones as well, as the library patches two of the shared functions (get_swagger_ui_html and get_redoc_html). Thus the library should not be used in a such setup unless all the apps will use StandaloneDocs with the same settings.

Development

PRs are welcome!

This project is using Poetry and pre-commit, so please ensure you've installed both. To set up the project with them run:

pre-commit install
poetry install

Running tests locally:

poetry run invoke test

Updating the static files from the CDNs:

poetry run update-fastapi-standalone-docs

If you want to test using it locally in some other project, in that project run this (make sure to adjust the path to the local repo):

poetry add ../fastapi-standalone-docs --editable

License

The code of this library itself is released under the MIT license. Please however note that this library also contains parts of ReDoc, which is licensed under the MIT license, Swagger UI, which is licensed under the Apache 2.0 License, as well as parts of FastAPI (the favicon), which is licensed under the MIT license.

Release files for fastapi-standalone-docs 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-standalone-docs 0.2.0
File Size Uploaded
fastapi_standalone_docs-0.2.0.tar.gz 723.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-standalone-docs 0.2.0
File Interpreter ABI Platform
fastapi_standalone_docs-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / fastapi_standalone_docs-0.2.0.tar.gz

Download URL fastapi_standalone_docs-0.2.0.tar.gz
Size 723.1 kB
Tags Source
SHA-256 checksum
How to use checksums
003ca68eff4c84bf7997bba4028eebf7b06f47fb6b08b1551cf33fe064f11f12
BLAKE2b-256 checksum
How to use checksums
206e1da54c427e4ae7c4dfd5f01d2b9d02680c105b7ea2d9f0dd76a79e390d22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.16 Linux/6.8.0-1021-azure

Release files / fastapi_standalone_docs-0.2.0-py3-none-any.whl

Download URL fastapi_standalone_docs-0.2.0-py3-none-any.whl
Size 726.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
553fc57ba18d15c4797000718cbece214468abd29e681f6bcfa004d5fbee93d0
BLAKE2b-256 checksum
How to use checksums
0719861022421e2c593d8f07275da22015d89ae7f4df0490e64653ca3fd8bd5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.10.16 Linux/6.8.0-1021-azure

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page