在加载swagger接口之前,我有一些数据库服务需要一些时间才能运行。因此,我想加载一个包含信息的HTML页面,然后将其重定向到swagger文档。
如何添加可以使用flask-restx加载的默认路由?
这段代码会做一些类似如下的事情
app = Flask(__name__)
@app.route("/start")
def hello():
return render_template("loading_spinner.html")
api = Api(
app,
version="1.0",
title="Python API",
description="API with Flask-RestX",
)
nsHealthCheck = api.namespace(
"api/v1/healthcheck", description="Health check for the API"
)在本例中,我希望在swagger接口之前加载/start。我该怎么做呢?
发布于 2021-07-26 06:29:55
我希望我答对了你的问题。
在我理解之后,解决方案可能是:
使用Flask problem
这个想法很简单,当用户等待加载页面时(更准确地说,直到您可以加载swagger文档为止),html加载器完成它的工作,然后bummm,您可以使用动态更新,如下所示:https://blog.miguelgrinberg.com/post/dynamically-update-your-flask-web-pages-using-turbo-flask
这是3pp:https://github.com/miguelgrinberg/turbo-flask,所以最有趣的部分在这里:
import time
# ...
def update_load():
with app.app_context():
while True:
time.sleep(5)
turbo.push(turbo.replace(render_template('loadavg.html'), 'load'))然后,您可以调整默认的API文档render,如下所示:https://flask-restx.readthedocs.io/en/latest/swagger.html#customization
from flask import Flask
from flask_restx import Api, apidoc
app = Flask(__name__)
api = Api(app)
@api.documentation
def custom_ui():
return apidoc.ui_for(api)当然,您甚至可以修改swagger的url。
另一种解决方案是通过修改swagger load,在那里插入SSE类型的load,我的意思是这样的:https://github.com/singingwolfboy/flask-sse,直到你发送你的作业状态,然后简单地替换页面。
可能是,我把这个简单的问题过于复杂化了。在这种情况下,我在等待更优雅的解决方案。
https://stackoverflow.com/questions/68260088
复制相似问题