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

Best approach to accomodating multiple callbacks for an output

As far as I am aware it is still not possible to have multiple callbacks with the same output, but getting around this limitation means creating large callback with all the various inputs that might change a given output, which implies a lot of extra data potentially coming across this wire.

So, my questions are: (a) is there planned support for multiple callbacks with the same output in the near future? (b) if not, are there any recommended tips, tricks, best practices for getting around this? I have some ideas, but don’t want to re-invent the wheel if others have already found good ways to deal with this.

Thanks!

Hi @jauerb, yes this is something that I’ve also been quite frustrated about, but there is a good reason for this :-). If you had several callbacks that could write to the same prop and that had overlapping inputs, then which one should you trigger?

Therefore you need to have a single callback for a given output prop. However, here are a couple of tips:

  • you can use dash.callback_context to know which Input triggered the callback, as in the example in https://dash.plot.ly/faqs (How do I determine which Input has changed).
  • You can write the callback as a router calling different functions depending on which input changed, which is not that different from having several callbacks. For example
ctx = dash.callback_context
if not ctx.triggered:
    button_id = 'No clicks yet'
else:
    button_id = ctx.triggered[0]['prop_id'].split('.')[0]

if button_id == 'button1':
    return button1_updated(...)
elif button_id == 'button2':
    return button2_updated(...)
...
3 Likes

Thanks for this response. This is similar to what I was thinking about implementing. I will give it a shot and see how it goes.

After digging into this some more, I just want to mention that something I found crucial for this type of solution was to use dash.no_update to only update a subset of possible outputs from a callback.

1 Like