Callback that waits until other callbacks are executed

Hi Everyone,

Is there a way to make a callback to wait until another callback is executed? And what’s the more efficient and straightforward way?

Here is my problem:

I need to execute a callback to run a query, so this takes time and all my other dash components depends on the data that I’m pulling from the database so I’m getting a bunch of errors due to this.

I’m looking for examples to make this interaction happens

Hi,

maybe background callbacks?

Does this stop the other callbacks to be executed while looking for the data?

Take a look at the 2nd example. If you can disable a button while the background callback is running, it must be possible to prevent other callbacks to be fired. I’m not 100% sure, though.

1 Like

Hello @seventy77,

What you can do, is in the background callback, alter a prop that you reference its state in all the other callbacks.

Something like this:

@app.callback(
Output..., Inputs...
running=[
        (Output("button_id", "disabled"), True, False),
    ],
background=True
)
def longQuery(inputs):
    return outputs

### dependent callbacks
@app.callback(
Outputs..., Inputs...,
State('button_id', 'disabled')
)
def dependentCallback(inputs..., s):
     if s:
         return 'Sorry, we are updating info, please wait...'
     ### rest of code
     return output

And then if you want to trigger the other callbacks immediately after, just convert the state into an input. :slight_smile: However, it would immediately fire when you start the background callback, so up to you.

1 Like

Hi Jinnyzor,

Hope you are doing ok :),

I have a few questions regarding your solution, so there is the callback that receives the inputs from the user and runs the longquery to pull the data that’s clear for me. But my question is regarding the other callbacks because imagine that there are dd menus that take columns from the data that I’m pulling so still not clear to me how if I remove the state attribute from the callback the callbacks won’t be fired.

Hello,

Yes, I am doing well.

I’m confused with what you mean by remove the state. You mean turn it into an input?

1 Like

I was able to implement this method but I got a partial solution, some of the callbacks are still failing due to the missing values. All the dd menus of my DashBoard are working as expected with this solution. I’m having issues with the callbacks that do the postprocessing and store the data in dcc.store components because they got fired even if I do the disable trick and I end up having null data because the user has not input any data. Any idea on this?

Hi, are these callbacks getting fired at startup or do you prevent this by using prevent_initial_call=True?

Here is what I have, I think that I have a miss concept problem:

These callbacks are getting fired at the start-up correct.

I want that everything starts disabled except for the user inputs once the user inputs the data and clicks submit, the submit option should be disabled and the other callbacks should wait for this callback to end.


@dash.callback(
    output=[Output('base-df', 'data'), # Output that updates the dcc.store component with the data that I pull from the db
    
    Output('lt_in', 'children'), # Output that just show the values that the user inputs for the query
    
    Output('oper_in', 'children')],# Output that just show the values that the user inputs for the query
    
    inputs=[Input('SubmitB', 'n_clicks'), # Submit Button
	State('lots-input', 'value'), # Query value
	State('oper-input', 'value')], # Query value
	background=True,
	running=[
    	    #Dcc Store that stores Data From DB 
	        (Output("base-df", "disabled"),True, False),
	        #Dcc Store that stores Cleaned Data ** The callback that cleans this is being fired
	        (Output("base-df-2", "disabled"),True, False),
            # Submit Botom
			(Output("SubmitB", "disabled"), True, False),
	        #Dcc Store that stores filtered Data based on user dd menus selection ** The callback that filtered this is being fired
			(Output("filtered-df", "disabled"), True, False),
			#DD Menu
			(Output("p-picker", "disabled"), True, False),
			#DD Menu 
			(Output("oper-picker", "disabled"), True, False),
			#DD Menu 
			(Output("d-picker", "disabled"), True, False),
			#DD Menu 
			(Output("m-picker", "disabled"), True, False),
			#DD Menu
			(Output("graph", "disabled"), True, False),
	],
	prevent_initial_call=True
)
def run_query(n_clicks, lots, oper):
#This is fired once I click submit, so here is my problem I want that everything starts in a disabled mode except for the user inputs. Once the user enters the inputs and click submit this callback needs to start running and the other callbacks should be disabled
    if n_clicks>0:
      df_hbase=us.getquerydata(lt,oper,'Queries/querietest.sql')
      base_json=df_hbase.to_json(date_format='iso', orient='split')
      return base_json, lots, oper
    else:
      raise PreventUpdate

All my problems were fixed with:
if data is None:
return dash.no_update

Nice, glad you figured it out!

1 Like