nwbuhq
Last Updated: February 25, 2016
·
5.249K
· monkee_magic

Ouput a CSV of a Pandas Dataframe in Flask without tempfile

Looked for this damn thing everywhere. Was not keen on writing a tempfile!

def serve_csv(filename):
    dataframe = filtered_dataframe(filename)
    buffer = StringIO()
    dataframe.to_csv(buffer,encoding='utf-8')
    buffer.seek(0)
    return send_file(buffer,
                 attachment_filename="test.csv",
                 mimetype='text/csv')