英文:
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.
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 <redoc spec-url="{openapi_url}">
to <redoc spec-url="{openapi_url}" hide-download-button>
, 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 = "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 requires Javascript to function. Please enable it to browse the documentation.
</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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论