Python 使用烧瓶流式传输具有静态内容的模板

Python 使用烧瓶流式传输具有静态内容的模板,python,flask,jinja2,werkzeug,Python,Flask,Jinja2,Werkzeug,我正在尝试使用Flask流式传输模板,如下所示。因此,我的app.py看起来像: from flask import Response def stream_template(template_name, **context): app.update_template_context(context) t = app.jinja_env.get_template(template_name) rv = t.stream(context) rv.enable_bu

我正在尝试使用Flask流式传输模板,如下所示。因此,我的
app.py
看起来像:

from flask import Response

def stream_template(template_name, **context):
    app.update_template_context(context)
    t = app.jinja_env.get_template(template_name)
    rv = t.stream(context)
    rv.enable_buffering(5)
    return rv

@app.route('/my-large-page.html')
def render_large_template():
    rows = iter_all_rows()
    return Response(stream_template('the_template.html', rows=rows))
还有我的
模板.html

<p>This is some static content:</p>
<img src="{{ url_for('static', filename='logo.png') }}"/>

<p>This is some streamed content:</p>
{% for row in rows %}
<p>{{ row }}</p>
{% endfor %}
这是一些静态内容:

这是一些流式内容:

{第%行中的第%行} {{row}}

{%endfor%}
到目前为止,流媒体的效果还不错,但是静态内容在流媒体完成之前不会呈现。如何让Flask在流启动后立即呈现流和静态内容?

您能试试吗

from flask import Response, stream_with_context

return Response(stream_with_context(stream_template('the_template.html', rows=rows)))
我在浏览了stackoverflow上的所有帖子后发现了这一点,并使其正常工作。 此外,我认为应该在
iter\u all\u rows()
函数中使用
yield