Skip to content

Commit 67f56d0

Browse files
committed
added docstrings
1 parent 49a5e59 commit 67f56d0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

dash/_callback_context.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def states(self):
5050
@property
5151
@has_context
5252
def triggered(self):
53+
"""
54+
Returns a list of all the Input props that changed and caused the callback to execute. It is empty when the callback is
55+
called on initial load, unless an Input prop got its value from another initial callback. Callbacks triggered
56+
by user actions typically have one item in triggered, unless the same action changes two props at once or
57+
the callback has several Input props that are all modified by another callback based on a single user action.
58+
59+
Example: To get the id of the component that triggered the callback:
60+
`component_id = ctx.triggered[0]['prop_id'].split('.')[0]`
61+
62+
Example: To detect initial call, empty triggered is not really empty, it's falsy so that you can use:
63+
`if ctx.triggered:`
64+
"""
5365
# For backward compatibility: previously `triggered` always had a
5466
# value - to avoid breaking existing apps, add a dummy item but
5567
# make the list still look falsy. So `if ctx.triggered` will make it
@@ -59,6 +71,26 @@ def triggered(self):
5971
@property
6072
@has_context
6173
def triggered_prop_ids(self):
74+
"""
75+
Returns a dictionary of all the Input props that changed and caused the callback to execute. It is empty when the callback is
76+
called on initial load, unless an Input prop got its value from another initial callback. Callbacks triggered
77+
by user actions typically have one item in triggered, unless the same action changes two props at once or
78+
the callback has several Input props that are all modified by another callback based on a single user action.
79+
80+
triggered_prop_ids (dict):
81+
- keys (str) : the triggered "prop_id" composed of "component_id.component_property"
82+
- values (str or dict): the id of the component that triggered the callback. Will be the dict id for pattern matching callbacks
83+
84+
Example - regular callback
85+
{"btn-1.n_clicks": "btn-1"}
86+
87+
Example - pattern matching callbacks:
88+
{'{"index":0,"type":"filter-dropdown"}.value': {"index":0,"type":"filter-dropdown"}}
89+
90+
Example usage:
91+
`if "btn-1.n_clicks" in ctx.triggered_prop_ids:
92+
do_something()`
93+
"""
6294
triggered = getattr(flask.g, "triggered_inputs", [])
6395
ids = AttributeDict({})
6496
for item in triggered:
@@ -71,6 +103,17 @@ def triggered_prop_ids(self):
71103
@property
72104
@has_context
73105
def triggered_id(self):
106+
"""
107+
Returns the component id (str or dict) of the Input component that triggered the callback.
108+
109+
Note - use `triggered_prop_ids` if you need both the component id and the prop that triggered the callback or if
110+
multiple Inputs triggered the callback.
111+
112+
Example usage:
113+
`if "btn-1" == ctx.triggered_id:
114+
do_something()`
115+
116+
"""
74117
component_id = None
75118
if self.triggered:
76119
prop_id = self.triggered_prop_ids.first()
@@ -80,6 +123,30 @@ def triggered_id(self):
80123
@property
81124
@has_context
82125
def args_grouping(self):
126+
"""
127+
args_grouping is a dict of the inputs used with flexible callback signatures. The keys are the variable names
128+
and the values are dictionaries containing:
129+
- “id”: (string or dict) the component id. If it’s a pattern matching id, it will be a dict.
130+
- “id_str”: (str) for pattern matching ids, it’s the strigified dict id with no white spaces.
131+
- “property”: (str) The component property used in the callback.
132+
- “value”: the value of the component property at the time the callback was fired.
133+
- “triggered”: (bool)Whether this input triggered the callback.
134+
135+
Example usage:
136+
@app.callback(
137+
Output("container", "children"),
138+
inputs=dict(btn1=Input("btn-1", "n_clicks"), btn2=Input("btn-2", "n_clicks")),
139+
)
140+
def display(btn1, btn2):
141+
c = ctx.args_grouping
142+
if c.btn1.triggered:
143+
return f"Button 1 clicked {btn1} times"
144+
elif c.btn2.triggered:
145+
return f"Button 2 clicked {btn2} times"
146+
else:
147+
return "No clicks yet"
148+
149+
"""
83150
triggered = getattr(flask.g, "triggered_inputs", [])
84151
triggered = [item["prop_id"] for item in triggered]
85152
grouping = getattr(flask.g, "args_grouping", {})

0 commit comments

Comments
 (0)