Hi,
I’m trying to download a dataframe as a csv, where I try to do as discussed in this topic; Allowing users to download CSV on click - #7 by kfyao
My problem is, that it writes all the data in the first row and is not recognizing any line breakers.
I send the dataframe as csv to the href link
@app.callback(Output('my-link', 'href'),
[Input('download-button', 'n_clicks')])
def update_link(clicks):
global df
return '/dash/urlToDownload?value={}'.format(df.to_csv(index=False, sep=',',
encoding='utf-8'))
My route looks like this;
@app.server.route('/dash/urlToDownload')
def download_csv():
value = flask.request.args.get('value')
# create a dynamic csv or file here using `StringIO`
# (instead of writing to the file system)
strIO = io.StringIO()
strIO.write(value)
writer = csv.writer(strIO, delimiter=',', quotechar='|')
writer.writerow(value)
mem = io.BytesIO()
mem.write(strIO.getvalue().encode('utf-8'))
mem.seek(0)
strIO.close()
return flask.send_file(mem,
mimetype='text/csv',
attachment_filename='downloadFile.csv',
as_attachment=True)
An example of what value is in this case;
version,id,value1,2,3
where the expected csv should look like this;
version id value
1 2 3