โ ๐¶
Warning
๐ ๐ ๐ง โ. ๐ ๐ฒ ๐ช ๐ถ โซ๏ธ.
๐ฅ ๐ ๐ ๐ฐ - ๐ฉโ๐ป ๐ฆฎ, ๐ ๐ช ๐ฒ ๐ถ ๐ ๐.
๐ฅ ๐ โช ๐ญ ๐ ๐ ๐ช ๐ ๐ ๐ ๐, ๐ฃ ๐.
๐ค ๐ผ ๐โ ๐ ๐ช ๐ช ๐ ๐ ๐ ๐.
๐ ๐ ๐ ๐ ๐ โ.
๐ ๐ ๏ธ¶
๐ (๐ข) ๐ ๏ธ, โฉ.
FastAPI
๐ธ (๐) โ๏ธ .openapi()
๐ฉโ๐ฌ ๐ ๐ ๐จ ๐ ๐.
๐ ๐ธ ๐ ๐, โก ๐ ๏ธ /openapi.json
(โ๏ธ โซ๏ธโ ๐ โ ๐ openapi_url
) ยฎ.
โซ๏ธ ๐จ ๐ป ๐จ โฎ๏ธ ๐ ๐ธ .openapi()
๐ฉโ๐ฌ.
๐ข, โซ๏ธโ ๐ฉโ๐ฌ .openapi()
๐จ โ
๐ .openapi_schema
๐ ๐ฅ โซ๏ธ โ๏ธ ๐ & ๐จ ๐ซ.
๐ฅ โซ๏ธ ๐ซ, โซ๏ธ ๐ ๐ซ โ๏ธ ๐ ๐ข fastapi.openapi.utils.get_openapi
.
& ๐ ๐ข get_openapi()
๐จ ๐ข:
title
: ๐ ๐, ๐ฆ ๐ฉบ.version
: โฌ ๐ ๐ ๏ธ, โ2.5.0
.openapi_version
: โฌ ๐ ๐ง โ๏ธ. ๐ข, โช:3.0.2
.description
: ๐ ๐ ๐ ๏ธ.routes
: ๐ ๐ฃ, ๐ซ ๐ ยฎ โก ๐ ๏ธ. ๐ซ โ โช๏ธโก๏ธapp.routes
.
๐ ๐ข¶
โ๏ธ โน ๐, ๐ ๐ช โ๏ธ ๐ ๐ ๐ข ๐ ๐ ๐ & ๐ ๐ ๐ ๐ ๐ ๐ช.
๐ผ, โก๏ธ ๐ฎ ๐ ๐ โ ๐ ๐ ๐ฑ.
๐ FastAPI¶
๐ฅ, โ ๐ ๐ FastAPI ๐ธ ๐:
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
app = FastAPI()
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
๐ ๐ ๐¶
โคด๏ธ, โ๏ธ ๐ ๐ ๐ข ๐ ๐ ๐, ๐ custom_openapi()
๐ข:
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
app = FastAPI()
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
๐ ๐ ๐¶
๐ ๐ ๐ช ๐ฎ ๐ โ, โ ๐ x-logo
info
"๐" ๐ ๐:
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
app = FastAPI()
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
๐พ ๐ ๐¶
๐ ๐ช โ๏ธ ๐ .openapi_schema
"๐พ", ๐ช ๐ ๐ ๐.
๐ ๐, ๐ ๐ธ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ ๐ฐ ๐ฉโ๐ป ๐ ๐ ๐ ๏ธ ๐ฉบ.
โซ๏ธ ๐ ๐ ๐ด ๐, & โคด๏ธ ๐ ๐พ ๐ ๐ โ๏ธ โญ ๐จ.
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
app = FastAPI()
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
๐ ๐ฉโ๐ฌ¶
๐ ๐ ๐ช โ .openapi()
๐ฉโ๐ฌ โฎ๏ธ ๐ ๐ ๐ข.
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
app = FastAPI()
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
โ โซ๏ธ¶
๐ ๐ ๐ถ http://127.0.0.1:8000/redoc ๐ ๐ ๐ ๐ ๐ โ๏ธ ๐ ๐ ๐ฑ (๐ ๐ผ, FastAPI'โ ๐ฑ):
๐ค-๐ธ ๐ธ & ๐ ๐ฉบ¶
๐ ๏ธ ๐ฉบ โ๏ธ ๐ฆ ๐ & ๐, & ๐ ๐ ๐ช ๐ธ & ๐ ๐.
๐ข, ๐ ๐ ๐ฆ โช๏ธโก๏ธ ๐ฒ.
โ๏ธ โซ๏ธ ๐ช ๐ โซ๏ธ, ๐ ๐ช โ ๐ฏ ๐ฒ, โ๏ธ ๐ฆ ๐ ๐.
๐ โ , ๐ผ, ๐ฅ ๐ ๐ช ๐ ๐ฑ ๐ง ๐ท โช ๐ฑ, ๐ต ๐ ๐ธ ๐, โ๏ธ ๐ง๐ฟ ๐ธ.
๐ฅ ๐ ๐ ๐ โ ๐ฆ ๐ ๐ ๐, ๐ FastAPI ๐ฑ, & ๐ ๐ฉบ โ๏ธ ๐ซ.
๐ ๐ ๐¶
โก๏ธ ๐ฌ ๐ ๐ ๐ ๐ ๐ ๐ ๐:
.
โโโ app
โ โโโ __init__.py
โ โโโ main.py
๐ โ ๐ ๐ช ๐ ๐ป ๐.
๐ ๐ ๐ ๐ ๐ช ๐ ๐ ๐:
.
โโโ app
โย ย โโโ __init__.py
โย ย โโโ main.py
โโโ static/
โฌ ๐¶
โฌ ๐ป ๐ ๐ช ๐ฉบ & ๐ฎ ๐ซ ๐ ๐ static/
๐.
๐ ๐ช ๐ฒ โถ๏ธ๏ธ-๐ ๐ ๐ & ๐ ๐ ๐ Save link as...
.
๐ฆ ๐ โ๏ธ ๐:
& ๐ โ๏ธ ๐:
โฎ๏ธ ๐, ๐ ๐ ๐ ๐ช ๐ ๐:
.
โโโ app
โย ย โโโ __init__.py
โย ย โโโ main.py
โโโ static
โโโ redoc.standalone.js
โโโ swagger-ui-bundle.js
โโโ swagger-ui.css
๐ฆ ๐ป ๐¶
- ๐
StaticFiles
. - "๐ป"
StaticFiles()
๐ ๐ฏ โก.
from fastapi import FastAPI
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
app.mount("/static", StaticFiles(directory="static"), name="static")
@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_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)
@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("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
๐ฏ ๐ป ๐¶
โถ๏ธ ๐ ๐ธ & ๐ถ http://127.0.0.1:8000/static/redoc.standalone.js.
๐ ๐ ๐ ๐ถ ๐ ๐ธ ๐ ๐.
โซ๏ธ ๐ช โถ๏ธ โฎ๏ธ ๐ณ ๐:
/*!
* ReDoc - OpenAPI/Swagger-generated API Reference Documentation
* -------------------------------------------------------------
* Version: "2.0.0-rc.18"
* Repo: https://github.com/Redocly/redoc
*/
!function(e,t){"object"==typeof exports&&"object"==typeof m
...
๐ โ ๐ ๐ ๐โโ ๐ช ๐ฆ ๐ป ๐ โช๏ธโก๏ธ ๐ ๐ฑ, & ๐ ๐ ๐ฅ ๐ป ๐ ๐ฉบ โ ๐ฅ.
๐ ๐ฅ ๐ช ๐ ๐ฑ โ๏ธ ๐ ๐ป ๐ ๐ฉบ.
โ ๐ง ๐ฉบ¶
๐ฅ ๐ โ ๐ง ๐ฉบ, ๐ โ๏ธ ๐ฒ ๐ข.
โ ๐ซ, โ ๐ซ ๐ None
๐โ ๐ ๐ FastAPI
๐ฑ:
from fastapi import FastAPI
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
app.mount("/static", StaticFiles(directory="static"), name="static")
@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_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)
@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("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
๐ ๐ ๐ฉบ¶
๐ ๐ ๐ช โ โก ๐ ๏ธ ๐ ๐ฉบ.
๐ ๐ช ๐ค-โ๏ธ FastAPI ๐ ๐ข โ ๐ธ ๐ ๐ฉบ, & ๐ถโโ๏ธ ๐ซ ๐ช โ:
openapi_url
: ๐ ๐โ ๐ธ ๐ ๐ฉบ ๐ช ๐ค ๐ ๐ ๐ ๐ ๏ธ. ๐ ๐ช โ๏ธ ๐ฅ ๐ขapp.openapi_url
.title
: ๐ ๐ ๐ ๏ธ.oauth2_redirect_url
: ๐ ๐ช โ๏ธapp.swagger_ui_oauth2_redirect_url
๐ฅ โ๏ธ ๐ข.swagger_js_url
: ๐ ๐โ ๐ธ ๐ ๐ฆ ๐ ๐ฉบ ๐ช ๐ค ๐ธ ๐. ๐ 1๏ธโฃ ๐ ๐ ๐ ๐ฑ ๐ ๐ฆ.swagger_css_url
: ๐ ๐โ ๐ธ ๐ ๐ฆ ๐ ๐ฉบ ๐ช ๐ค ๐ ๐. ๐ 1๏ธโฃ ๐ ๐ ๐ ๐ฑ ๐ ๐ฆ.
& โก ๐...
from fastapi import FastAPI
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
app.mount("/static", StaticFiles(directory="static"), name="static")
@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_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)
@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("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
Tip
โก ๐ ๏ธ swagger_ui_redirect
๐ฉโ๐ ๐โ ๐ โ๏ธ Oauth2๏ธโฃ.
๐ฅ ๐ ๐ ๏ธ ๐ ๐ ๏ธ โฎ๏ธ Oauth2๏ธโฃ ๐โ๐ฆบ, ๐ ๐ ๐ช ๐ & ๐ ๐ ๐ ๏ธ ๐ฉบ โฎ๏ธ ๐ ๐. & ๐ โฎ๏ธ โซ๏ธ โ๏ธ ๐ฐ Oauth2๏ธโฃ ๐ค.
๐ฆ ๐ ๐ ๐ต โซ๏ธ โ ๐ ๐, โ๏ธ โซ๏ธ ๐ช ๐ "โ" ๐ฉโ๐.
โ โก ๐ ๏ธ ๐ฏ โซ๏ธ¶
๐, ๐ช ๐ฏ ๐ ๐ ๐ท, โ โก ๐ ๏ธ:
from fastapi import FastAPI
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
app.mount("/static", StaticFiles(directory="static"), name="static")
@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_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)
@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("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
๐ฏ โซ๏ธ¶
๐, ๐ ๐ ๐ช ๐ ๐ ๐ป, ๐ถ ๐ ๐ฉบ http://127.0.0.1:8000/docs, & ๐ ๐.
& ๐ต ๐ธ, ๐ ๐ ๐ช ๐ ๐ฉบ ๐ ๐ ๏ธ & ๐ โฎ๏ธ โซ๏ธ.
๐ ๏ธ ๐ฆ ๐¶
๐ ๐ช ๐ โ ๐ฆ ๐ ๐ข.
๐ ๐ซ, ๐ถโโ๏ธ swagger_ui_parameters
โ ๐โ ๐ FastAPI()
๐ฑ ๐ โ๏ธ get_swagger_ui_html()
๐ข.
swagger_ui_parameters
๐จ ๐ โฎ๏ธ ๐ณ ๐ถโโ๏ธ ๐ฆ ๐ ๐.
FastAPI ๐ ๐ณ ๐ป โ ๐ซ ๐ โฎ๏ธ ๐ธ, ๐ โซ๏ธโ ๐ฆ ๐ ๐ช.
โ โ ๐ฆ¶
๐ผ, ๐ ๐ช โ โ ๐ฆ ๐ฆ ๐.
๐ต ๐ โ, โ ๐ฆ ๐ ๏ธ ๐ข:
โ๏ธ ๐ ๐ช โ โซ๏ธ โ syntaxHighlight
False
:
from fastapi import FastAPI
app = FastAPI(swagger_ui_parameters={"syntaxHighlight": False})
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
...& โคด๏ธ ๐ฆ ๐ ๐ ๐ซ ๐ฆ โ ๐ฆ ๐ซ๐:
๐ ๐ข¶
๐ ๐ ๐ ๐ช โ โ ๐ฆ ๐ข โฎ๏ธ ๐ "syntaxHighlight.theme"
(๐ ๐ โซ๏ธ โ๏ธ โฃ ๐):
from fastapi import FastAPI
app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
๐ ๐ณ ๐ ๐ โ ๐ฆ ๐จ ๐ข:
๐ ๐ข ๐ฆ ๐ ๐ข¶
FastAPI ๐ ๐ข ๐ณ ๐ข โ ๐ โ๏ธ ๐ผ.
โซ๏ธ ๐ ๐ซ ๐ข ๐ณ:
swagger_ui_default_parameters = {
"dom_id": "#swagger-ui",
"layout": "BaseLayout",
"deepLinking": True,
"showExtensions": True,
"showCommonExtensions": True,
}
๐ ๐ช ๐ ๐ ๐ซ โ ๐ ๐ฒ โ swagger_ui_parameters
.
๐ผ, โ deepLinking
๐ ๐ช ๐ถโโ๏ธ ๐ โ swagger_ui_parameters
:
from fastapi import FastAPI
app = FastAPI(swagger_ui_parameters={"deepLinking": False})
@app.get("/users/{username}")
async def read_user(username: str):
return {"message": f"Hello {username}"}
๐ ๐ฆ ๐ ๐ข¶
๐ ๐ ๐ ๐ช ๐ณ ๐ ๐ช โ๏ธ, โ ๐ ๐ฉบ ๐ฆ ๐ ๐ข.
๐ธ-๐ด โ¶
๐ฆ ๐ โ ๐ ๐ณ ๐ธ-๐ด ๐ (๐ผ, ๐ธ ๐ข).
FastAPI ๐ ๐ซ ๐ธ-๐ด presets
โ:
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
]
๐ซ ๐ธ ๐, ๐ซ ๐ป, ๐ ๐ช ๐ซ ๐ถโโ๏ธ ๐ซ โช๏ธโก๏ธ ๐ ๐ ๐.
๐ฅ ๐ ๐ช โ๏ธ ๐ธ-๐ด ๐ณ ๐ ๐, ๐ ๐ช โ๏ธ 1๏ธโฃ ๐ฉโ๐ฌ ๐. ๐ ๐ ๐ฆ ๐ โก ๐ ๏ธ & โ โ ๐ ๐ธ ๐ ๐ช.