flask处理数据,页面实时刷新展示

时间:2022-07-31 07:14:19

背景:

后端 flask(python)处理数据,页面实时刷新,类似于打包页面的动态展示,展示效果如图:

flask处理数据,页面实时刷新展示

代码如下:

前端主要使用以下循环处理, 2--- 2秒刷新一次

{% if 0 == stop %}
<meta http-equiv="refresh" content="2;url={{ url_for('test') }}">
{% endif %}

html:

 <html>
<head>
{% if 0 == stop %}
<meta http-equiv="refresh" content="2;url={{ url_for('test') }}">
{% endif %}
</head>
<body> <div class="header">
<h4>标题XXX</h4>
</div>
<div class="body">
<form class="form" action="/test" method="post">
<div class="form">
<button class="btn btn-primary" type="submit" >开始</button>
</div>
</form>
</div> <div class="c" id="show2" >
<div class="header">
<h4>结果展示:</h4>
<p style="white-space:pre-line;">{{ s }}</p>
</div> </div>
</body>
<!- 根据自己实际路径填写>
<script type="text/javascript" src="static/js/main.min.js"></script>
<script type="text/javascript" src="static/js/jquery.min.js"></script> <script type="text/javascript"> </script>
</html>

python代码如下:

 from flask import Flask,render_template,request

 app = Flask(__name__)

 @app.route('/test',methods=['GET','POST'])
def test():
s = testDis()
stop = 0
# notify(access_token, ('调用地址:','tools_updateTimes'), ('调用名称','修改司机交易测试'),'','','','')
if s == 'z':
stop = 1
return render_template("test/test.html",s = s,stop=stop) import random
def testDis():
s =random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')
if s == 'a':
s = s+'\n'+s
return s if __name__ == '__main__':
app.run(host='0.0.0.0')