Currently I am storing the data uploaded in a dcc.Store component using the folowing code
@du.callback(
output=Output('store-data', 'data'),
id='upload-data',
)
def store_data(filenames):
data1 = pd.read_excel(filenames[0])
return data1.to_dict('records')
and then I am using this data in other callbacks by simply reading the data using pandas -
@app.callback(
Output('output-datatable', 'children'),
Input('store-data','data'),
prevent_initial_call = True
)
def mwe(data):
df = pd.DataFrame(data)
and then perform whatever I want to do on this df dataframe and return a final result to be stored in the āoutput-datatableā and similarly I am updating dropdowns based on which graphs would be plotted using the following code snippets for reference -
@app.callback(
Output("xaxis-data","options"),
Input("store-data", "data")
)
def update_dropdown(data):
df = pd.DataFrame(data)
return [{'label':x, 'value':x} for x in df.columns.unique()]
@app.callback(
Output("yaxis-data","options"),
Input("store-data", "data")
)
def update_dropdown(data):
df = pd.DataFrame(data)
return [{'label':x, 'value':x} for x in df.columns.unique()]
@app.callback(Output('output-graph', 'children'),
Input('submit-button','n_clicks'),
State('store-data','data'),
State('xaxis-data','value'),
State('yaxis-data', 'value'))
def make_graphs(n, data, x_data, y_data):
df = pd.DataFrame(data)
if n is None:
return no_update
else:
bar_fig = px.bar(df, x=x_data, y=y_data)
# print(data)
return dcc.Graph(figure=bar_fig)
The problem is for the first time it does work but after I switch tabs and I come back to the Tab the dash uploader is in, I can see that the data is gone as shown by the progress bar which now reads āUpload dataā instead of āUpload completedā which it was earlier showing. Also all the dropdowns are empty. Although I have tried using persistence in the dropdowns and dash tables it is still happening and I think that is because the original source of data is not there after switching tabs. It would be great if you can provide your inputs on this.