kanpisek sasuk
kanpisek sasuk

Reputation: 57

How to pass pandas dataframe to button in web for download file(.csv or .xlsx) with flask, Python 2.7

My web application has visualization function for user see chart(Average, Standard Deviation) by from ...day to ...day(Start Date to End Date), where(Production line) then request to SQL for connect database. This is visualization form. :

enter image description here

After request of user my web app will show about 31 charts and I add 3 buttons for download file every chart in web look like this. (example. number 1,2 of chart) :

enter image description here enter image description here

And this is my javascript for create chart and , etc in file .html (spc_chart.html) but don't yet pass dataframe to .html:

enter image description here

I have all pandas dataframe of Avg Chart Data, Std Chart Data, Raw Data(use calculate for Avg,Std) in 3 arrays(name is: ChartDataAvg, ChartDataStd, RawData).

I just know that how to download file must will add href="/..." at each of button and set @app.route('/...') in python then send SQL to each def of @app.route('/...') and pass dataframe to web after calculated. ref(Flask: Download a csv file on clicking a button) But I don't know how to pass SQL request of user from def in another @app.route(/...).

Is there any way to pass all pandas dataframe to button for download file in each of the chart 1, 2, 3,... or send SQL to def in another app.route???

Upvotes: 0

Views: 2959

Answers (2)

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

Here's how I solved for this:

@app.route('/download-data/')
def download_data():
    """Download the data to the user as a csv..."""

    # Retrieve the data from  user's request
    data = cache.get("my_data")

    if not data:
        return jsonify({
            "ERROR": "data not found."
        })

    # -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
    
    # Load from the format it was saved in
    df = pd.DataFrame.from_records(data)

    csv_data = df.to_csv(index=False, encoding='utf-8')

    response = Response(csv_data,mimetype='text/csv')
    response.headers.set("Content-Disposition", "attachment", filename="data.csv")

    return response

Upvotes: 0

A. Ner
A. Ner

Reputation: 71

You can first make your dataframe in a csv format with the following function:

def build_csv_data(dataframe):
    csv_data = dataframe.to_csv(index=False, encoding='utf-8')
    csv_data = "data:text/csv;charset=utf-8," + quote(csv_data)
    return csv_data

Then create a callback function for your download link as follows:

@app.callback(Output('download_link', 'href'), [<your dataframe input variables>])
    def callback_build_csv_data(dataframe):           
        return build_csv_data(dataframe)

Finally in your page layout add the download link as follows:

html.A('Download Data',
        id='download_link',
        download="rawdata.csv",
        href="",
        target="_blank"
        ),

Hope this is useful.

Upvotes: 3

Related Questions