Inputs in Chained Call backs

Is it possible to give inputs to a callback function which is inside another callback? In the below example I need to process each of the dict_item['Name'] which is getting passed from the outer call back function in a for loop.

# Outer Call back 
@app.callback(dash.dependencies.Output('dummydiv','children'), # Dummy Output
              [dash.dependencies.Input('interval1', 'n_intervals')], # Interal Triger 
              [dash.dependencies.Input("table", "data")]) # dcc.Store which stores values


def use_table(n, data): 
    print(type(data))
    print("Outer Called")
    
    if data:
        for dict_item in data:
             
            @app.callback(dash.dependencies.Output('lable1','children'), #Output to print the values in a llop
                          dash.dependencies.Input(dict_item['Name'])
                          )
             
            def printing(s_name):
                
                sn = s_name
                print(sn)
                
                return sn  # Return "Name to Print" to id "Lable1"
                 
        return ""  #Dummy String  - Print Nothing to id "dummydiv"
    return dash.no_update

Unfortunately, I am not able to pass the input parameter to the inner call back function. It returns the below error.


dash.dependencies.Input(dict_item['Name])
TypeError: __init__() missing 1 required positional argument: 'component_property'

How do I pass the variable from the outer call back to the inner call back? Or is there any other possible way to implement this logic?

Hi @Sumed

I think it is not possible to have one callback inside onother. :thinking:

But you can do it in a different way, build the entire children in the callback and return it into a dcc.Div():

@app.callback(Output('any_div','children'), # any dcc.Div()
              [dash.dependencies.Input('interval1', 'n_intervals')], # Interal Triger 
              [dash.dependencies.Input("table", "data")]) # dcc.Store which stores values
def use_table(n, data): 
    print(type(data))
    print("Outer Called")
    
    if data:
        div_children = ""
        for dict_item in data:
              div_children = div_children + "html.Label("+dict_item['Name']+"),"

                
        return div_children

Hello,

Thanks for your support, I tried with your solution, it creates UI component which appends the values in dict_item['Name'] as it loops along.

But my actual requirement is to replace the value that will be printed to the component having id = lablel1 as it loops through dict_item['Name'].

Sorry I do not understand your explanation :woozy_face:

What kind of component has the id “label1” (I thought it was a html.Label)
What information do you want to show and why :thinking:

oh, I am very sorry for not explaining it clearly.

lable1 is just an header component. Like this html.H1(id='label1', children='').

I have a table which stores certain data, in which one of the columns is Name. I just want to loop through the data store and show the names in the output component having id lable1. Of course this repeats after certain interval of time hence using the Input component interval1.

But you want to display all the names that exist in the rows of the dict_item[‘Name’] to lable1, or do you want to display one name row each time the interval1 change?

It’s not clear the information you want to show to the user. :thinking:

I do not want to display all the names that exist in the rows of the table at once or as a list, instead I need to display one name at a time. Assume that that column “Name” consists of 5 names, So after say 1 minute I need to loop through and display each of the 5 names(Display only one name at a given time).

Ok.

In this case use the “n_interval” property of the interval1 as index of the row you want to display.

n_interval start in 0 and adds 1 each time it trigger the callback.

Then select the row of the column using the index [n] that represents the variable that has the n_interval number.