streaming.rst
This file
Path
docs/patterns/streaming.rst
61 lines · 2.4 KiB
Last changed by
Direct passthrough is counter productiveArmin Ronacherbefore adoption54875e9
History of this fileTake it
Updated docs for streamingArmin Ronacherbefore adoption4d2c818
lines 1–15why, one step back1Streaming Contents2==================34Sometimes you want to send an enormous amount of data to the client, much5more than you want to keep in memory. When you are generating the data on6the fly though, how do you send that back to the client without the7roundtrip to the filesystem?89The answer is by using generators and direct responses.1011Basic Usage12-----------1314This is a basic view function that generates a lot of CSV data on the fly.15The trick is to have an inner function that uses a generator to generateDirect passthrough is counter productiveArmin Ronacherbefore adoption54875e9
line 16why, one step back16data and to then invoke that function and pass it to a response object::Updated docs for streamingArmin Ronacherbefore adoption4d2c818
lines 17–24why, one step back1718 from flask import Response1920 @app.route('/large.csv')21 def generate_large_csv():22 def generate():23 for row in iter_all_rows():24 yield ','.join(row) + '\n'Direct passthrough is counter productiveArmin Ronacherbefore adoption54875e9
line 25why, one step back25 return Response(generate(), mimetype='text/csv')Updated docs for streamingArmin Ronacherbefore adoption4d2c818
lines 26–35why, one step back2627Each ``yield`` expression is directly sent to the browser. Now though28that some WSGI middlewares might break streaming, so be careful there in29debug environments with profilers and other things you might have enabled.3031Streaming from Templates32------------------------3334The Jinja2 template engine also supports rendering templates piece by35piece. This functionality is not directly exposed by Flask because it isNo need for the directive here.Armin Ronacherbefore adoption029eebe
line 36why, one step back36quite uncommon, but you can easily do it yourself::Updated docs for streamingArmin Ronacherbefore adoption4d2c818
lines 37–49why, one step back3738 from flask import Response3940 def stream_template(template_name, **context):41 app.update_template_context(context)42 t = app.jinja_env.get_template(template_name)43 rv = t.stream(context)44 rv.enable_buffering(5)45 return rv4647 @app.route('/my-large-page.html')48 def render_large_template():49 rows = iter_all_rows()Direct passthrough is counter productiveArmin Ronacherbefore adoption54875e9
line 50why, one step back50 return Response(stream_template('the_template.html', rows=rows))Updated docs for streamingArmin Ronacherbefore adoption4d2c818
lines 51–61why, one step back5152The trick here is to get the template object from the Jinja2 environment53on the application and to call :meth:`~jinja2.Template.stream` instead of54:meth:`~jinja2.Template.render` which returns a stream object instead of a55string. Since we're bypassing the Flask template render functions and56using the template object itself we have to make sure to update the render57context ourselves by calling :meth:`~flask.Flask.update_template_context`.58The template is then evaluated as the stream is iterated over. Since each59time you do a yield the server will flush the content to the client you60might want to buffer up a few items in the template which you can do with61``rv.enable_buffering(size)``. ``5`` is a sane default.