我有一个生成数据并实时流式传输的视图.我无法弄清楚如何将这些数据发送到我可以在我的HTML模板中使用的变量.我当前的解决方案只是在数据到达时将数据输出到空白页面,这有效,但我希望将其包含在带格式的较大页面中.如何在数据流式传输到页面时更新,格式化和显示数据?
import flask
import time, math
app = flask.Flask(__name__)
@app.route('/')
def index():
def inner():
# simulate a long process to watch
for i in range(500):
j = math.sqrt(i)
time.sleep(1)
# this value should be inserted into an HTML template
yield str(i) + '<br/>\n'
return flask.Response(inner(), mimetype='text/html')
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)