python – Data frame not being passed through flask sessions

Question:

The error is that in the final CSV file, the file opens only headers and everything is merged without separators and there is simply no data!

I have different data frames generated in 1 @app.route . I need to pass them to another @app.route for download.

@app.route('/dataupload_2', methods=['GET', 'POST'])
def dataupload_2():
    if request.method == 'POST' and 'csv_data' in request.files:
        file = request.files['csv_data']
        filename = secure_filename(file.filename)
        #преобразования с датафрейм pandas функциями
        session["data_3"] = res_new_1.to_json()
        #передача датафрейма

@app.route('/download', methods=['GET', 'POST'])
def getPlotCSV():
    dat = session.get('data_3')
    dat = pd.read_json(dat)
    return Response(
        dat,
        mimetype="text/csv",
        headers={"Content-disposition": "attachment; filename=myplot.csv"})

Help to make the data frame open in CSV completely and normally? ..

Answer:

you forgot to convert DataFrame to CSV file when calling Response() :

@app.route('/download', methods=['GET', 'POST'])
def getPlotCSV():
    dat = session.get('data_3')
    dat = pd.read_json(dat)
    return Response(
        dat.to_csv(index=False),    # <------------------- NOTE !!!
        mimetype="text/csv",
        headers={"Content-disposition":
                 "attachment; filename=myplot.csv"})
Scroll to Top