Get list of all layout component ids (from multi-pages app and single page apps)

Hi, I currently can’t find a method or an attribute that return all component ids of app.layout (nested id included also). It would be nice if someone can help me.

I would like a return like a list or dictionary of some sorts.

Example: ['button_1','button_2','input_1',.....]

Hello @DeKhaos,

What you can do is something like this:

elements = []
for pg in dash.page_registry:
    data = dash.page_registry[pg]['layout']
    try:
        for i in data:
            elements.append(i)
    except:
        pass


data = dash.get_app().layout

try:
    for i in data:
        elements.append(i)
except:
    pass

print(elements)

Issue that this runs into is when the layout’s of pages are functions.

1 Like

Thanks, great tip @jinnyzor . I have another question, is it possible to change the id of an existing component in the layout using a callback. Something like this:

@callback(
    Output('element-1','id'),
    Input('button','n_clicks')
)
def change_id(click):
    return 'new_element'

I haven’t done it, but it might work.

However, when you do that, you will break the callback that changed it. :stuck_out_tongue_winking_eye:

If you are altering the element, it may be better to pass a className instead to highlight it.

1 Like