✨ Allow including additional scripts to Swagger UI page and adding presets by YuriiMotov · Pull Request #14762 · fastapi/fastapi

For now there is no easy way to enable StandaloneLayout and dark mode that comes with it.

With changes from this PR we will be able to enable StandaloneLayout the following way:

from fastapi import FastAPI
from fastapi.openapi.docs import (
    get_swagger_ui_html,
    get_swagger_ui_oauth2_redirect_html,
)

app = FastAPI(docs_url=None, redoc_url=None)


@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title=app.title + " - Swagger UI",
        oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
+       swagger_extra_js_urls=["https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-standalone-preset.js"],
+       swagger_extra_presets=["SwaggerUIStandalonePreset"],
+       swagger_ui_parameters={"layout": "StandaloneLayout"},
    )


@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()


@app.get("/users/{username}")
async def read_user(username: str):
    return {"message": f"Hello {username}"}