Use Flask to convert a Pandas dataframe to CSV and serve a download

This is pretty much the same solution but you can just pass the same info into Response:

return Response(
       df.to_csv(),
       mimetype="text/csv",
       headers={"Content-disposition":
       "attachment; filename=filename.csv"})

Set the Content-Disposition to tell the browser to download the file instead of showing its content on the page.

resp = make_response(df.to_csv())
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp