@@ -50,6 +50,18 @@ def states(self):
50
50
@property
51
51
@has_context
52
52
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
+ """
53
65
# For backward compatibility: previously `triggered` always had a
54
66
# value - to avoid breaking existing apps, add a dummy item but
55
67
# make the list still look falsy. So `if ctx.triggered` will make it
@@ -59,6 +71,26 @@ def triggered(self):
59
71
@property
60
72
@has_context
61
73
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
+ """
62
94
triggered = getattr (flask .g , "triggered_inputs" , [])
63
95
ids = AttributeDict ({})
64
96
for item in triggered :
@@ -71,6 +103,17 @@ def triggered_prop_ids(self):
71
103
@property
72
104
@has_context
73
105
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
+ """
74
117
component_id = None
75
118
if self .triggered :
76
119
prop_id = self .triggered_prop_ids .first ()
@@ -80,6 +123,30 @@ def triggered_id(self):
80
123
@property
81
124
@has_context
82
125
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
+ """
83
150
triggered = getattr (flask .g , "triggered_inputs" , [])
84
151
triggered = [item ["prop_id" ] for item in triggered ]
85
152
grouping = getattr (flask .g , "args_grouping" , {})
0 commit comments