Skip to content

Commit 47efec5

Browse files
committed
adds allow_optional to State and Input to place no value as the placeholders
1 parent 8ee6a46 commit 47efec5

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

dash/dash-renderer/src/actions/callbacks.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,27 @@ function unwrapIfNotMulti(
122122

123123
if (idProps.length !== 1) {
124124
if (!idProps.length) {
125-
const isStr = typeof spec.id === 'string';
126-
msg =
127-
'A nonexistent object was used in an `' +
128-
depType +
129-
'` of a Dash callback. The id of this object is ' +
130-
(isStr
131-
? '`' + spec.id + '`'
132-
: JSON.stringify(spec.id) +
133-
(anyVals ? ' with MATCH values ' + anyVals : '')) +
134-
' and the property is `' +
135-
spec.property +
136-
(isStr
137-
? '`. The string ids in the current layout are: [' +
138-
keys(paths.strs).join(', ') +
139-
']'
140-
: '`. The wildcard ids currently available are logged above.');
125+
if (spec.allow_optional) {
126+
idProps = [{...spec, value: null}]
127+
msg = ''
128+
} else {
129+
const isStr = typeof spec.id === 'string';
130+
msg =
131+
'A nonexistent object was used in an `' +
132+
depType +
133+
'` of a Dash callback. The id of this object is ' +
134+
(isStr
135+
? '`' + spec.id + '`'
136+
: JSON.stringify(spec.id) +
137+
(anyVals ? ' with MATCH values ' + anyVals : '')) +
138+
' and the property is `' +
139+
spec.property +
140+
(isStr
141+
? '`. The string ids in the current layout are: [' +
142+
keys(paths.strs).join(', ') +
143+
']'
144+
: '`. The wildcard ids currently available are logged above.');
145+
}
141146
} else {
142147
msg =
143148
'Multiple objects were found for an `' +
@@ -203,7 +208,6 @@ function fillVals(
203208
// That's a real problem, so throw the first message as an error.
204209
refErr(errors, paths);
205210
}
206-
207211
return inputVals;
208212
}
209213

dash/dependencies.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DashDependency: # pylint: disable=too-few-public-methods
3535
allow_duplicate: bool
3636
component_property: str
3737
allowed_wildcards: Sequence[_Wildcard]
38+
allow_optional: bool
3839

3940
def __init__(self, component_id: ComponentIdType, component_property: str):
4041

@@ -45,6 +46,7 @@ def __init__(self, component_id: ComponentIdType, component_property: str):
4546

4647
self.component_property = component_property
4748
self.allow_duplicate = False
49+
self.allow_optional = False
4850

4951
def __str__(self):
5052
return f"{self.component_id_str()}.{self.component_property}"
@@ -56,7 +58,8 @@ def component_id_str(self) -> str:
5658
return stringify_id(self.component_id)
5759

5860
def to_dict(self) -> dict:
59-
return {"id": self.component_id_str(), "property": self.component_property}
61+
specs = {"id": self.component_id_str(), "property": self.component_property}
62+
return {**specs, 'allow_optional': True} if self.allow_optional else specs
6063

6164
def __eq__(self, other):
6265
"""
@@ -133,14 +136,29 @@ def __init__(
133136

134137
class Input(DashDependency): # pylint: disable=too-few-public-methods
135138
"""Input of callback: trigger an update when it is updated."""
136-
137-
allowed_wildcards = (MATCH, ALL, ALLSMALLER)
139+
def __init__(
140+
self,
141+
component_id: ComponentIdType,
142+
component_property: str,
143+
allow_optional: bool = False,
144+
):
145+
super().__init__(component_id, component_property)
146+
self.allow_optional = allow_optional
147+
self.allowed_wildcards = (MATCH, ALL, ALLSMALLER)
138148

139149

140150
class State(DashDependency): # pylint: disable=too-few-public-methods
141151
"""Use the value of a State in a callback but don't trigger updates."""
142-
143-
allowed_wildcards = (MATCH, ALL, ALLSMALLER)
152+
def __init__(
153+
self,
154+
component_id: ComponentIdType,
155+
component_property: str,
156+
allow_optional: bool = False,
157+
):
158+
super().__init__(component_id, component_property)
159+
self.allow_optional = allow_optional
160+
self.allowed_wildcards = (MATCH, ALL, ALLSMALLER)
161+
# allowed_wildcards = (MATCH, ALL, ALLSMALLER)
144162

145163

146164
class ClientsideFunction: # pylint: disable=too-few-public-methods

0 commit comments

Comments
 (0)