Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬 Register for the Oct 27 webinar.

How can i have dash.callback_context triggered only for the button click in a tab and not when the tab itself is selected?

I am quite new to programming and to plotly dash as well. I have some tabs in my dashboard and in each tab among other things i have a text input box (int) and a button that triggers some calculations using the input.

With the following script, when I select the tab, the callback is triggered and the calculation already starts with the empty value in the text, even before I click the button. I have an if clause to return nothing when the text input box is empty. But that doesn’t seem to be working. It directly goes to the final “else” clause. How can i solve this? Thank you!

@app.callback(Output('PRINTOUTPUT', 'children'),
              [Input('create_button', 'n_clicks'),
              Input('selection_tabs', 'value')],
              [State('text-input', 'value')])

def xyz (clicks, selected_tab, textinput):    
    
    changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0] #To determine if n_clicks is changed. 

      
    if 'create_button' not in changed_id:
        return ""

    elif 'create_button' in changed_id:           
        if textinput =="":  #Do nothing if button is clicked and input num is blank.            
            return "No input"
        
        else: 
            #Do some calculations based on the selected tab and input
             return "Successful"

Hi @Arr0w and welcome to the Dash community!

It looks like you are on the right track - here are a few things to try:

  • Using print statements in the callbacks so you can see the variables can make debugging easier.

  • When the app starts, the default value for dcc.Input is None (it’s not “” unless you set it to that value) So that final if statement needs to be: if text-input is None: rather than if text-input == "":

  • I’m not sure about how you define changed_id, but this works for identifying which callback triggered the callback: ( See more info here: https://dash.plotly.com/advanced-callbacks)

ctx = dash.callback_context
changed_id = ctx.triggered[0]['prop_id'].split('.')[0]

I hope this helps!