@login_required
def download():
    """Export a CSV of all sales data"""
purchases = Purchase.query.all()
csvfile = cStringIO.StringIO()
headers = ['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', 'coupon_used']
rows = []
for purchase in Purchase.query.all():
    pass
How to stream CSV from Flask via sqlalchemy query?