Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

How to work with dynamic callbacks in Dash?

I have a Dash application where the items of a ListGroup are generated dynamically, based on the length of a certain file. The file can have different lengths. I have added the items to the list, each of them having a unique id, based on its position in the file.
Here I am appending the items from file to the list group.

output.append(dbc.ListGroupItem(row_value, id=str(row_number), n_clicks=0, action=True))

I have a callback where I need to know the length of listgroup items, but I don’t know the length of them in the Input of the callback. How can I solve this problem?
This is the dynamic callback:

    [Output("counter", "children"), Output("sparql_query", "value")],
    [Input(str(i), "n_clicks") for i in range(prefixes_count)],
    [Input(str(i), "children") for i in range(prefixes_count)],
    State('sparql_query', 'value'), prevent_initial_call=True

I need to set the prefixes_count beforehand. I was thinking of using a helper div where to store the prefixes_count value, but how can I access it in the previous callback ? I was thinking of something like this, but obviously this is not working:

range(Input('length_children_tab', 'children'))

How can I reach the same effect?

Hi lola_bunny,
Althought in the documentation prevents the use of Global variables, it seams to me that in your case it will be a right solution.
Unless exists a way to take the info from a Div store (without using a callback), that should be very helpfull, but I do not know that there is one.

See pattern matching callbacks: :slight_smile:

How can I trigger the change of the prefixes_count if I update its value in another callback function, based on the change of the ‘helper’ html div?

@app.callback(Output("helper", "children"),
              [Input('list_group', 'children'), Input('length_children_tab', 'children')])
def populate(list_group_children, length_children_tab):
    print("list_group_children", list_group_children)
    print("length_children_tab", length_children_tab)
    trigger = callback_context.triggered[0]
    global prefixes_count
    prefixes_count = trigger['value']
    print("TRIGGER iS ", trigger['value'])
    print("PREFIXES FROM TRIGGER iS ", prefixes_count)
    return "UPDATED length is  ", length_children_tab

Everytime this callback change the prefixes_count, the ‘helper’ ‘children’ also change, that means that you can trigger a callback everytime the ‘helper’ ‘children’ changed.
Hope I understood your question.

I see, but I am new to Dash and I don’t know how to achieve this :slight_smile:


I never worked with this kind of input in a callback:

    [Output("counter", "children"), Output("sparql_query", "value")],
    [Input(str(i), "n_clicks") for i in range(prefixes_count)],
    [Input(str(i), "children") for i in range(prefixes_count)],
    State('sparql_query', 'value'), prevent_initial_call=True

I answer your questions trying to follow your needs.
If you can add a simple example that can be copied and pasted perhaps I can find a solution, or explain a little bit what do you want to accomplish and why you need the lenght, perhaps could be other options.

Okay, so I uploaded my code on github: The app is, please ignore the id errors, the main application is bigger and structured over many files. I extracted only a bit of it. So basically i need to update the id of the elements of the group list, so that the number of id(list items) is equal to the number of lines from the chosen file. The files are 3 and their length differ, they are dynamically updated.

Also, whenever I click on an option from the list, it is copyed in the left side of the page.

Hi lola_bunny,

I have the answer you are looking for:


import dash_core_components as dcc





Change the second Output in the first callback for:

Output('length_children_tab', 'value')]

Add in the second Callback:

[Input('length_children_tab', 'value')],

And then the args[-2] gives the number you are looking for.

The dcc.Input ‘value’ property stores the len from the first callback and then provides it for the second.

Great, thanks! But still, how can I get the length of length_children_tab in the second callback as the length is needed here:

    [Input(str(i), "n_clicks") for i in range(prefixes_count)],
    [Input(str(i), "children") for i in range(prefixes_count)],

How can I know before how many items i have in the groupList? prefixes_count does not update its value when changing length_children_tab…

I found the solution and i updated it here if you want to take a look:
This is the application from github

import dash_html_components as html
from dash import callback_context
from dash.dependencies import Input, Output, State
import yasqe_dcc

import dash_bootstrap_components as dbc
import dash

# style to vertically split the screen in two parts
SPARQL_INPUT_STYLE = {'position': 'relative', 'width': '50%', 'float': 'left', 'height': '10%'}
PROPERTIES_STYLE = {'width': '49%', 'display': 'inline-block', 'height': '259px', 'overflow': 'scroll'}

app = dash.Dash(

# path changes, based on click on "classes", 'prefixes' and 'properties'
PATH_TO_PREFIXES_FILE = r"prefixes_tab.txt"

# needs to change based on file length
prefixes_count = len(open(PATH_TO_PREFIXES_FILE, "r").readlines())

def read_file(tab_name):
    prefixes_file = open(str(tab_name) + ".txt", "r")

    # holds prefixes read from file
    output = []
    for row_number, row_value in enumerate(prefixes_file):
        # append prefixes from file to list
        # get value without \n
        # output.append(dbc.ListGroupItem(row_value.splitlines(), id=str(row_number), n_clicks=0, action=True))
        if row_number == 0:
                dbc.ListGroupItem(row_value, id={"item": str(row_number)}, n_clicks=0, action=True, disabled=True))       
            output.append(dbc.ListGroupItem(row_value, id={"item": str(row_number)}, n_clicks=0, action=True))
    return output

set layout 
app.layout = html.Div(
    style={'fontFamily': 'Bahnschrift'},
        html.H3(children='Select text from list to view into textInput',
                style={'color': 'rgba(0,102,102,1)',
                       'textAlign': 'center'}),
        # SPARQL interpreter (left side of screen)

                    value=" "


        # WIKI tabs (right side of screen)
            (id="wiki_tabs", active_tab='prefixes_tab',
                 dbc.Tab(label='Classes', tab_id='classes_tab'),
                 dbc.Tab(label='Prefixes', tab_id='prefixes_tab'),
                 dbc.Tab(label='Properties', tab_id='properties_tab'),
             ], ),
        dbc.Button('Submit Query', id='submit', n_clicks=0, color="info",
                   className="mr-1", style={'margin': '0 auto', 'width': '150px',
                                            'textAlign': 'center',

                 style={'whiteSpace': 'pre-line', 'textAlign': 'center',
                        'color': 'rgba(0,102,102,1)'}),

        , html.Div(id='helper'),


    [Output('wiki_tabs_content', 'children'), Output('length_children_tab', 'value')],
    [Input('wiki_tabs', 'active_tab')])
def render_content(tab):
    output = read_file(tab)
    prefix_list = [dbc.ListGroup(id="list_group",
                                 output, style=PROPERTIES_STYLE
    return prefix_list, len(output)

    [Output("helper", "children"), Output("sparql_query", "value")],
    [Input({"item": ALL}, "n_clicks")],
    [Input({"item": ALL}, "children")],
    [Input('length_children_tab', 'value')],
    State('sparql_query', 'value'), prevent_initial_call=True
def func(*args):
    print('len', len(args))
    length_children_tab = args[-2]
    print("args", args)
    print("callback context ", callback_context.triggered[0])
    trigger = callback_context.triggered[0]
    selected_value = trigger["prop_id"].split(".")[0]
    selected_from_value = selected_value.split(":")[1]
    SELECTED_ID = selected_from_value.split('"')[1]
    # SELECTED_VALUE=args[1][int(SELECTED_ID)]

    if int(SELECTED_ID) == 0:
        raise dash.exceptions.PreventUpdate()
        print("selected option is ", SELECTED_ID)
        # get selected item from prefixes list based on the selected id
        # args is a list of all the ids, followed by the prefixes value
        SELECTED_VALUE = args[1][int(SELECTED_ID)]
        # sparql_query = args[len(args) - 1] + "\n" + selected_value
        # sparql query holds the query from yasqe, followed by the chosen prefix from the list
        sparql_query = args[len(args) - 1] + SELECTED_VALUE
        print("sparql query is ", sparql_query)
        print("selected option is ", )
    except ValueError:
    return SELECTED_ID, sparql_query

if __name__ == '__main__':
    app.run_server(debug=True, port=8019)

Glad to here that !!! :smiley:
chriddyp is the author of Dash, he knows a little bit more than me. :laughing: :laughing: :joy:

Everytime this callback change the prefixes_count, the ‘helper’ ‘children’ also change, that means that you can trigger a callback everytime the ‘helper’ ‘children’ changed.