python - How to stream CSV from Flask via sqlalchemy query? -
i'd stream csv flask using technique they describe here:
from flask import response @app.route('/large.csv') def generate_large_csv(): def generate(): row in iter_all_rows(): yield ','.join(row) + '\n' return response(generate(), mimetype='text/csv')
i have query sqlalchemy returns list of purchase objects. i'd write csv, ideally customization on attributes output file.
unfortunately, i'm getting blank csv output currently:
@app.route('/sales_export.csv') @login_required def sales_export(): """ export csv of sales data """ def generate(): count = 0 fieldnames = [ 'uuid', 'recipient_name', 'recipient_email', 'shipping_street_address_1', 'shipping_street_address_2', 'shipping_city', 'shipping_state', 'shipping_zip', 'purchaser_name', 'purchaser_email', 'personal_message', 'sold_at' ] i, row in enumerate(purchase.query.all()): if == 0: yield fieldnames csv = ','.join(row) + '\n' yield csv return response(generate(), mimetype='text/csv')
where going wrong?
http://flask.pocoo.org/docs/0.10/patterns/streaming/#streaming-with-context
pretty explains how keep context around
Comments
Post a Comment