This one was not addressed in the Sharing Data Between Callbacks video - a minor oversight in a great video,
And it is defined, but not discussed, in the documentation:
clear_data
( boolean ; default False
): Set to True to remove the data contained in data_key
.
But it has become important to my attempts to resolve a major issue, notably that the previous session’s data remains cached somewhere in my browser or app, making it impossible to analyze new data.
I spent hours experimenting with gc.collect, clearing lists, deleting variables, closing files, etc…and now I am trying to clear this data by using dcc.Store to contain it and then change the clear_value parameter to True when the Reset button is clicked. The code below - my latest attempt - is not successful.
May I ask, what is wrong with it? Or is my approach misguided here?
Do you know of a solution to this kind of problem?
Here is the code for dcc.Store at the tail end of an Accordion component:
dcc.Store(id='store-data', data=[], storage_type='memory', clear_data=False) # 'local' or 'session'
]) # Later use callback to chang clear_data to True when final analysis is completed.
Here is the callback in question:
@callback(
Output('store-data', 'clear_data'),
[Input("recipe-textarea-reset-button", "n_clicks")],
prevent_initial_call=True
)
def clear_memory(n_clicks):
if n_clicks > 0:
return True
Thank you,
Robert
If the intent of your callback is to clear the data in your store-data
dcc.Store, I would do the following:
@callback(
Output('store-data', 'data'),
[Input("recipe-textarea-reset-button", "n_clicks")],
prevent_initial_call=True
)
def clear_memory(n_clicks):
if n_clicks > 0:
return []
I am a little perplexed on what the data_key
property is used for. In my usage, I set the storage_type
to session
. I would assume the above callback would work if set to memory
Thank you for your response.
I was also perplexed by the documentation on the data_key.
I have only been writing in Dash for a few weeks, and coding for about eight months; but I ultimately decided that documentation needs editing. It seems to imply there is a property called ‘data_key’ like there is a property called ‘clear_data’. But now I think it just refers to the key “data”, where the data is stored. I think you’ve written a much better callback to control that action, and I am going to try it out now.
Thank you again.
Robert
This was the initial reaction to a cut-and-paste job.
I’ve never heard of `dash.callback_context’ and need to look it up:
In the callback for output(s):
store-data.data
Output 0 (store-data.data) is already in use.
Any given output can only have one callback that sets it.
To resolve this situation, try combining these into
one callback function, distinguishing the trigger
by using `dash.callback_context` if necessary.
Without my morning coffee, I am a little fuzzy, but I think it means 1) you wrote the right callback, but its 2) conflicting with another callback pasted below. I need to wake up and experiment with it.
Do you think it mean there is a redundancy and the “data” from the callback below is already stored in dcc.Store and I don’t need another one to control it? That I just need to incorporate the clear_data property into the callback/function below?
Like I said, I need to wake up!
If you are interested (I don’t want to impose), here is only other callback that uses data:
def create_nutritional_profile(n_clicks, data):
if n_clicks > 0:
finalize_ing_dicts(data)
create_match_idslist()
create_nutrient_dicts()
merge_nutrient_to_ing_dicts()
results = final_conversions()
for each_dict in results:
for k,v in each_dict.items():
print(k, v)
df = pd.read_csv('final_charts_data.csv')
# round numbers to two decimal places
dff = df.round(decimals = 2)
# supress/hide certain columns
dfff = dff.drop(['Gender', 'Ages', 'Benchmarks'], axis=1)
# unique values only
final_data = dfff.drop_duplicates(subset=['Nutrients'])
return dbc.Table.from_dataframe(final_data, id='review-final-analysis-datatable',
class_name="me-2",
striped=True,
bordered=True,
hover=True,
style={"width": "1200px"} )
Thanks again,
Robert
That error means you have another callback that sets store-data.data
parameter. Dash only allows one callback to set the output of a component’s parameter. So in your case, you have a callback that stores the results in store-data.data
…that same function must also be used to clear the data.
1 Like