fastapi的docs页面是空白解决

devtools/2024/10/20 20:25:15/

环境:openEuler、python 3.11.6、fastapi 0.115.2

背景:居家办公,默认搭建的fastapi的docs接口为空白

时间:20241016

说明:网上很多教程的缺点是复杂(但是能够了解的更清楚),使用官方文档解决很便利

官方文档地址:Custom Docs UI Static Assets

 1、搭建环境

 安装相应的python

pip install fastapi uvicorn

创建main文件:

python"># main.py
from fastapi import FastAPI
app = FastAPI()@app.get("/") # 根路由
async def root():return "I want to change the world"if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)# 启动命令:uvicorn main:app --reload --host 0.0.0.0 --port 8000

运行测试:

说明启动成功

python">(venv) [jack@Laptop-L14-gen4 fastTest]$ uvicorn main:app --reload --host 0.0.0.0 --port 8000
INFO:     Will watch for changes in these directories: ['/home/jack/fastTest']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [6119] using StatReload
INFO:     Started server process [6121]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

 浏览器查看

 至此,说明基本环境搭建完毕

2、问题发现

默认访问 http://172.26.20.199:8000/docs 应该如下:

可问题却是空白

 原因分析:

fastapi的文件是提供的CDN为国外的网址,偶尔可能存在网络延迟,导致为空白,使用F12可以看到是文件获取不到

3、解决问题

第一种方式的再试试,存在这种可能性

第二种方式为复制官方文档提供的部分代码:

python"># 增加的代码部分from fastapi.openapi.docs import (get_redoc_html,get_swagger_ui_html,get_swagger_ui_oauth2_redirect_html,
)app = FastAPI(docs_url=None, redoc_url=None)@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="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js",swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css",)

上述代码完全无脑复制到你的main.py中,覆盖app = 这一行即可,复制完如下:

python"># main.py
from fastapi import FastAPIfrom fastapi.openapi.docs import (get_redoc_html,get_swagger_ui_html,get_swagger_ui_oauth2_redirect_html,
)app = FastAPI(docs_url=None, redoc_url=None)@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="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js",swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css",)@app.get("/") # 根路由
async def root():return "I want to change the world"if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)# 启动命令:uvicorn main:app --reload --host 0.0.0.0 --port 8000

保存,fastapi会自动重新加载,刷新网页即可。


http://www.ppmy.cn/devtools/127375.html

相关文章

[含文档+PPT+源码等]精品基于ssm实现的原生微信小程序线上养花系统的设计与实现

基于SSM(Spring、SpringMVC、MyBatis)实现的原生微信小程序线上养花系统的设计与实现背景,可以从以下几个方面进行阐述: 一、选题背景 随着人们生活水平的提高和环境保护意识的增强,养花已经成为一种流行的休闲活动。…

6.C++经典实例-计算给定范围内的素数(质数)

所谓素数&#xff08;也称为质数&#xff09;是指大于1的自然数&#xff0c;且只能被1和它本身整除的数。例如&#xff0c;2、3、5、7、11等都是素数。 下面是计算3000内所有的素数的实例 #include <iostream> #include <vector> #include <cmath>// 判断一…

止步阿里一面。。。

时间过的真快&#xff0c;转眼间国庆已经过去一周了&#xff0c;又到了新的一周&#xff0c;继续分享最新的面经。 今天分享的是粉丝在阿里巴巴的一面&#xff0c;考察了数据库、redis、kafka、ES和项目&#xff0c;数据库和redis不用多说&#xff0c;项目必用面试必考&#x…

5.C++经典实例-判断输入的年份是否为闰年

要判断一个年份是否为闰年&#xff0c;首先要明白闰年的定义&#xff1a; 如果年份能被4整除但不能被100整除&#xff0c;则是闰年。 如果年份能被400整除&#xff0c;也是闰年。 其他情况则不是闰年。 根据上面的逻辑&#xff0c;我们写代码&#xff1a; #include <io…

Redis Windows最新安装教程(2024.10.10)

文章目录 redis介绍下载地址安装流程基础操作测试Redis常用的服务指令redis介绍 Redis(Remote Dictionary Server)是一个开源的、基于内存的数据结构存储系统,常用作数据库、缓存和消息中间件。Redis具有快速、灵活、可扩展和高可用性等特点。 Redis支持多种数据结构,包括…

《计算机视觉》—— 基于dlib库的人检检测

文章目录 一、dlib库的安装1. 通过PyCharm的Settings安装2. 通过Anaconda安装&#xff08;适用于Windows等操作系统&#xff09;3. 通过命令行安装4.懒人安装 二、基于dlib库的人检测1.对图像进行人脸检测2.打开电脑摄像头&#xff0c;检测人脸 一、dlib库的安装 在PyCharm中&…

Flume面试整理-设计一个Flume数据流方案

设计一个Apache Flume数据流方案涉及多个因素,包括数据源的类型、通道(Channel)的选择、数据的目标系统(Sink),以及如何实现高吞吐量、可靠性和可扩展性。以下是一个完整的Flume数据流方案设计示例,适用于从多个Web服务器收集日志并将其传输到HDFS进行后续分析的场景。 …

如何解决JMeter响应数据乱码?

问题&#xff1a; 解决&#xff1a; 1、找到JMeter安装目录下的bin目录 2、 在bin目录下&#xff0c;打开" jmeter.properties "文件 3、搜索"sampleresult.default.encoding" 4、改成"sampleresult.default.encodingUTF-8"&#xff0c;去掉前面…