产品网站开发流程图,华为手机网站建设策划书,做网站前置审批,中英文网站开发公司使用fastapi的时候#xff0c;swagger-ui.css 、swagger-ui-bundle.js、redoc.standalone.js 有时候无法加载#xff08;国内环境原因或者是局域网屏蔽#xff09;#xff0c;此时就需要自己用魔法下载好对应文件#xff0c;然后替换到fastapi里面去。
fastapi里面依靠这…使用fastapi的时候swagger-ui.css 、swagger-ui-bundle.js、redoc.standalone.js 有时候无法加载国内环境原因或者是局域网屏蔽此时就需要自己用魔法下载好对应文件然后替换到fastapi里面去。
fastapi里面依靠这2个函数实现docs和redoc
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_htmlfastapi里面官网给的解决办法
https://github.com/tiangolo/fastapi/blob/ac93277d3b506f4076aee1af6ae5a86406f545c6/docs_src/custom_docs_ui/tutorial001.py#L4
实践起来就是首先下载好这些文件
其次复现出/docs路由和/redoc路由
import os
import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_htmlapp FastAPI(titleoutpainting_captioning_upscaler,descriptionoutpainting images, captioning images,upscaler images,version1.0.0,docs_urlNone,redoc_urlNone, # 设置 ReDoc 文档的路径)
app.add_middleware(CORSMiddleware,allow_origins[*],allow_credentialsTrue,allow_methods[*],allow_headers[*],
)
app.mount(/static, StaticFiles(directorystatic), namestatic)app.get(/docs, include_in_schemaFalse)
async def custom_swagger_ui_html():return get_swagger_ui_html(openapi_url/openapi.json,titlexx,# oauth2_redirect_urlapp.swagger_ui_oauth2_redirect_url,swagger_js_url/static/swagger/swagger-ui-bundle.js,swagger_css_url/static/swagger/swagger-ui.css,swagger_favicon_url/static/swagger/img.png,)app.get(/redoc, include_in_schemaFalse)
async def redoc_html():return get_redoc_html(openapi_urlapp.openapi_url,titleapp.title - ReDoc,redoc_js_url/static/swagger/redoc.standalone.js,)# 写个测试接口
app.get(/xx)
async def test():return {message: test success}if __name__ __main__:uvicorn.run(f{os.path.basename(__file__).split(.)[0]}:app,host0.0.0.0,port7777,reloadFalse,workers1)
其中文件也可以去这里下载
下载:https://wwsm.lanzouy.com/i682Q1frjnmd 密码:9zw2成功