Sometimes, we want to download a csv file on clicking a button with Python Flask.
In this article, we’ll look at how to download a csv file on clicking a button with Python Flask.
How to download a csv file on clicking a button with Python Flask?
To download a csv file on clicking a button with Python Flask, we can create a route that returns the csv file.
For instance, we write
from flask import send_file
@app.route("/get-plot-csv")
def plot_csv():
return send_file(
"outputs/Adjacency.csv",
mimetype="text/csv",
attachment_filename="Adjacency.csv",
as_attachment=True,
)
to call send_file
with the file path, mimetype
, attachment_filename
, and as_attachment
set to True
to return a csv file response.
We set the MIME type to 'text/csv'
and the download file’s filename is Adjacency.csv.
Conclusion
To download a csv file on clicking a button with Python Flask, we can create a route that returns the csv file.