Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Download filtered data from DataTable to csv

$
0
0

@zylatis wrote:

I would like to enable the downloading to csv of filtered data from DataTable. From this link

I can see how to get the filtered DT as a dataframe but i’m not sure the best way to pass that to a download link. It seems to me that, to ensure different users don’t mess up each other’s data, I should use a global dict with the user info as key and the filtered data s the dataframe and then that can be referred to in the download call back.

I guess the issue I am asking about is: can I do this without storing a global variable, i.e. actually pass the filtered dataframe to a function to generate the download link?

Currently I have a download link + callback which just filters based on the dates in a dropdown, but this uses URL parameters which seem unsuitable for this task

@app.callback(Output('my-link', 'href'), [Input('shipper_dropdown', 'value'), Input('date_range','start_date'), Input('date_range','end_date')])
def update_link(s,d1,d2):
	return f'/dash/urlToDownload?s={s}&d1={d1}&d2={d2}'

@app.server.route('/dash/urlToDownload') 
def download_csv():
	df, clv = data.data_list
	shipper = int(flask.request.args.get('s'))
	d1 = flask.request.args.get('d1')
	d2 = flask.request.args.get('d2')
	subset = df[(df['shipper']==shipper) & (df['create_date']<=d2) & (df['create_date']>=d1) ]

	s = io.StringIO()
	subset.to_csv(s)
	
	return flask.Response(
        s.getvalue(),
        mimetype="text/csv",
        headers={"Content-disposition":
                 f"attachment; filename=cid-{shipper}-from-{d1}-to-{d2}.csv"})

Any tips would be appreciated!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles