This is the first time I'm immersed in Flask, and I'm having a little problem. I have a page with a form and a bunch of flags. When submitting the form, I take the values from all the checkboxes and passing them to a script (which I already had), which basically writes the CSV file.
What I'm doing is that when I submit the CSV form, the file is created in the background and sent right back to the user. I created this part by creating a script file in memory ( using StringIO ) and then returning it using Flask send_file .
I would also like to give the user some feedback after he uploads the file, highlighting the message in the template (you may ask why I want to notify the user if he already uploaded the file - I just want to give him additional information). However, after my view function returns send_file and presents the download dialog in the browser, the page does not reload, so the flash message does not go through.
I am struggling with this: how can I return a file and also show a message to the user? I understand that each request can have only one answer, so if I use my only chance with downloading a file, I may need a different strategy. Any ideas?
This is what my “download route” looks like:
@app.route('/process', methods=["POST"])
def process():
error = None
if request.method == 'POST':
fields = request.form.getlist("field")
csv = generate_csv()
if len(fields) != 0:
csv = amxml2csv.xml2csv(xml, *fields)
flash("Extraction succeeded!")
return send_file(data, attachment_filename="newresults.csv", as_attachment=True)
else:
error = "No fields selected!"
return render_template("index.html", error=error)