A nonexistent object was used in an `Input` of a Dash callback - Bug with dcc.store

Hi,

in a different function I return the .getlayout function if the tab is activated

The components in get_layout are added dynamically to the layout, depending on which tab you are. Some of the added components are Inputs to the callback that updates the Store, but you also have the active tab, which seems to be always present in the layout.

Now, suppose that your app starts with the tab in question. If you switch to a new tab, the components “imb_data_*” are present in the layout when Input('imbalanced_data_tabs', "active_tab") triggers the callback, so all Inputs are present in the layout and the execution is fine. However, if you come back to the tab, then these components are gone, but the active_tab remains, triggering the callback and raising the error. prevent_initial_call won’t be of any help in this case, since this is not necessarily a problem when the app starts.


The solution is relatively simple. Looking at your callback, it seems to me that you don’t need active_tab to trigger it, since you just want to check the value to know if synthetic_datastore has to be updated. The only case when it is updated is i-when the tab is active and ii-when “imb_data_tune_btn” is pressed, but note that ii can happen only if i is true (the button must be in the layout), so basically “imb_data_tune_btn” should be your only Input to this callback (based on the conditional you have on it).

There is a gotcha though… :slightly_frowning_face: Callbacks are also triggered when an Input is added to the layout (see here), so the callback will be triggered when “imb_data_tune_btn” is added to the layout. This can be solved by setting n_clicks (say, to None) in the button when it gets inserted and explicitly testing if the value is not None in the callback body. I am pretty sure triggered can’t distinguish when the button is pressed or insterted, but I might be wrong on that…

Apologies for the long explanation, I hope that you’ll find it helpful to understand the error. :slightly_smiling_face:

3 Likes