fastapi 中文档显示空白

原因: fastapi swagger js css 都是写死在代码中的,他的正常传参修改地址不没有打开的,所以不能用正常的方式修改他的内置js css 路径:
原始路径为:
墙内很难打开

解决方案:
在生命app=FastAPI(....)之前粘贴下面代码,可以修改swagger js css 引用路径

from fastapi import applications
from fastapi.openapi.docs import get_swagger_ui_html


def swagger_monkey_patch(*args, **kwargs):
    """
    Wrap the function which is generating the HTML for the /docs endpoint and
    overwrite the default values for the swagger js and css.
    """
    # 国内优秀的资源库 https://www.liangwei.cc/website_tech/jsdelivr_zha_le_guo_nei_ti_dai_fang_an.html, 这里用的是七牛云的
    return get_swagger_ui_html(
        *args, **kwargs,
        swagger_js_url="https://cdn.staticfile.org/swagger-ui/5.2.0/swagger-ui-bundle.min.js",
        swagger_css_url="https://cdn.staticfile.org/swagger-ui/5.2.0/swagger-ui.min.css")


# Actual monkey patch
applications.get_swagger_ui_html = swagger_monkey_patch

# app = FastAPI(title="xxx",..................)
posted @ 2023-07-31 15:24  つつつつつつ  阅读(185)  评论(0编辑  收藏  举报