Skip to content

Commit b492497

Browse files
Instead allow _selector_matches to accept function selector
1 parent 7040a57 commit b492497

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

Diff for: packages/python/plotly/plotly/basedatatypes.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -822,24 +822,33 @@ def _perform_select_traces(self, filter_by_subplot, grid_subplot_refs, selector)
822822
def _selector_matches(obj, selector):
823823
if selector is None:
824824
return True
825+
# If selector is a dict, compare the fields
826+
if type(selector) == type(dict()):
827+
# This returns True if selector is an empty dict
828+
for k in selector:
829+
if k not in obj:
830+
return False
825831

826-
for k in selector:
827-
if k not in obj:
828-
return False
829-
830-
obj_val = obj[k]
831-
selector_val = selector[k]
832-
833-
if isinstance(obj_val, BasePlotlyType):
834-
obj_val = obj_val.to_plotly_json()
832+
obj_val = obj[k]
833+
selector_val = selector[k]
835834

836-
if isinstance(selector_val, BasePlotlyType):
837-
selector_val = selector_val.to_plotly_json()
835+
if isinstance(obj_val, BasePlotlyType):
836+
obj_val = obj_val.to_plotly_json()
838837

839-
if obj_val != selector_val:
840-
return False
838+
if isinstance(selector_val, BasePlotlyType):
839+
selector_val = selector_val.to_plotly_json()
841840

842-
return True
841+
if obj_val != selector_val:
842+
return False
843+
return True
844+
# If selector is a function, call it with the obj as the argument
845+
elif type(selector) == type(lambda x: True):
846+
return selector(obj)
847+
else:
848+
raise TypeError(
849+
"selector must be dict or a function "
850+
"accepting a trace returning a boolean."
851+
)
843852

844853
def for_each_trace(self, fn, selector=None, row=None, col=None, secondary_y=None):
845854
"""

0 commit comments

Comments
 (0)