mercredi 17 juillet 2019

Converting pandas dataframe to csv file and download in flask (in memory)

I am trying to create a CSV file from a panda data frame, to then be downloaded by the client. This is using flask, python, and html. I don't want to save the file to the directory as it will be deployed to aws in the end. The data frame is created inside another python file, NOT the flask app. 1. df converted to a csv file in memory from a python file 2. user clicks the button to download a file from HTML 3. file downloads(?)

I have tried to create a CSV file and pass it back to the flask app but not sure where to go from here. I have tried to create another route in the flask but I still am unsure of where to go.

'''python - returns df to flask app

return df

'''

return the data frame to the flask app

flask app - responseFile = df
`@app.route("/", methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
responseFile,uptime,lastConnected,totalDispense,dispenseByDay,twoWeeksCons,numErrors = controller.QueryTable(text)
    csvData = buildCSV(responseFile)
    else:
        return render_template("result.html")

`

build CSV in the same file as flask app

def buildCSV(dataframe):
    csvData = dataframe.to_csv(index = False, encoding = "utf-8")
    # csvData = "data:text/csv;charset = utf-8," + quote(csvData)
    return csvData`

'''

'''html `

Click `

'''

initially, the df is returned to the flask app and 'result.html' is rendered. A button is on that page and once clicked, will begin to download the CSV version of the df. However, now it says 'method not allowed'. Please help! The CSV must NOT be saved to the directory, but an 'on the fly' type

Aucun commentaire:

Enregistrer un commentaire