From e876185c8e0eddfade6f1b9f20cfd957b750cdc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Rejet=C3=A9?= Date: Mon, 15 Aug 2022 13:38:39 +0900 Subject: [PATCH] Update streaming.rst Code example was incorrect. --- docs/patterns/streaming.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/patterns/streaming.rst b/docs/patterns/streaming.rst index e35ac4ab..adeaf3a0 100644 --- a/docs/patterns/streaming.rst +++ b/docs/patterns/streaming.rst @@ -15,12 +15,14 @@ This is a basic view function that generates a lot of CSV data on the fly. The trick is to have an inner function that uses a generator to generate data and to then invoke that function and pass it to a response object:: +.. code-block:: python + @app.route('/large.csv') def generate_large_csv(): def generate(): for row in iter_all_rows(): yield f"{','.join(row)}\n" - return generate(), {"Content-Type": "text/csv") + return app.response_class(generate(), mimetype="text/csv") Each ``yield`` expression is directly sent to the browser. Note though that some WSGI middlewares might break streaming, so be careful there in