Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Stop callback when triggered indirectly

Guys,

I have the following vivious cirle and wanted to consult with you as maybe you will know good solution for that. Namely, how to stop callback if it’s triggered indirectly?

A bit of background:

  • In my app I have a checkbox (toggle button)
  • Its status is set when loading the page: if there is record in database then ON, if no record then OFF
  • Toggle has its own callback: if user switched toggle to ON then add a record, if switched to OFF then remove the record

So what happens it that when page is loaded: code is checking in the database if record exists > yes, so switch toggle to ON > because it was switched to ON add record to the database. Sam happens for OFF, but with delete record effect.

I want to avoid unnecessary traffic on db, so wanted to check if there is any way to break such relationship? Basically initial setting of the checkbox should stop callback from being triggered.

Any solutions?

Hi,

You can pass prevent_initial_call=True to the callback, as explained here.

Hi,

It seems not to be working for me as initial state of checkbox is set via callback:

@app.callback(
Output(‘intermediate_team_data’, ‘children’),
Output(‘intermediate_budget_data’, ‘children’),
Output(‘intermediate_job_data’, ‘children’),
Output(‘intermediate_artificial_div’, ‘children’),
Output(‘eom_process_switch’, ‘value’), #here’s our checkbox
Input(‘period_slider’, ‘value’),
State(‘intermediate_period_data’, ‘children’)
)

So it comes down to “when user changes checkbox fire underlying callback, but when checkbox is set programmatically (at the loading of the page) then do nothing”.

So it comes down to “when user changes checkbox fire underlying callback, but when checkbox is set programmatically (at the loading of the page) then do nothing”.

Ok, for that you can use the callback_context as explained here. These two events should be identifiable from the context, but it is hard to say for sure without a MWE with the two callbacks and the initial layout.

Nearly, but not that easy.

Just checked your idea. Initially I thought this is exactly what I was looking for, but failed a minute after. Context tells you which input fired event, but not how this input was generated (via callback or via user). In other words dash.callback_context does not store full stack of inputs, but basically just the one that triggered your callback.

I think the only way to solve it is to have hidden div that would change value when page is loaded (and checkbox status is set for the very first time). Then every other instance of a given callback would check the div and modify behavior accordingly. This would make it possible to have different behavior if callback is run for the very first time.

Got it, sometimes this happen, depending on how the callbacks are structured…

As a last observation, you can use a dcc.Store component instead of a hidden div for your solution. It is more flexible and elegant.