Skip to content

Commit

Permalink
More efficient check
Browse files Browse the repository at this point in the history
  • Loading branch information
minhtuevo committed Nov 21, 2024
1 parent 37315d2 commit 22eb1c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
20 changes: 12 additions & 8 deletions fiftyone/operators/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ def btn(
name,
label,
icon=None,
icon_variant=None,
variant=None,
disabled=False,
on_click=None,
Expand Down Expand Up @@ -297,7 +296,6 @@ def btn(
btn = Button(
href=href,
icon=icon,
icon_variant=icon_variant,
icon_position=icon_position,
disabled=disabled,
label=label,
Expand Down Expand Up @@ -1857,7 +1855,7 @@ def __init__(self, row, column, **kwargs):
self.column = column

def clone(self):
clone = Tooltip(self.row, self. column, **self._kwargs)
clone = Tooltip(self.row, self.column, **self._kwargs)
return clone

def to_json(self):
Expand All @@ -1877,6 +1875,7 @@ def __init__(self, **kwargs):
self.columns = kwargs.get("columns", [])
self.row_actions = kwargs.get("row_actions", [])
self.tooltips = kwargs.get("tooltips", [])
self._tooltip_map = {}

def keys(self):
return [column.key for column in self.columns]
Expand Down Expand Up @@ -1907,21 +1906,27 @@ def add_row_action(
)
self.row_actions.append(row_action)
return row_action

def add_tooltip(self, row, column, value, **kwargs):
for tooltip in self.tooltips:
if tooltip.row == row and tooltip.column == column:
raise ValueError(f"Tooltip for row '{row}' and column '{column}' already exists")
if (row, column) in self._tooltip_map:
raise ValueError(
f"Tooltip for row '{row}' and column '{column}' already exists"
)

tooltip = Tooltip(row=row, column=column, value=value, **kwargs)
self.tooltips.append(tooltip)
self._tooltip_map[(row, column)] = tooltip
return tooltip

def clone(self):
clone = super().clone()
clone.columns = [column.clone() for column in self.columns]
clone.row_actions = [action.clone() for action in self.row_actions]
clone.tooltips = [tooltip.clone() for tooltip in self.tooltips]
clone._tooltip_map = {
(tooltip.row, tooltip.column): tooltip
for tooltip in clone.tooltips
}
return clone

def to_json(self):
Expand Down Expand Up @@ -2311,7 +2316,6 @@ class StatusButtonView(View):
"""

def __init__(self, **kwargs):
print("Status Button View is new")
super().__init__(**kwargs)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@ def test_table_view_basic(self):

mock_on_click = lambda: None

table.add_row_action("action1", on_click=mock_on_click, icon="icon1", color="primary", tooltip="Action 1")
table.add_row_action("action2", on_click=mock_on_click, icon="icon2", color="secondary", tooltip="Action 2")
table.add_row_action(
"action1",
on_click=mock_on_click,
icon="icon1",
color="primary",
tooltip="Action 1",
)
table.add_row_action(
"action2",
on_click=mock_on_click,
icon="icon2",
color="secondary",
tooltip="Action 2",
)

with self.assertRaises(ValueError):
table.add_row_action("action1", on_click=mock_on_click, icon="icon3", color="primary",
tooltip="Action 3")
table.add_row_action(
"action1",
on_click=mock_on_click,
icon="icon3",
color="primary",
tooltip="Action 3",
)

table.add_tooltip(1, 1, "Tooltip 1")
table.add_tooltip(1, 2, "Tooltip 2")
Expand Down

0 comments on commit 22eb1c3

Please sign in to comment.