2022-5-19 fastapi突然无法打开docs了
网上查了很多都是升级什么的。
我根据这篇文档修改好了:https://www.icode9.com/content-4-1383591.html
原因就是https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js 访问不通了,我还以为是内网限制了,自己用手机网络依据无法访问~
那我们就修改地址他的访问地址,我没有深入了解,根据上面办法解决了
我这里直接写linux修改文件的方法:
uos需要修改的文件路径在:/usr/local/lib/python3.7/dist-packages/fastapi/openapi/docs.py
其他的系统:find / -name docs.py
# swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js",
swagger_js_url: str = "https://petstore.swagger.io/swagger-ui-bundle.js",
# swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css",
swagger_css_url: str = "https://petstore.swagger.io/swagger-ui.css",
# swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
swagger_favicon_url: str = "https://petstore.swagger.io/favicon-32*32.png",
下面有一位仁兄有更好的办法:
from fastapi import FastAPI
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(
openapi_url=app.openapi_url,
title=app.title + " - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)