Skip to content
Myr

streaming.rst

This file

Path

docs/patterns/streaming.rst

85 lines · 2.6 KiB

Last changed by

Merge branch '2.2.x'David Lordbefore adoption73b9bac

History of this file

Take it

The walk stopped after 200 commits: lines older than 41aaaf7 point at it.

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

lines 1–22why, one step back
1Streaming Contents
2==================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 branch '2.2.x'David Lordbefore adoption73b9bac

line 23why, one step back
23 return generate(), {"Content-Type": "text/csv"}

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

lines 24–31why, one step back
24
25Each ``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------------------------31

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 32–35why, one step back
32The Jinja2 template engine supports rendering a template piece by
33piece, 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.

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 36why, one step back
36

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

line 37why, one step back
37.. code-block:: python

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 38why, one step back
38

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

line 39why, one step back
39 from flask import stream_template

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 40why, one step back
40

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 41–43why, one step back
41 @app.get("/timeline")
42 def timeline():43 return stream_template("timeline.html")

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 44why, one step back
44

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 45–48why, one step back
45The parts yielded by the render stream tend to match statement blocks in
46the template.4748

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

lines 49–51why, one step back
49Streaming with Context
50----------------------51

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 52–54why, one step back
52The :data:`~flask.request` will not be active while the generator is
53running, because the view has already returned at that point. If you try54to access ``request``, you'll get a ``RuntimeError``.

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 55why, one step back
55

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 56–58why, one step back
56If your generator function relies on data in ``request``, use the
57:func:`~flask.stream_with_context` wrapper. This will keep the request58context active during the generator.

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 59why, one step back
59

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 60–61why, one step back
60.. code-block:: python
61

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 62why, one step back
62 from flask import stream_with_context, request

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

line 63why, one step back
63 from markupsafe import escape

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

lines 64–67why, one step back
64
65 @app.route('/stream')66 def streamed_response():67 def generate():

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 68–71why, one step back
68 yield '<p>Hello '
69 yield escape(request.args['name'])70 yield '!</p>'71 return stream_with_context(generate())

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

line 72why, one step back
72

Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542

lines 73–85why, one step back
73It 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.