✨ Allow including additional scripts to Swagger UI page and adding presets #14762
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For now there is no easy way to enable
StandaloneLayoutand dark mode that comes with it.To do this you need to:
SwaggerUIStandalonePresetto presetslayoutswagger parameter toStandaloneLayout(this is possible now, but will giveNo layout defined for "StandaloneLayout"without first two steps)With changes from this PR we will be able to enable
StandaloneLayoutthe 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}"}