如图:
原因是FastAPI的接口文档默认使用https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css
和https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js
来渲染页面,而这两个URL是外网的CDN,在国内响应超慢,导致请求超时了。
解决方案是更换CDN或把这两个URL对应的文件下载到本地的static目录中并挂载它,具体代码如下:
from fastapi import FastAPI
# pip install fastapi_cdn_host
from fastapi_cdn_host import monkey_patch_for_docs_ui
app = FastAPI()
monkey_patch_for_docs_ui(app) # 会自动将CDN更换为响应速度较快的unpkg.com