Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Feature Request] Add triggered_id to clientside callabck_context #2692

Closed
AnnMarieW opened this issue Nov 13, 2023 · 1 comment · Fixed by #2695
Closed

[Feature Request] Add triggered_id to clientside callabck_context #2692

AnnMarieW opened this issue Nov 13, 2023 · 1 comment · Fixed by #2695

Comments

@AnnMarieW
Copy link
Collaborator

AnnMarieW commented Nov 13, 2023

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 🙂

@geophpherie
Copy link

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]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants