Right now I have a flask app in which part of the functionality allows me to select a date range and see data from a sql database from that selected date range. I then can click a button and it exports this to a csv file which is just saved in the flask project directory. I want the user to be able to download this csv file. I want to know what the best practice for a user to download a dynamic csv file. Should I send_file() and then delete the file after user has downloaded since this data shouldn't be saved and the user won't be using that file again. Should the file be saved to the database and then deleted out of the db? Or can I just keep it within the flask directory? Please provide insight if possible, thank you so much.
Asked
Active
Viewed 6,953 times
1
-
2No need to actually write the CSV to disk anywhere - does this help? https://stackoverflow.com/a/26998089/1073696 – brunns May 06 '19 at 15:00
2 Answers
4
@brunns pointed it in very right direction.
You don't have to save the file in your database or in your file structure or anywhere. It will get created in memory on user request.
I've done this with django for pdf
and for csv
files it'll work in the same way with flask too. Basics are all same.
for python3
use io.StringIO
, for python2
use StringIO
from io import StringIO
import csv
from flask import make_response
@app.route('/download')
def post(self):
si = StringIO.StringIO()
cw = csv.writer(si)
cw.writerows(csvList)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=export.csv"
output.headers["Content-type"] = "text/csv"
return output
Courtesy: vectorfrog

xxbinxx
- 1,527
- 11
- 18
-
This directly downloads the file, is there a way to provide a text "here is your download link, and give a link , which on click downloads – Quantum Dreamer Jul 16 '19 at 18:49
-
@JohnRuby yes, save the csv to a folder in your system. Let's say in `
/static/tmp/ – xxbinxx Jul 17 '19 at 06:05-mycsv.csv` and then give your user a url to this file saying "this link is only valid for 2 days". Write a cron script that regularly do a cleanup task for this folder. Files older than 2 days get's deleted. -
I am using this for serving reports through python. I think saving it to project directory will overload the server. – Quantum Dreamer Jul 18 '19 at 18:18
-
well then save it somewhere else like some cloud storage AWS S3 etc but that how it's done. Think about it, if you don't keep the file how will the user download. Or you make a temporary link which understands which file to generate on realtime. – xxbinxx Jul 19 '19 at 04:58
-
-
0
Based on @xxbinxx's answer, used with pandas
from io import StringIO
import csv
from flask import make_response
@app.route('/download')
def download_csv(self, df: pd.DataFrame):
si = StringIO()
cw = csv.writer(si)
cw.writerows(df.columns.tolist())
cw.writerows(df.values.tolist())
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=export.csv"
output.headers["Content-type"] = "text/csv"
return output

David Mendes
- 197
- 2
- 4