Skip to content

Commit 6771920

Browse files
committed
Add triggered_id to callback_context
1 parent 97470a0 commit 6771920

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

dash/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import html # noqa: F401,E402
2020
from . import dash_table # noqa: F401,E402
2121
from .version import __version__ # noqa: F401,E402
22-
from ._callback_context import callback_context # noqa: F401,E402
22+
from ._callback_context import callback_context, ctx # noqa: F401,E402
2323
from ._callback import callback, clientside_callback # noqa: F401,E402
2424
from ._get_paths import ( # noqa: F401,E402
2525
get_asset_url,

dash/_callback_context.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22
import warnings
3-
3+
import json
44
import flask
55

66
from . import exceptions
@@ -54,6 +54,18 @@ def triggered(self):
5454
# look empty, but you can still do `triggered[0]["prop_id"].split(".")`
5555
return getattr(flask.g, "triggered_inputs", []) or falsy_triggered
5656

57+
@property
58+
@has_context
59+
def triggered_ids(self):
60+
triggered = getattr(flask.g, "triggered_inputs", [])
61+
ids = {}
62+
for item in triggered:
63+
component_id, _, _ = item["prop_id"].rpartition(".")
64+
ids[item["prop_id"]] = component_id
65+
if component_id.startswith("{"):
66+
ids[item["prop_id"]] = json.loads(component_id)
67+
return ids
68+
5769
@property
5870
@has_context
5971
def args_grouping(self):
@@ -145,3 +157,4 @@ def using_outputs_grouping(self):
145157

146158

147159
callback_context = CallbackContext()
160+
ctx = CallbackContext()

tests/integration/callbacks/test_callback_context.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import operator
33
import pytest
44

5-
from dash import Dash, Input, Output, html, dcc, callback_context
5+
from dash import Dash, Input, Output, html, dcc, callback_context, ctx
66

77
from dash.exceptions import PreventUpdate, MissingCallbackContextException
88
import dash.testing.wait as wait
@@ -330,3 +330,28 @@ def update_results(n1, n2, nsum):
330330
assert len(keys1) == 2
331331
assert "sum-number.value" in keys1
332332
assert "input-number-2.value" in keys1
333+
334+
335+
def test_cbcx007_triggered_id(dash_duo):
336+
app = Dash(__name__)
337+
338+
btns = ["btn-{}".format(x) for x in range(1, 6)]
339+
340+
app.layout = html.Div(
341+
[html.Div([html.Button(btn, id=btn) for btn in btns]), html.Div(id="output")]
342+
)
343+
344+
@app.callback(Output("output", "children"), [Input(x, "n_clicks") for x in btns])
345+
def on_click(*args):
346+
if not ctx.triggered:
347+
raise PreventUpdate
348+
for btn in btns:
349+
if btn in ctx.triggered_ids.values():
350+
return f"Just clicked {btn}"
351+
352+
dash_duo.start_server(app)
353+
354+
for i in range(1, 5):
355+
for btn in btns:
356+
dash_duo.find_element("#" + btn).click()
357+
dash_duo.wait_for_text_to_equal("#output", f"Just clicked {btn}")

0 commit comments

Comments
 (0)