Solution 1 :

Well, I don’t know how your to_csv() is written, but it should look something like:


def to_csv ():
    path = "/Examples.csv"
    return send_file(path, as_attachment=True)

Notice send_file(path, as_attachment=True)

That way, you won’t present the data to the browser, but will rather notify the browser to download the file as attachment.

Problem :

I am am creating a web app with Python and Flask. My python code outputs a pandas dataframe, which I would like the user to download by the click of a button. More specifically, I want the user to click the download button and I want the csv file to be displayed as a download on the bottom download bar in Google Chrome (Something like this picture.).

Here’s what I did so far:

HTML button:

 <a class="btn btn-primary" onclick="csvDownload()" href=""  type="button">Download</a>

Which leads to this JavaScript function:

 function csvDownload() {
        location.replace('/download')
    }

In Flask, I process it like so:

@app.route('/download')
def download():
   backend.final_data.to_csv()

Here, backend.final_data is my data frame coming from another python file called backend.

Comments

Comment posted by help1

this may help you

Comment posted by DinoMan

thanks a lot, help1 solved my problem

Comment posted by DinoMan

yes it does @sahasrara62

Comment posted by sahasrara62

if solution give by @mutantkeyboard solved your problem also you can accept it

By