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?
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.
get_redoc_html
fastapi.openapi.docs
<redoc spec-url="{openapi_url}">
<redoc spec-url="{openapi_url}" hide-download-button>
hide-download-button
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)
Copyright © 2021 Jogjafile Inc.
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 fromfastapi.openapi.docs
, we can make the small modification of<redoc spec-url="{openapi_url}">
to<redoc spec-url="{openapi_url}" hide-download-button>
, adding thehide-download-button
attribute.All in all the code becomes