Update streaming.rst

Code example was incorrect.
This commit is contained in:
François Rejeté 2022-08-15 13:38:39 +09:00 committed by GitHub
parent 36af821edf
commit e876185c8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 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:: data and to then invoke that function and pass it to a response object::
.. code-block:: python
@app.route('/large.csv') @app.route('/large.csv')
def generate_large_csv(): def generate_large_csv():
def generate(): def generate():
for row in iter_all_rows(): for row in iter_all_rows():
yield f"{','.join(row)}\n" 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 Each ``yield`` expression is directly sent to the browser. Note though
that some WSGI middlewares might break streaming, so be careful there in that some WSGI middlewares might break streaming, so be careful there in