streaming.rst
This file
Path
docs/patterns/streaming.rst
85 lines · 2.6 KiB
Last changed by
Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a444c5 point at it.
update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
lines 1–22why, 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 generate16data and to then invoke that function and pass it to a response object::1718 @app.route('/large.csv')19 def generate_large_csv():20 def generate():21 for row in iter_all_rows():22 yield f"{','.join(row)}\n"Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
line 23why, one step back23 return generate(), {"Content-Type": "text/csv")update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
lines 24–31why, one step back2425Each ``yield`` expression is directly sent to the browser. Note though26that some WSGI middlewares might break streaming, so be careful there in27debug environments with profilers and other things you might have enabled.2829Streaming from Templates30------------------------31Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 32–35why, one step back32The Jinja2 template engine supports rendering a template piece by33piece, returning an iterator of strings. Flask provides the34:func:`~flask.stream_template` and :func:`~flask.stream_template_string`35functions to make this easier to use.update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 36why, one step back36Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
line 37why, one step back37.. code-block:: pythonupdate tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 38why, one step back38Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
line 39why, one step back39 from flask import stream_templateupdate tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 40why, one step back40Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 41–43why, one step back41 @app.get("/timeline")42 def timeline():43 return stream_template("timeline.html")update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 44why, one step back44Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 45–48why, one step back45The parts yielded by the render stream tend to match statement blocks in46the template.4748update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
lines 49–51why, one step back49Streaming with Context50----------------------51Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 52–54why, one step back52The :data:`~flask.request` will not be active while the generator is53running, because the view has already returned at that point. If you try54to access ``request``, you'll get a ``RuntimeError``.update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 55why, one step back55Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 56–58why, one step back56If your generator function relies on data in ``request``, use the57:func:`~flask.stream_with_context` wrapper. This will keep the request58context active during the generator.update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 59why, one step back59Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 60–61why, one step back60.. code-block:: python61update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 62why, one step back62 from flask import stream_with_context, requestMerge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
line 63why, one step back63 from markupsafe import escapeupdate tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
lines 64–67why, one step back6465 @app.route('/stream')66 def streamed_response():67 def generate():Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 68–71why, one step back68 yield '<p>Hello '69 yield escape(request.args['name'])70 yield '!</p>'71 return stream_with_context(generate())update tests for new werkzeug client (#3827)David Lordbefore adoption7a444c5
line 72why, one step back72Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 73–85why, one step back73It can also be used as a decorator.7475.. code-block:: python7677 @stream_with_context78 def generate():79 ...8081 return generate()8283The :func:`~flask.stream_template` and84:func:`~flask.stream_template_string` functions automatically85use :func:`~flask.stream_with_context` if a request is active.