Skip to content

Commit

Permalink
Replacing table._underlying_table_id with table._callable_object_id
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Oct 31, 2023
1 parent 0b68969 commit e0fbf97
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _build_action_loop_callbacks() -> None:
actions_chain_trigger_component = model_manager[ModelID(str(actions_chain_trigger_component_id))]
# Use underlying table object as a trigger component.
if isinstance(actions_chain_trigger_component, Table):
actions_chain_trigger_component_id = actions_chain_trigger_component._underlying_table_id
actions_chain_trigger_component_id = actions_chain_trigger_component._callable_object_id
# Not all action_chain_trigger_components are included in model_manager e.g. on_page_load_action_trigger
except KeyError:
pass
Expand Down
9 changes: 2 additions & 7 deletions vizro-core/src/vizro/actions/_actions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _get_parent_vizro_table(_underlying_table_id: str) -> Table:
from vizro.models import Table

for _, table in model_manager._items_with_type(Table):
if table._underlying_table_id == _underlying_table_id:
if table._callable_object_id == _underlying_table_id:
return table
raise KeyError(f"No parent Vizro.Table component found for underlying table with id {_underlying_table_id}.")

Expand All @@ -105,12 +105,7 @@ def _apply_table_filter_interaction(
if not ctd_active_cell["value"] or not ctd_derived_viewport_data["value"]:
return data_frame

source_table_id = ctd_active_cell["id"]
try:
source_table_actions = _get_component_actions(model_manager[source_table_id])
except KeyError:
# source_table_id is underlying table id, so actions from parent Vizro.Table component should be considered.
source_table_actions = _get_component_actions(_get_parent_vizro_table(source_table_id))
source_table_actions = _get_component_actions(_get_parent_vizro_table(ctd_active_cell["id"]))

for action in source_table_actions:
if target not in action.function["targets"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def _get_inputs_of_figure_interactions(
inputs.append(
{
"active_cell": State(
component_id=triggered_model._underlying_table_id, component_property="active_cell"
component_id=triggered_model._callable_object_id, component_property="active_cell"
),
"derived_viewport_data": State(
component_id=triggered_model._underlying_table_id,
component_id=triggered_model._callable_object_id,
component_property="derived_viewport_data",
),
}
Expand Down
6 changes: 3 additions & 3 deletions vizro-core/src/vizro/models/_components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Table(VizroBaseModel):
title: Optional[str] = Field(None, description="Title of the table")
actions: List[Action] = []

_underlying_table_id: str = PrivateAttr()
_callable_object_id: str = PrivateAttr()

# Component properties for actions and interactions
_output_property: str = PrivateAttr("children")
Expand Down Expand Up @@ -72,15 +72,15 @@ def pre_build(self):
" Underlying table object has to be provided."
)

self._underlying_table_id = underlying_table_object.id
self._callable_object_id = underlying_table_object.id

def build(self):
return dcc.Loading(
html.Div(
[
html.H3(self.title, className="table-title") if self.title else None,
html.Div(
dash_table.DataTable(**({"id": self._underlying_table_id} if self.actions else {})), id=self.id
dash_table.DataTable(**({"id": self._callable_object_id} if self.actions else {})), id=self.id
),
],
className="table-container",
Expand Down
13 changes: 6 additions & 7 deletions vizro-core/tests/unit/vizro/actions/test_export_data_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def callback_context_export_data(request):
args_grouping_filter_interaction.append(
{
"active_cell": CallbackTriggerDict(
id="vizro_table",
id="underlying_table_id",
property="active_cell",
value={"row": 0, "column": 0, "column_id": "country"},
str_id="vizro_table",
str_id="underlying_table_id",
triggered=False,
),
"derived_viewport_data": CallbackTriggerDict(
id="vizro_table",
id="underlying_table_id",
property="derived_viewport_data",
value=[
{
Expand All @@ -92,7 +92,7 @@ def callback_context_export_data(request):
"year": 2007,
},
],
str_id="vizro_table",
str_id="underlying_table_id",
triggered=False,
),
}
Expand Down Expand Up @@ -307,9 +307,8 @@ def test_multiple_targets_with_filter_and_filter_interaction_and_table(
]

# Add table filter_interaction Action to scatter_chart component
model_manager["vizro_table"].actions = [
vm.Action(id="table_filter_interaction", function=filter_interaction(targets=["scatter_chart"]))
]
model_manager["vizro_table"].actions = [vm.Action(function=filter_interaction(targets=["scatter_chart"]))]
model_manager["vizro_table"].pre_build()

# Add export_data action to relevant component
model_manager["button"].actions = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def callback_context_filter_interaction(request):
id="underlying_table_id",
property="active_cell",
value={"row": 0, "column": 0, "column_id": "country"},
str_id="vizro_table",
str_id="underlying_table_id",
triggered=False,
),
"derived_viewport_data": CallbackTriggerDict(
Expand All @@ -60,7 +60,7 @@ def callback_context_filter_interaction(request):
"year": 2007,
},
],
str_id="vizro_table",
str_id="underlying_table_id",
triggered=False,
),
}
Expand Down
10 changes: 6 additions & 4 deletions vizro-core/tests/unit/vizro/models/_components/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def expected_table_with_id():
],
className="table-container",
id="text_table_outer",
)
),
color="grey",
parent_className="loading-container",
)


Expand Down Expand Up @@ -141,7 +143,7 @@ def test_pre_build_no_actions_no_underlying_table_id(self, standard_dash_table):
)
table.pre_build()

assert hasattr(table, "_underlying_table_id") is False
assert hasattr(table, "_callable_object_id") is False

def test_pre_build_actions_no_underlying_table_id_exception(self, standard_dash_table, filter_interaction_action):
table = vm.Table(
Expand All @@ -160,8 +162,8 @@ def test_pre_build_actions_underlying_table_id(self, dash_data_table_with_id, fi
)
table.pre_build()

assert hasattr(table, "_underlying_table_id") is True
assert table._underlying_table_id == "underlying_table_id"
assert hasattr(table, "_callable_object_id") is True
assert table._callable_object_id == "underlying_table_id"


class TestBuildTable:
Expand Down

0 comments on commit e0fbf97

Please sign in to comment.