Hello
I implemented this approach in an dash app:
In my case, the user upload a set of csv files, the dash app concatenate them and display the whole in a dash_table (id="data-table"
) plus several plots …
@app.callback(Output('download-button', 'href'),
[Input('data-table', 'data')])
def update_download_button(rows):
df = pd.DataFrame(rows)
csv_string = df.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8," + urllib.parse.quote(csv_string)
return csv_string
But as mentioned in this post if the file is larger than 2Mb, the download does not work on chrome.
Thus I would like to try this approach: Allowing users to download CSV on click - #9 by chriddyp
But I do not understand how it works. Looking at this:
@app.callback(Output('my-link', 'href'), [Input('my-dropdown', 'value')])
def update_link(value):
return '/dash/urlToDownload?value={}'.format(value)
@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 = StringIO.StringIO()
strIO.write('You have selected {}'.format(value))
strIO.seek(0)
return send_file(strIO,
mimetype='text/csv',
attachment_filename='downloadFile.csv',
as_attachment=True)
If I well understand, the first function returns only a string. The second, defines a url using flask server. But where is the downloadFile.csv
or where do I have to write it ? How do I chose this /dash/urlToDownload
address ?
Thanks