Skip to content

Commit

Permalink
dash_with_id fixture refactored + building datatable in pre_build Tab…
Browse files Browse the repository at this point in the history
…le method
  • Loading branch information
petar-qb committed Oct 27, 2023
1 parent 0383f02 commit 9051156
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 40 deletions.
14 changes: 7 additions & 7 deletions vizro-core/src/vizro/models/_components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Literal, Optional

from dash import dash_table, html
from pandas import DataFrame
from pydantic import Field, PrivateAttr, validator

import vizro.tables as vt
Expand Down Expand Up @@ -53,17 +54,16 @@ def __getitem__(self, arg_name: str):
return self.type
return self.figure[arg_name]

def _build_underlying_table_object(self):
data = data_manager._get_component_data(self.id) # type: ignore
kwargs = self.figure._arguments.copy()
kwargs.pop("data_frame", None)
return self.figure._function(data_frame=data, **kwargs)

@_log_call
def pre_build(self):
if self.actions:
kwargs = self.figure._arguments.copy()

# This is a hack to get around the fact that the underlying table object requires a data_frame
kwargs["data_frame"] = DataFrame()

# The underlying table object is pre-built, so we can fetch its ID.
underlying_table_object = self._build_underlying_table_object()
underlying_table_object = self.figure._function(**kwargs)

# Underlying table object has to have "id" defined if it triggers actions chain.
if not hasattr(underlying_table_object, "id"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from vizro.actions import export_data, filter_interaction
from vizro.actions._action_loop._get_action_loop_components import _get_action_loop_components
from vizro.managers import model_manager
from vizro.tables import dash_data_table


@pytest.fixture
Expand Down Expand Up @@ -69,12 +68,7 @@ def trigger_to_actions_chain_mapper_component(request):


@pytest.fixture
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
def managers_one_page_two_components_two_controls(dash_data_table_fixture_with_id):
def managers_one_page_two_components_two_controls(dash_data_table_with_id):
"""Instantiates managers with one page that contains two controls and two components."""
vm.Dashboard(
pages=[
Expand All @@ -84,7 +78,7 @@ def managers_one_page_two_components_two_controls(dash_data_table_fixture_with_i
components=[
vm.Table(
id="vizro_table",
figure=dash_data_table_fixture_with_id,
figure=dash_data_table_with_id,
actions=[
vm.Action(
id="table_filter_interaction_action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from vizro.actions import export_data, filter_interaction
from vizro.actions._callback_mapping._get_action_callback_mapping import _get_action_callback_mapping
from vizro.models.types import capture
from vizro.tables import dash_data_table


@capture("action")
Expand All @@ -30,12 +29,7 @@ def export_data():


@pytest.fixture
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
def managers_one_page_four_controls_two_figures_filter_interaction(request, dash_data_table_fixture_with_id):
def managers_one_page_four_controls_two_figures_filter_interaction(request, dash_data_table_with_id):
"""Instantiates managers with one page that contains four controls, two graphs and filter interaction."""
# If the fixture is parametrised set the targets. Otherwise, set export_data without targets.
export_data_action_function = export_data(targets=request.param) if hasattr(request, "param") else export_data()
Expand All @@ -58,7 +52,7 @@ def managers_one_page_four_controls_two_figures_filter_interaction(request, dash
),
vm.Table(
id="vizro_table",
figure=dash_data_table_fixture_with_id,
figure=dash_data_table_with_id,
actions=[
vm.Action(
id="table_filter_interaction_action",
Expand Down
10 changes: 2 additions & 8 deletions vizro-core/tests/unit/vizro/actions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.tables import dash_data_table


@pytest.fixture
Expand Down Expand Up @@ -31,11 +30,6 @@ def scatter_chart(gapminder_2007, scatter_params):
return px.scatter(gapminder_2007, **scatter_params).update_layout(margin_t=24)


@pytest.fixture
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
def target_scatter_filtered_continent(request, gapminder_2007, scatter_params):
continent = request.param
Expand Down Expand Up @@ -66,15 +60,15 @@ def managers_one_page_two_graphs_one_button(box_chart, scatter_chart):


@pytest.fixture
def managers_one_page_two_graphs_one_table_one_button(box_chart, scatter_chart, dash_data_table_fixture_with_id):
def managers_one_page_two_graphs_one_table_one_button(box_chart, scatter_chart, dash_data_table_with_id):
"""Instantiates a simple model_manager and data_manager with a page, two graph models and the button component."""
vm.Page(
id="test_page",
title="My first dashboard",
components=[
vm.Graph(id="box_chart", figure=box_chart),
vm.Graph(id="scatter_chart", figure=scatter_chart),
vm.Table(id="vizro_table", figure=dash_data_table_fixture_with_id),
vm.Table(id="vizro_table", figure=dash_data_table_with_id),
vm.Button(id="button"),
],
)
Expand Down
5 changes: 5 additions & 0 deletions vizro-core/tests/unit/vizro/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def standard_dash_table(gapminder):
return dash_data_table(data_frame=gapminder)


@pytest.fixture
def dash_data_table_with_id(gapminder):
return dash_data_table(id="underlying_table_id", data_frame=gapminder)


@pytest.fixture
def standard_go_chart(gapminder):
return go.Figure(data=go.Scatter(x=gapminder["gdpPercap"], y=gapminder["lifeExp"], mode="markers"))
Expand Down
13 changes: 4 additions & 9 deletions vizro-core/tests/unit/vizro/models/_components/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
from vizro.tables import dash_data_table


@pytest.fixture
def dash_table_with_id(gapminder):
return dash_data_table(id="underlying_table_id", data_frame=gapminder)


@pytest.fixture
def dash_table_with_arguments():
return dash_data_table(data_frame=px.data.gapminder(), style_header={"border": "1px solid green"})
Expand Down Expand Up @@ -151,10 +146,10 @@ def test_pre_build_actions_no_underlying_table_id_exception(self, standard_dash_
with pytest.raises(ValueError, match="Underlying table object has no attribute 'id'."):
table.pre_build()

def test_pre_build_actions_underlying_table_id(self, dash_table_with_id, filter_interaction_action):
def test_pre_build_actions_underlying_table_id(self, dash_data_table_with_id, filter_interaction_action):
table = vm.Table(
id="text_table",
figure=dash_table_with_id,
figure=dash_data_table_with_id,
actions=[filter_interaction_action],
)
table.pre_build()
Expand All @@ -176,10 +171,10 @@ def test_table_build_mandatory_only(self, standard_dash_table, expected_table):
expected = json.loads(json.dumps(expected_table, cls=plotly.utils.PlotlyJSONEncoder))
assert result == expected

def test_table_build_with_id(self, dash_table_with_id, filter_interaction_action, expected_table_with_id):
def test_table_build_with_id(self, dash_data_table_with_id, filter_interaction_action, expected_table_with_id):
table = vm.Table(
id="text_table",
figure=dash_table_with_id,
figure=dash_data_table_with_id,
actions=[filter_interaction_action],
)

Expand Down

0 comments on commit 9051156

Please sign in to comment.