fastapi swagger js css 都是写死在代码中的,他的正常传参修改地址不没有打开的,所以不能用正常的方式修改他的内置js css 路径:
原始路径为:
swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js",
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css",
墙内很难打开
解决方案:
在生命app=FastAPI(....)之前粘贴下面代码,可以修改swagger js css 引用路径
from fastapi import applications
from fastapi.openapi.docs import get_swagger_ui_html
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="https://cdn.staticfile.org/swagger-ui/4.15.5/swagger-ui-bundle.min.js",
swagger_css_url="https://cdn.staticfile.org/swagger-ui/4.15.5/swagger-ui.min.css")
# Actual monkey patch
applications.get_swagger_ui_html = swagger_monkey_patch
# app = FastAPI(title="xxx",..................)