Skip to content

โ†” ๐Ÿ—„

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๏ธโƒฃ ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿ”›. ๐Ÿ” ๐ŸŒ ๐Ÿฆ ๐ŸŽš โžก ๐Ÿ› ๏ธ & โŽ โœ ๐Ÿ™† ๐Ÿ•ธ ๐Ÿ‘† ๐Ÿ’ช.