Skip to content

Commit a676f23

Browse files
committed
👌 Add multi output duplicate callback validation.
1 parent b4136a3 commit a676f23

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

dash/dash.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import warnings
1313
import re
1414
import logging
15+
import pprint
1516

1617
from functools import wraps
1718

@@ -754,15 +755,47 @@ def _validate_callback(self, output, inputs, state, events):
754755
).replace(' ', ''))
755756

756757
callback_id = _create_callback_id(output)
757-
if callback_id in self.callback_map:
758-
raise exceptions.CantHaveMultipleOutputs('''
758+
is_multi = isinstance(output, (list, tuple))
759+
callbacks = set(itertools.chain(*(
760+
x[1:-1].split(':')
761+
if x.startswith('[')
762+
else [x]
763+
for x in self.callback_map
764+
)))
765+
ns = {
766+
'duplicates': callback_id
767+
}
768+
if is_multi:
769+
def duplicate_check():
770+
ns['duplicates'] = intersection = callbacks.intersection(
771+
_create_callback_id(y) for y in output
772+
)
773+
return intersection
774+
else:
775+
def duplicate_check():
776+
return callback_id in callbacks
777+
if duplicate_check():
778+
if is_multi:
779+
msg = '''
780+
Multi output {} contains an `Output` object
781+
that was already assigned.
782+
Duplicates:
783+
{}
784+
'''.format(
785+
callback_id,
786+
pprint.pformat((ns['duplicates']))
787+
)
788+
else:
789+
msg = '''
759790
You have already assigned a callback to the output
760791
with ID "{}" and property "{}". An output can only have
761792
a single callback function. Try combining your inputs and
762793
callback functions together into one function.
763-
'''.format(
764-
output.component_id,
765-
output.component_property).replace(' ', ''))
794+
'''.format(
795+
output.component_id,
796+
output.component_property
797+
).replace(' ', '')
798+
raise exceptions.CantHaveMultipleOutputs(msg)
766799

767800
def _validate_callback_output(self, output_value, output):
768801
valid = [str, dict, int, float, type(None), Component]

0 commit comments

Comments
 (0)