Skip to content

Commit 2a3f9c2

Browse files
committed
Add optional callbacks
1 parent d556791 commit 2a3f9c2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

dash/_callback.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def callback(
7777
cache_args_to_ignore: Optional[list] = None,
7878
cache_ignore_triggered=True,
7979
on_error: Optional[Callable[[Exception], Any]] = None,
80+
optional: Optional[bool] = False,
8081
**_kwargs,
8182
) -> Callable[..., Any]:
8283
"""
@@ -159,6 +160,8 @@ def callback(
159160
Function to call when the callback raises an exception. Receives the
160161
exception object as first argument. The callback_context can be used
161162
to access the original callback inputs, states and output.
163+
:param optional:
164+
Mark all dependencies as not required on the initial layout checks.
162165
"""
163166

164167
background_spec = None
@@ -213,6 +216,7 @@ def callback(
213216
manager=manager,
214217
running=running,
215218
on_error=on_error,
219+
optional=optional,
216220
)
217221

218222

@@ -258,6 +262,7 @@ def insert_callback(
258262
running=None,
259263
dynamic_creator: Optional[bool] = False,
260264
no_output=False,
265+
optional=False,
261266
):
262267
if prevent_initial_call is None:
263268
prevent_initial_call = config_prevent_initial_callbacks
@@ -281,6 +286,7 @@ def insert_callback(
281286
},
282287
"dynamic_creator": dynamic_creator,
283288
"no_output": no_output,
289+
"optional": optional,
284290
}
285291
if running:
286292
callback_spec["running"] = running
@@ -624,6 +630,7 @@ def register_callback(
624630
dynamic_creator=allow_dynamic_callbacks,
625631
running=running,
626632
no_output=not has_output,
633+
optional=_kwargs.get("optional", False),
627634
)
628635

629636
# pylint: disable=too-many-locals

dash/dash-renderer/src/actions/dependencies.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,18 @@ export function validateCallbacksToLayout(state_, dispatchError) {
570570
for (const id in map) {
571571
const idProps = map[id];
572572
const fcb = flatten(values(idProps));
573-
const optional = all(
574-
({allow_optional}) => allow_optional,
575-
flatten(
576-
fcb.map(cb => concat(cb.outputs, cb.inputs, cb.states))
577-
).filter(dep => dep.id === id)
578-
);
573+
const optional = fcb.reduce((acc, cb) => {
574+
if (acc === false || cb.optional) {
575+
return acc;
576+
}
577+
const deps = concat(cb.outputs, cb.inputs, cb.states).filter(
578+
dep => dep.id === id
579+
);
580+
return (
581+
!deps.length ||
582+
all(({allow_optional}) => allow_optional, deps)
583+
);
584+
}, true);
579585
if (optional) {
580586
continue;
581587
}

0 commit comments

Comments
 (0)