Debian Codegraph @

docs_src/custom_docs_ui/tutorial002_py310.py

raw
1 from fastapi import FastAPI
2 from fastapi.openapi.docs import (
3 get_redoc_html,
4 get_swagger_ui_html,
5 get_swagger_ui_oauth2_redirect_html,
6 )
7 from fastapi.staticfiles import StaticFiles
8
9 app = FastAPI(docs_url=None, redoc_url=None)
10
11 app.mount("/static", StaticFiles(directory="static"), name="static")
12
13
14 @app.get("/docs", include_in_schema=False)
15 async def custom_swagger_ui_html():
16 return get_swagger_ui_html(
17 openapi_url=app.openapi_url,
18 title=app.title + " - Swagger UI",
19 oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
20 swagger_js_url="/static/swagger-ui-bundle.js",
21 swagger_css_url="/static/swagger-ui.css",
22 )
23
24
25 @app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
26 async def swagger_ui_redirect():
27 return get_swagger_ui_oauth2_redirect_html()
28
29
30 @app.get("/redoc", include_in_schema=False)
31 async def redoc_html():
32 return get_redoc_html(
33 openapi_url=app.openapi_url,
34 title=app.title + " - ReDoc",
35 redoc_js_url="/static/redoc.standalone.js",
36 )
37
38
39 @app.get("/users/{username}")
40 async def read_user(username: str):
41 return {"message": f"Hello {username}"}