Conditional expensive callbacks

I have a situation where I have want to separate two ways of updating the same figure. The reason for that is that one of the ways has expensive parameters to pass to the dash server, so based on a specific condition, I would like the app to call different callbacks based on a simple condition.

How can I achieve making a callback that checks a condition and based on the result, it will call one of other two callbacks that update a figure without passing the expensive parameters before the final callback is chosen?

Could you solve this by just using one callback?

@app.callback()
def(args):
    if args not expensive:
        out = function_1()
    else:
        out = function_2()
    return out

I don’t think so.

Both callbacks take different arguments. One is expensive (we are talking children of a row with figures and data in it) and the other is cheap to pass along. So in the case where the callback does not need all the children, the expensive argument would still be passed anyway and not used. So ideally, the row_children would never get passed unless necessary.

Why dont you just make two separate callbacks?

In outline, add two dcc.Store() items to your layout, say with ids ‘s-expensive-trigger’ and ‘s-cheap-trigger’.

Then have three callbacks:

1 - triggered by the original event. Checks if an expensive operation is needed. If it is update the data of s-expensive-trigger, and if not update s-cheap-trigger.

2 - triggered by a change to the contents of s-expensive-trigger, that performs the expensive operation

3 - triggered by a change to the contents of s-cheap-trigger, that performs the cheap operation

(And it would be good for the first one, at least, to be clientside if you can)

2 Likes

Because the callback in question takes dcc.Store as an Input. Basically, I need it to decide on the expensive and cheap callback based on the values in specific keys of that dcc.Store. I need the check to happen before passing the expensive arguments to a callback.

That’s what I needed! Thanks

1 Like

But do you need the data of the store itself to determine whether it is a cheap or expensive call? If so, then it doesn’t really work, as you would have to send the data to the server anyway. That would only work if the check can be done clientside.