Skip to content
Myr

Updated docs for streaming

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 343e678 · Browse files · 2 files+71 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+71 added−0 removed

  • other:+71 added−0 removed

2 files+71 added−0 removed

modified:docs/patterns/index.rst+1 added
33 unmodified lines
34 errorpages35 lazyloading36 mongokit37 favicon38 streaming
added:docs/patterns/streaming.rst+70 added
1Streaming 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 generate16data and to then invoke that function and pass it to a response object17that has the ``direct_passthrough`` flag set. This flag is used to inform18the system that data is generated on the fly and should be passed through19without buffering:2021.. sourcecode:: python2223 from flask import Response2425 @app.route('/large.csv')26 def generate_large_csv():27 def generate():28 for row in iter_all_rows():29 yield ','.join(row) + '\n'30 return Response(generate(), direct_passthrough=True,31 mimetype='text/csv')3233Each ``yield`` expression is directly sent to the browser. Now though34that some WSGI middlewares might break streaming, so be careful there in35debug environments with profilers and other things you might have enabled.3637Streaming from Templates38------------------------3940The Jinja2 template engine also supports rendering templates piece by41piece. This functionality is not directly exposed by Flask because it is42quite uncommon, but you can easily do it yourself:4344.. sourcecode:: python4546 from flask import Response4748 def stream_template(template_name, **context):49 app.update_template_context(context)50 t = app.jinja_env.get_template(template_name)51 rv = t.stream(context)52 rv.enable_buffering(5)53 return rv5455 @app.route('/my-large-page.html')56 def render_large_template():57 rows = iter_all_rows()58 return Response(stream_template('the_template.html', rows=rows),59 direct_passthrough=True)6061The trick here is to get the template object from the Jinja2 environment62on the application and to call :meth:`~jinja2.Template.stream` instead of63:meth:`~jinja2.Template.render` which returns a stream object instead of a64string. Since we're bypassing the Flask template render functions and65using the template object itself we have to make sure to update the render66context ourselves by calling :meth:`~flask.Flask.update_template_context`.67The template is then evaluated as the stream is iterated over. Since each68time you do a yield the server will flush the content to the client you69might want to buffer up a few items in the template which you can do with70``rv.enable_buffering(size)``. ``5`` is a sane default.