I have a Pandas dataframe in my Flask app that I want to return as a CSV file.
return Response(df.to_csv())
The problem is that the output
set the content-disposition and use stringIO to convert dataframe to stream, below is the code to achieve,
execel_file = StringIO.StringIO()
filename = "%s.csv" % ('output file')
df.to_csv(execel_file, encoding='utf-8')
csv_output = execel_file.getvalue()
execel_file.close()
resp = make_response(csv_output)
resp.headers["Content-Disposition"] = ("attachment; filename=%s" % filename)
resp.headers["Content-Type"] = "text/csv"
return resp