烧瓶插座 |使用 Flask Executor 或 ThreadPoolExecutor 创建的后台任务更新和绘制图表新和、烧瓶、图表、插座

由网友(我还是那个干净的少年)分享简介:我目前正在尝试在数据框的结果更新或数据发生任何变化时成功绘制条形图.设置:Flask-SocketIO==4.3.1python-engineio==3.13.2python-socketio==4.6.0确保您重新启动机器以免遇到错误.Python 代码 - 保存在 D:Projectsest_background...

我目前正在尝试在数据框的结果更新或数据发生任何变化时成功绘制条形图.

设置:

Flask-SocketIO==4.3.1python-engineio==3.13.2python-socketio==4.6.0flask是什么意思,flask怎么读,flask翻译为 瓶,长颈瓶 烧 听力课堂在线翻译

确保您重新启动机器以免遇到错误.

Python 代码 - 保存在 D:Projects est_backgroundtask:

from flask import Flask, render_template, request将熊猫导入为 pd从 flask_executor 导入执行器导入情节导入 plotly.graph_objs从 flask_socketio 导入 SocketIO,发出导入json全局 test_valapp = Flask(__name__)socketio = SocketIO(应用程序)def create_plot(feature_importance):feature_importance=feature_importance.reset_index(drop=True)feature_importance=feature_importance.iloc[0:5]打印(特征重要性)数据 = [去吧(x=feature_importance['Age'], # 指定 x 作为数据框列 'x'y=feature_importance['名称'],方向='h')]graphJSON = json.dumps(数据,cls=plotly.utils.PlotlyJSONEncoder)返回图JSON@socketio.on(响应")def background_task_func():全局 test_val全局图socketio.sleep(10)数据= {'姓名':['汤姆','约瑟夫','克里什','约翰'],'年龄':[20,21,19,18]}test_val= pd.DataFrame(数据)bar = create_plot(test_val)情节=酒吧如果 test_val.shape[0]>1:打印(test_val)发射('response_output',绘图,广播=真)socketio.sleep(1)#return render_template('views/index_img_soc.html', plot=bar)@app.route('/', 方法=['GET'])定义索引():全局图executor.submit(background_task_func)bar = create_plot(test_val)return render_template('views/index_img_soc.html', plot=bar)如果 __name__ == __main__":数据={'姓名':[],'年龄':[]}test_val= pd.DataFrame(数据)执行者 = 执行者(应用程序)socketio.run(应用程序)

Html 代码(保存在 D:Projects est_backgroundtask emplateviews 中):

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js>></script>