【问题标题】:How to remove "- Swagger UI" from HTML Page title of OpenAPI docs in FastAPI如何从 FastAPI 中 OpenAPI 文档的 HTML 页面标题中删除“-Swagger UI”
【发布时间】:2022-10-15 19:55:43
【问题描述】:

我正在尝试自定义由 FastAPI 生成的 OpenAPI (Swagger UI) 文档,但该字符串 - Swagger UI 仍然存在。

app = FastAPI(
    title="Test",
    version="0.1.0"
)

HTML 结果:

<title>Test - Swagger UI</title>

有没有办法从标题中删除这个- Swagger UI

【问题讨论】:

    标签: python swagger swagger-ui fastapi openapi


    【解决方案1】:

    - Swagger UI 部分由 FastAPI 添加到标题中。要改变这一点,您需要覆盖/docs 路由,如documentation 所示,当您希望自托管文档的JS 和CSS 文件时。 FastAPI 为 JS 和 CSS 文件提供了CDN URLs,因此您可以将它们传递给下面的参数(不一定需要下载并作为静态文件提供)。示例如下:

    from fastapi.openapi.docs import (
        get_swagger_ui_html,
        get_swagger_ui_oauth2_redirect_html,
    )
    
    app = FastAPI(title ="Test", version="0.1.0", docs_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,
            oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
            swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js",
            swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-17
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多