隐藏“下载 OpenAPI 规范”按钮。

huangapple go评论64阅读模式
英文:

Hide the "Download OpenAPI specification" button

问题

我正在使用FastAPI,我希望/redoc页面不显示下载按钮。

我知道用户仍然可以下载规范,但我需要隐藏此按钮。

FastAPI会自动生成/redoc页面,我该如何做到这一点?

英文:

I'm using FastAPI and I want the /redoc page to not display the Download button.

IMAGE

I know that users will still be able to download the specification, but I need to hide this button.

FastApi automatically generates /redoc, how can I do this?

答案1

得分: 3

以下是翻译好的部分:

所以,在 Redoc 中有一个隐藏下载按钮的选项,我们只需从 FastAPI 中获取它。似乎没有任何方式可以将配置传递到 FastAPI 中的 Redoc。然而,你可以覆盖静态文件本身,通过复制 fastapi.openapi.docs 中的 get_redoc_html 函数,我们可以进行小的修改,将 <redoc spec-url="{openapi_url}"> 改为 <redoc spec-url="{openapi_url}" hide-download-button>,添加 hide-download-button 属性。

总之,代码变为

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI(redoc_url=None)


def get_custom_redoc_html(
    *,
    openapi_url: str,
    title: str,
    redoc_js_url: str = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
    redoc_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
    with_google_fonts: bool = True,
) -> HTMLResponse:
    html = f"""
    <!DOCTYPE html>
    <html>
    <head>
    <title>{title}</title>
    <!-- needed for adaptive design -->
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    """
    if with_google_fonts:
        html += """
        <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
        """
    html += f"""
    <link rel="shortcut icon" href="{redoc_favicon_url}">
    <!--
    ReDoc doesn't change outer page styles
    -->
    <style>
      body {{
        margin: 0;
        padding: 0;
      }}
    </style>
    </head>
    <body>
    <noscript>
        ReDoc 需要启用 Javascript 才能正常运行。请启用它以浏览文档。
    </noscript>
    <redoc spec-url="{openapi_url}" hide-download-button></redoc>
    <script src="{redoc_js_url}"> </script>
    </body>
    </html>
    """
    return HTMLResponse(html)


@app.get("/redoc", include_in_schema=False)
async def redoc_try_it_out() -> HTMLResponse:
    return get_custom_redoc_html(openapi_url=app.openapi_url, title=app.title)
英文:

So, there is an option in redoc to hide the download button, we just have to get it in from FastAPI. There doesn't really seem to be any way to pass configuration into Redoc from FastAPI. However, what you can do is override the static files themselves, so by copying the get_redoc_html function from fastapi.openapi.docs, we can make the small modification of &lt;redoc spec-url=&quot;{openapi_url}&quot;&gt; to &lt;redoc spec-url=&quot;{openapi_url}&quot; hide-download-button&gt;, adding the hide-download-button attribute.

All in all the code becomes

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI(redoc_url=None)


def get_custom_redoc_html(
    *,
    openapi_url: str,
    title: str,
    redoc_js_url: str = &quot;https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js&quot;,
    redoc_favicon_url: str = &quot;https://fastapi.tiangolo.com/img/favicon.png&quot;,
    with_google_fonts: bool = True,
) -&gt; HTMLResponse:
    html = f&quot;&quot;&quot;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;{title}&lt;/title&gt;
    &lt;!-- needed for adaptive design --&gt;
    &lt;meta charset=&quot;utf-8&quot;/&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
    &quot;&quot;&quot;
    if with_google_fonts:
        html += &quot;&quot;&quot;
    &lt;link href=&quot;https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700&quot; rel=&quot;stylesheet&quot;&gt;
    &quot;&quot;&quot;
    html += f&quot;&quot;&quot;
    &lt;link rel=&quot;shortcut icon&quot; href=&quot;{redoc_favicon_url}&quot;&gt;
    &lt;!--
    ReDoc doesn&#39;t change outer page styles
    --&gt;
    &lt;style&gt;
      body {{
        margin: 0;
        padding: 0;
      }}
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;noscript&gt;
        ReDoc requires Javascript to function. Please enable it to browse the documentation.
    &lt;/noscript&gt;
    &lt;redoc spec-url=&quot;{openapi_url}&quot; hide-download-button&gt;&lt;/redoc&gt;
    &lt;script src=&quot;{redoc_js_url}&quot;&gt; &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    &quot;&quot;&quot;
    return HTMLResponse(html)


@app.get(&quot;/redoc&quot;, include_in_schema=False)
async def redoc_try_it_out() -&gt; HTMLResponse:
    return get_custom_redoc_html(openapi_url=app.openapi_url, title=app.title)

huangapple
  • 本文由 发表于 2023年7月4日 22:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定