DODS page cannot access CSS and JS #6475
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}DescriptionRequest URL: https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css Operating SystemLinux Operating System DetailsNo response FastAPI Versionfastapi==0.54.1 Python Versionpython 3.7.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
My solution is to download Swagger-ui, which must include favicon.png、swagger-ui.css、swagger-ui-bundle.js,Then create a new static folder Provides static file services: from fastapi import applications
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
app.mount("/static", StaticFiles(directory="./static"), name="static")
def swagger_monkey_patch(*args, **kwargs):
return get_swagger_ui_html(
*args,
**kwargs,
swagger_js_url="/static/swagger-ui/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui/swagger-ui.css",
swagger_favicon_url="/static/swagger-ui/favicon.png"
)
applications.get_swagger_ui_html = swagger_monkey_patch |
Beta Was this translation helpful? Give feedback.
-
|
Perhaps downloading Swagger-UI files will solve this problem. if static_root:
app.mount('/static', StaticFiles(directory=static_root), name='static')
else:
app.mount('/static', StaticFiles(directory=f"{root}/src/static"), name='static')
def swagger_monkey_patch(*args, **kwargs):
"""
Wrap the function which is generating the HTML for the /docs endpoint and
overwrite the default values for the swagger js and css.
"""
return get_swagger_ui_html(
*args, **kwargs,
swagger_js_url="/static/dist/swagger-ui-bundle.js",
swagger_css_url="/static/dist/swagger-ui.css")
# Actual monkey patch
applications.get_swagger_ui_html = swagger_monkey_patch |
Beta Was this translation helpful? Give feedback.
-
|
It is normal now, thank you |
Beta Was this translation helpful? Give feedback.
Docs on this: https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/