writer = csv.DictWriter(csvfile, headers)
writer.writeheader()
for row in rows:
    writer.writerow(dict((k, v.encode('utf-8') if type(v) is str else v
        ) for k, v in row.items()))
csvfile.seek(0)
return send_file(csvfile, attachment_filename='sales_export.csv',
    as_attachment=True)
How to stream CSV from Flask via sqlalchemy query?