fastapi更改swagger、redoc的ui地址

from fastapi.openapi.docs import get_swagger_ui_html,get_redoc_html
from fastapi import FastAPI,applications
def swagger_ui_patch(*args, **kwargs):
    return get_swagger_ui_html(
    *args, **kwargs,
    swagger_js_url='/statics/swagger-ui/swagger-ui-bundle.js',
    swagger_css_url='/statics/swagger-ui/swagger-ui.css',
    swagger_favicon_url='/statics/swagger-ui/favicon.png',
    )

def redoc_ui_path(*args,**kwargs):
    return get_redoc_html(*args,**kwargs,
    redoc_js_url='/statics/swagger-ui/redoc.standalone.js',
    redoc_favicon_url='/statics/swagger-ui/favicon.png',
    )

applications.get_swagger_ui_html=swagger_ui_patch
applications.get_redoc_html=redoc_ui_path



app=FastAPI(title='测试项目')


# 挂载静态文件
app.mount('/statics',StaticFiles(directory='statics'),name='statics')

先在项目中创建statics的目录,然后下载swagger-ui-bundle.js、swagger-ui.css、favicon.png、redoc.standalone.js

,下载地址分别如下:

https://cdn.bootcdn.net/ajax/libs/swagger-ui/5.6.2/swagger-ui-bundle.js

https://cdn.bootcdn.net/ajax/libs/swagger-ui/5.6.2/swagger-ui.css

https://fastapi.tiangolo.com

https://unpkg.com/redoc@2.0.0-rc.75/bundles/redoc.standalone.js

下载之后,把相关文件放入该目录statics/swagger-ui/。然后,配置代码如上。

posted @ 2024-04-23 17:42  stone9693  阅读(933)  评论(0)    收藏  举报
GitHub账户:https://github.com/stone9693