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
Provenance
- Commit
4d2c818- Tree
5e62dcd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
343e678
2 files changed+71 added−0 removed
modified:docs/patterns/index.rst+1 added
33 unmodified lines34 errorpages35 lazyloading36 mongokit37 favicon38+ streamingadded:docs/patterns/streaming.rst+70 added
1+Streaming Contents2+==================3+4+Sometimes you want to send an enormous amount of data to the client, much5+more than you want to keep in memory. When you are generating the data on6+the fly though, how do you send that back to the client without the7+roundtrip to the filesystem?8+9+The answer is by using generators and direct responses.10+11+Basic Usage12+-----------13+14+This is a basic view function that generates a lot of CSV data on the fly.15+The trick is to have an inner function that uses a generator to generate16+data and to then invoke that function and pass it to a response object17+that has the ``direct_passthrough`` flag set. This flag is used to inform18+the system that data is generated on the fly and should be passed through19+without buffering:20+21+.. sourcecode:: python22+23+ from flask import Response24+25+ @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')32+33+Each ``yield`` expression is directly sent to the browser. Now though34+that some WSGI middlewares might break streaming, so be careful there in35+debug environments with profilers and other things you might have enabled.36+37+Streaming from Templates38+------------------------39+40+The Jinja2 template engine also supports rendering templates piece by41+piece. This functionality is not directly exposed by Flask because it is42+quite uncommon, but you can easily do it yourself:43+44+.. sourcecode:: python45+46+ from flask import Response47+48+ 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 rv54+55+ @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)60+61+The trick here is to get the template object from the Jinja2 environment62+on the application and to call :meth:`~jinja2.Template.stream` instead of63+:meth:`~jinja2.Template.render` which returns a stream object instead of a64+string. Since we're bypassing the Flask template render functions and65+using the template object itself we have to make sure to update the render66+context ourselves by calling :meth:`~flask.Flask.update_template_context`.67+The template is then evaluated as the stream is iterated over. Since each68+time you do a yield the server will flush the content to the client you69+might want to buffer up a few items in the template which you can do with70+``rv.enable_buffering(size)``. ``5`` is a sane default.