如何删除“- Swagger UI”来自 FastAPI 中 OpenAPI 文档的 HTML 页面标题

发布于 2025-01-13 06:39:09 字数 358 浏览 0 评论 0原文

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

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

HTML 结果:

<title>Test - Swagger UI</title>

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

I am trying to customize my OpenAPI (Swagger UI) docs generated by FastAPI, but that string - Swagger UI still remains.

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

HTML result :

<title>Test - Swagger UI</title>

Is there a way to remove this - Swagger UI from the title?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2025-01-20 06:39:09

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

swagger_js_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js",
swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css"

但是,如果您不打算自定义 /docs,而只是想更改标题,则可以简单地在 get_swagger_ui_html()< 中自行设置它们。 /code> 下面的函数,因此,无论如何,默认情况下都会使用默认的 CDN URL。

例子

from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
    
app = FastAPI(title ="Test", 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)

The - Swagger UI part is added to the title by FastAPI itself. In order to change that, you would need to override the /docs route, as shown in the documentation for self-hosting the JS and CSS files for /docs. FastAPI provides the CDN URLs for JS and CSS files, and hence, you could pass those to the parameters below (you don't necessarily need to download and serve them as static files). These are:

swagger_js_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js",
swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css"

However, if you are not about to customise the /docs, but just want to change the title, you could simply omit from setting them on your own in the get_swagger_ui_html() function below, and thus, the default CDN URLs would be used by default, regardless.

Example

from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
    
app = FastAPI(title ="Test", 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)
聆听风音 2025-01-20 06:39:09

我让它工作的方式如下:

from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html

app = FastAPI(
    title="Docs Title",
    description="Documentation ...",
    version="1.0.0",
    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,
    )

这实际上强制 fastapi 使用定义的 app.get("/docs") 端点而不是默认端点。

The way I got this to work is as follows:

from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html

app = FastAPI(
    title="Docs Title",
    description="Documentation ...",
    version="1.0.0",
    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,
    )

This essentially forces fastapi to use the defined app.get("/docs") endpoint instead of the default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文