Black Lives Matter. Please consider donating to Black Girls Code today.

Download filtered data from DataTable to csv

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}'

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()
	return flask.Response(
                 f"attachment; filename=cid-{shipper}-from-{d1}-to-{d2}.csv"})

Any tips would be appreciated!

I am facing the similar issue. Have you solved it ?

Can you please share the code snippet where you are generating the datatable ?
I am facing similar issue so request you to share the code where the datatable is being generated.