其中的例子简化修改一下如下:本文地址:http://www.04007.cn/article/1142.html,未经许可,不得转载.
#引入库要的库 from fastapi import FastAPI from fastapi.openapi.docs import get_swagger_ui_html, #Import StaticFiles. from fastapi.staticfiles import StaticFiles #定义app,这步很重要,一定要传入docs_url=None, #Disable the automatic docs¶ #The first step is to disable the automatic docs, as those use the CDN by default. #To disable them, set their URLs to None when creating your FastAPI app: app = FastAPI(docs_url=None, redoc_url=None) #"Mount" a StaticFiles() instance in a specific path. 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", swagger_favicon_url='/docs/favicon.png', )因为我们虽然用的fastapi,但是对其经过了封装处理,并不是直接如上面这种使用,我曾简单的尝试了一下切换成静态资源,没有切换成功,怀疑和我们的封装有关,今天经过排查发现要使用本地静态资源时,在定义app时一定要传入docs_url=None,不然没法成功,这步很重要。 本文地址:http://www.04007.cn/article/1142.html,未经许可,不得转载.
本文地址:http://www.04007.cn/article/1142.html,未经许可,不得转载.
本文地址:http://www.04007.cn/article/1142.html 未经许可,不得转载. 手机访问本页请扫描右下方二维码.
![]() |
![]() |
手机扫码直接打开本页面 |