✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Dynamically created item is absent from Dash.layout and never trigger callbacks

PROBLEM: callbacks for dynamically created buttons do not register

  • added a close-to-minimal example of dynamically creating buttons
  • dynamic buttons get dynamic ID
  • callbacks registered dynamically using this ID
  • callbacks are present in app.callback_map - checked with debugger
  • however dynamic button is missing from app.layout


  • can I update app.layout to involve the dynamically added components?
  • would this fix the callback issue?
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__)

app.layout = html.Div(children=[
    html.H1(children='Dynamic content with callback'),
    html.Div(id="btn-wrapper", children=[html.Button("Click me", id="btn-0")]),

def log_click(n_clicks, children):
    print("Dynamic button was clicked")
    return children

def on_click(n_clicks, children):
    btn_id = "btn-%d" % n_clicks
    children.append(html.Button("%s" % btn_id, id=btn_id))
    # add callback to dynamically created button
                 output=Output(btn_id, 'children'),
                 inputs=[Input(btn_id, 'n_clicks')],
                 state=[State(btn_id, 'children')]
    return children

    output=Output('btn-wrapper', 'children'),
    inputs=[Input('btn-0', 'n_clicks')],
    state=[State('btn-wrapper', 'children')]

if __name__ == '__main__':