Skip to content

[Feature Request] Add triggered_id to clientside callabck_context #2692

Closed

Description

@AnnMarieW

Make triggered_id available clientside like in #1240

It would be convenient to include triggered_id with clientside callbacks as well, so you could get the id simply by:


from dash import Dash, html, Input, Output

app = Dash(prevent_initial_callbacks=True)

app.layout = html.Div([
    html.Button("Button 1", id="btn1"),
    html.Button("Button 2", id="btn2"),
    html.Button("Button 3", id="btn3"),
    html.Div(id="log")
])

app.clientside_callback(
    """
    function(){
        const triggered_id = dash_clientside.callback_context.triggered_id
        return "triggered id: " + triggered_id
    }
    """,
    Output("log", "children"),
    Input("btn1", "n_clicks"),
    Input("btn2", "n_clicks"),
    Input("btn3", "n_clicks"),
)

if __name__ == '__main__':
    app.run_server()


And with pattern matching callbacks you can access the dict id like this:


from dash import Dash, html, Input, Output, ALL

app = Dash(prevent_initial_callbacks=True)

app.layout = html.Div([
    html.Button("Button 4", id={"type": "btn", "index":4}),
    html.Button("Button 5", id={"type": "btn", "index":5}),
    html.Button("Button 6", id={"type": "btn", "index":6}),
    html.Div(id="log")
])
app.clientside_callback(
    """
    function(){
        const triggered_id = dash_clientside.callback_context.triggered_id
        return "triggered button index # " + triggered_id.index;
    }
    """,
    Output("log", "children"),
    Input({"type":"btn","index":ALL}, "n_clicks" )
)

if __name__ == '__main__':
    app.run_server()


@alexcjohnson I have a PR ready to go if you would like it 🙂

Activity

  1. geophpherie commented on Nov 13, 2023

    @geophpherie

    I was just looking for this property and instead ended up with this workaround. triggered_id would be nice!

    window.dash_clientside.callback_context.triggered[0].prop_id.split('.')[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions