Skip to main content

FastAPI plugin to add dark theme swagger docs.

Project description

uv ruff image image image style tests codecov

Simple plugin to support enabling a dark theme for swagger docs in a FastAPI application.

screenshot

Usage

The simplest usage with default /docs endpoint can be achieved with something like:

import fastapi
import fastapi_swagger_dark as fsd

app = fastapi.FastAPI(docs_url=None)
router = fastapi.APIRouter()

fsd.install(router)
app.include_router(router)

To install using a custom path:

import fastapi
import fastapi_swagger_dark as fsd

app = fastapi.FastAPI(docs_url=None)
router = fastapi.APIRouter()

fsd.install(router, path="/swagger-docs")
app.include_router(router)

To install using a custom prefix:

import fastapi
import fastapi_swagger_dark as fsd

app = fastapi.FastAPI(docs_url=None)
router = fastapi.APIRouter(prefix="/api/v1")

fsd.install(router, path="/docs")
app.include_router(router)

To install using custom swagger_ui_parameters:

import fastapi
import fastapi_swagger_dark as fsd

app = fastapi.FastAPI(docs_url=None)
router = fastapi.APIRouter()

fsd.install(router, swagger_ui_parameters={...})
app.include_router(router)

If you are customising the documentation endpoints, for example with authorization, you can replace fastapi's default get_swagger_ui_html with the custom one using the dark theme. Ensure the dark_theme route is also included.

import typing

import fastapi
import fastapi_swagger_dark as fsd

app = fastapi.FastAPI(docs_url=None)


def auth_validation(...) -> None:
    ...


async def swagger_ui_html(
    request: fastapi.Request,
    _docs_auth: typing.Annotated[None, fastapi.Depends(auth_validation)],
) -> fastapi.responses.HTMLResponse:
    return fsd.get_swagger_ui_html(request)


app.get("/docs")(swwagger_ui_html)
app.get("/dark_theme.css", include_in_schema=False, name="dark_theme")(fsd.dark_swagger_theme)

Credit

Thanks go to @georgekhananaev and their repository darktheme-auth-fastapi-server for the basis of the stylesheet used here.

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_dark-0.0.9.tar.gz (179.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_dark-0.0.9-py3-none-any.whl (58.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_swagger_dark-0.0.9.tar.gz.

File metadata

  • Download URL: fastapi_swagger_dark-0.0.9.tar.gz
  • Upload date:
  • Size: 179.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for fastapi_swagger_dark-0.0.9.tar.gz
Algorithm Hash digest
SHA256 3f981af17ced385e53bd813c4ba08c99c8ca551c1a2957295abbf12c511d97d5
MD5 3e82419a493be37902df05cebec4623a
BLAKE2b-256 f790f68af675cf21bd4f091c6e686b9d01839a956f3188fbd68c3527399a8fdb

See more details on using hashes here.

File details

Details for the file fastapi_swagger_dark-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_swagger_dark-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 189ae4b8c4b58fc2faa19929517fc81495add85611b3117f650f15135d53f748
MD5 38e9aae4b0cb50be99294ab1510325af
BLAKE2b-256 6306f880792625d4ebb75786e47867ade0c9a7163a52293847babb9adf9540c1

See more details on using hashes here.

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