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

Displaying a warning message during an expensive operation

I have an expensive operation in my script and would like to warn the user that it is time to wait while my callback processes. What’s the best way to do this? I’ve tried:

@app.callback(
dash.dependencies.Output(‘grossreality’, ‘children’),
[dash.dependencies.Input(‘idwafer’, ‘value’)],
[dash.dependencies.State(‘memory’, ‘data’)]
)
def checkrealitystate(value, storedata):
if storedata is None:
storedata = {‘loading’: False}
if storedata[‘loading’]:
return ‘Please wait, loading…’
else:
return ‘Now showing:’ + str(value)

… other callback with the operation …
storedata[‘loading’] = True
checkrealitystate(wfrval, storedata)
-expensive operation-
storedata[‘loading’] = False
checkrealitystate(wfrval, storedata)

but right now at least, that isn’t working. Is there something else I have to do, maybe push the new values to the store or something? Thanks.

Hey you need a callback wit the warning to finish before the callback with the expensive operation is fired, since the warning is an output that gets sent only when the entire callback processing finishes.

You can achieve that for example by diverting your trigger (let’s say a button) to a warning callback, and have the warning callback trigger the callback of the heavy process (for example by changing the value of a hidden div).

I have something I’m using for updating the user on the different stages of a heavy process, it’s a bit sneaky but maybe it can give you some ideas - Asynchronous notifications / output console for long callbacks - what do you think about my solution?