I'm trying to make a API for my machine leaning model in flask. I want it to return and show a CSV file which i will will read with javascript:

Plotly.d3.csv("http://127.0.0.1:5000/predict") 

Right now my API makes the browser download the CSV file and my JavaScript code above wont work. As I understand I want the CSV file to be shown in the browser, like this: (https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv) to make it possible to read in the data.

My code:

@app.route('/predict', methods=['GET','POST'])
def predict():

#lots of code

#export df with prediction to csv
   export_csv = df.to_csv (r'../untitled/predict_data.csv', header=True)

   with open("../untitled/predict_data.csv") as fp:
        csv = fp.read()

return Response(
        csv
        headers={"Content-disposition":
                 "attachment; filename=pred_data.csv"})

I don't really understand what the header are doing, but I guess that my header is the problem that makes the browser download the CVS. I've tried to remove the header which results in a text file being showed. It is comma separated but has no new lines.

Is there another header I can use or should I try to insert \n where a new line is supposed to be? Or how should I approach my problem?

0 Answers