Skip to content

Commit

Permalink
Solving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Apr 8, 2024
2 parents 0c65e2c + 31bd5a3 commit d0eac24
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 73 deletions.
18 changes: 8 additions & 10 deletions vizro-core/examples/_dev/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Rough example used by developers."""
from dash import Output, State
from dash import Output, State

from typing import Any, Dict, List, Optional

import vizro.models as vm
import vizro.plotly.express as px
from dash import Output
from vizro import Vizro
from vizro.actions import export_data, filter_interaction
from vizro.tables import dash_ag_grid
from vizro.models.types import capture
from vizro.managers._model_manager import ModelID
from typing import Any, Dict, List, Optional

from vizro.models.types import capture
from vizro.tables import dash_ag_grid

df_gapminder = px.data.gapminder().query("year == 2007")
default_card_text = "Click on a cell in the table to filter the scatter plot."
Expand Down Expand Up @@ -41,16 +41,14 @@ def update_card_with_grid_clicked_data(grid_cell_clicked: Optional[Dict] = None)
# to solve this, but it's not clear which one is the best.
@capture("action")
def overwritten_filter_interactions_1(
targets: Optional[List[ModelID]] = None,
grid_cell_clicked: Optional[Dict] = None,
**inputs: Dict[str, Any]
targets: Optional[List[ModelID]] = None, card_cellClicked: Optional[Dict] = None, **inputs: Dict[str, Any]
):
return {
"card_children": _build_card_text_message(grid_cell_clicked=grid_cell_clicked),
**filter_interaction.pure_function(targets=targets, **inputs),
}

# 2. Overwriting the filter_interaction action by inheriting filter_interaction
# 2. Overwriting the filter_interaction action by inheriting FilterInteractionAction

# This also works:
# from vizro.actions.filter_interaction_action import FilterInteractionAction
Expand Down Expand Up @@ -152,7 +150,7 @@ def outputs(self):
# TODO-AV2-TICKET-CREATED: Attach the 'update_card_with_grid_clicked_data' action after
# the default filter action.
vm.Filter(targets=["scatter"], column="continent"),
vm.Parameter(targets=["scatter.x"], selector=vm.RadioItems(options=["gdpPercap", "lifeExp"]))
vm.Parameter(targets=["scatter.x"], selector=vm.RadioItems(options=["gdpPercap", "lifeExp"])),
],
# TODO-AV2-TICKET-CREATED: Try to attach the custom action that clicks data after the update_figures action.
),
Expand Down
4 changes: 2 additions & 2 deletions vizro-core/src/vizro/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# TODO-AV2-OQ: Consider: 1. Actions alias names, 2. actions folder structure 3. making the actions public/private
from vizro.actions.filter_action import filter_action
from vizro.actions.parameter_action import parameter_action
from vizro.actions.export_data_action import export_data
from vizro.actions.filter_action import filter_action
from vizro.actions.filter_interaction_action import filter_interaction
from vizro.actions.parameter_action import parameter_action
from vizro.actions.update_figures_action import update_figures

# Please keep alphabetically ordered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
"""Contains utilities to create the action_callback_mapping."""

from typing import Any, Callable, Dict, List, Union
from typing import Dict, List

from dash import Output, State, dcc
from dash import State

from vizro.actions import parameter_action, export_data, filter_interaction
from vizro.managers import model_manager
from vizro.managers._model_manager import ModelID
from vizro.models import Action, Page
from vizro.models._controls import Filter, Parameter
from vizro.models.types import ControlType
from vizro.models.types import CapturedActionCallable


def _get_matching_page_actions_by_action_class(
page_id: ModelID, action_class: CapturedActionCallable,
page_id: ModelID,
action_class: CapturedActionCallable,
) -> List[Action]:
"""Gets list of `Actions` on triggered `Page` that match the provided `action_class`."""
return [
Expand All @@ -29,10 +27,7 @@ def _get_matching_page_actions_by_action_class(
# _get_inputs_of_filters, _get_inputs_of_parameters, _get_inputs_of_figure_interactions will become a single function.
# This approach should enable predefined actions to see custom actions if they are inherited from the predefined
# actions (which is a use case we want to achieve).
def _get_inputs_of_filters(
page: Page,
action_class: CapturedActionCallable = None
) -> List[State]:
def _get_inputs_of_filters(page: Page, action_class: CapturedActionCallable = None) -> List[State]:
"""Gets list of `States` for all components that have the `filter` action from the `Page`."""
filter_actions_on_page = _get_matching_page_actions_by_action_class(
page_id=ModelID(str(page.id)), action_class=action_class
Expand All @@ -45,10 +40,7 @@ def _get_inputs_of_filters(
return inputs


def _get_inputs_of_parameters(
page: Page,
action_class: CapturedActionCallable = None
) -> List[State]:
def _get_inputs_of_parameters(page: Page, action_class: CapturedActionCallable = None) -> List[State]:
"""Gets list of `States` for all components that have the `parameter` action from the `Page`."""
parameter_actions_on_page = _get_matching_page_actions_by_action_class(
page_id=ModelID(str(page.id)), action_class=action_class
Expand Down
10 changes: 3 additions & 7 deletions vizro-core/src/vizro/actions/export_data_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib
from typing import Any, Dict, List, Literal

from dash import Output, State, ctx, dcc
from dash import Output, ctx, dcc

from vizro.managers import model_manager
from vizro.models.types import CapturedActionCallable
Expand Down Expand Up @@ -42,9 +42,7 @@ def _post_init(self):

@staticmethod
def pure_function(
targets: List[str],
file_format: Literal["csv", "xlsx"] = "csv",
**inputs: Dict[str, Any]
targets: List[str], file_format: Literal["csv", "xlsx"] = "csv", **inputs: Dict[str, Any]
) -> Dict[str, Any]:
"""Exports visible data of target charts/components on page after being triggered.
Expand Down Expand Up @@ -96,9 +94,7 @@ def inputs(self):
page = model_manager[self._page_id]
return {
"filters": _get_inputs_of_filters(page=page, action_class=FilterAction),
"filter_interaction": _get_inputs_of_figure_interactions(
page=page, action_class=FilterInteractionAction
),
"filter_interaction": _get_inputs_of_figure_interactions(page=page, action_class=FilterInteractionAction),
}

@property
Expand Down
13 changes: 5 additions & 8 deletions vizro-core/src/vizro/actions/filter_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import importlib
from typing import Any, Callable, Dict, List, Literal
from typing import Any, Callable, Dict, List

from dash import Output, State, ctx, dcc
from dash import Output, State, ctx
from pandas import Series

from vizro.managers import model_manager
Expand Down Expand Up @@ -32,7 +31,7 @@ def pure_function(
filter_column: str,
targets: List[ModelID],
filter_function: Callable[[Series, Any], Series],
**inputs: Dict[str, Any]
**inputs: Dict[str, Any],
) -> Dict[str, Any]:
"""Filters targeted charts/components on page by interaction with `Filter` control.
Expand Down Expand Up @@ -63,8 +62,8 @@ def inputs(self):
_get_inputs_of_filters,
_get_inputs_of_parameters,
)
from vizro.actions.parameter_action import ParameterAction
from vizro.actions.filter_interaction_action import FilterInteractionAction
from vizro.actions.parameter_action import ParameterAction

# TODO-AV2-OQ: Consider the following inputs ctx form:
# ```
Expand All @@ -80,9 +79,7 @@ def inputs(self):
page = model_manager[self._page_id]
return {
"filters": _get_inputs_of_filters(page=page, action_class=FilterAction),
"filter_interaction": _get_inputs_of_figure_interactions(
page=page, action_class=FilterInteractionAction
),
"filter_interaction": _get_inputs_of_figure_interactions(page=page, action_class=FilterInteractionAction),
"parameters": _get_inputs_of_parameters(page=page, action_class=ParameterAction),
# TODO-AV2-OQ: Propagate theme_selector only if it exists on the page (could be overwritten by the user)
"theme_selector": State("theme_selector", "checked"),
Expand Down
10 changes: 3 additions & 7 deletions vizro-core/src/vizro/actions/filter_interaction_action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import importlib
from typing import Any, Callable, Dict, List, Literal, Optional
from typing import Any, Dict, List, Optional

from dash import Output, State, ctx, dcc
from pandas import Series
from dash import Output, State, ctx

from vizro.managers import model_manager
from vizro.managers._model_manager import ModelID
Expand Down Expand Up @@ -70,9 +68,7 @@ def inputs(self):

return {
"filters": _get_inputs_of_filters(page=page, action_class=FilterAction),
"filter_interaction": _get_inputs_of_figure_interactions(
page=page, action_class=FilterInteractionAction
),
"filter_interaction": _get_inputs_of_figure_interactions(page=page, action_class=FilterInteractionAction),
"parameters": _get_inputs_of_parameters(page=page, action_class=ParameterAction),
"theme_selector": State("theme_selector", "checked"),
}
Expand Down
26 changes: 11 additions & 15 deletions vizro-core/src/vizro/actions/parameter_action.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import importlib
from typing import Any, Dict, List, Literal
from typing import Any, Dict, List

from dash import Output, State, ctx, dcc
from dash import Output, State, ctx

from vizro.actions import filter_action
from vizro.managers import model_manager
from vizro.models.types import CapturedActionCallable

Expand Down Expand Up @@ -35,15 +33,15 @@ def _post_init(self):
def pure_function(targets: List[str], **inputs: Dict[str, Any]) -> Dict[str, Any]:
"""Modifies parameters of targeted charts/components on page.
Args:
targets: List of target component ids to change parameters of.
inputs: Dict mapping action function names with their inputs e.g.
inputs = {'filters': [], 'parameters': ['gdpPercap'], 'filter_interaction': [], 'theme_selector': True}
Args:
targets: List of target component ids to change parameters of.
inputs: Dict mapping action function names with their inputs e.g.
inputs = {'filters': [], 'parameters': ['gdpPercap'], 'filter_interaction': [], 'theme_selector': True}
Returns:
Dict mapping target component ids to modified charts/components e.g. {'my_scatter': Figure({})}
Returns:
Dict mapping target component ids to modified charts/components e.g. {'my_scatter': Figure({})}
"""
"""
from vizro.actions._actions_utils import _get_modified_page_figures

target_ids: List[ModelID] = [target.split(".")[0] for target in targets] # type: ignore[misc]
Expand All @@ -60,17 +58,15 @@ def inputs(self):
from vizro.actions._callback_mapping._callback_mapping_utils import (
_get_inputs_of_figure_interactions,
_get_inputs_of_filters,
_get_inputs_of_parameters
_get_inputs_of_parameters,
)
from vizro.actions.filter_action import FilterAction
from vizro.actions.filter_interaction_action import FilterInteractionAction

page = model_manager[self._page_id]
return {
"filters": _get_inputs_of_filters(page=page, action_class=FilterAction),
"filter_interaction": _get_inputs_of_figure_interactions(
page=page, action_class=FilterInteractionAction
),
"filter_interaction": _get_inputs_of_figure_interactions(page=page, action_class=FilterInteractionAction),
"parameters": _get_inputs_of_parameters(page=page, action_class=ParameterAction),
"theme_selector": State("theme_selector", "checked"),
}
Expand Down
10 changes: 3 additions & 7 deletions vizro-core/src/vizro/actions/update_figures_action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import importlib
from typing import Any, Callable, Dict, List, Literal, Optional
from typing import Any, Dict, List, Optional

from dash import Output, State, ctx, dcc
from pandas import Series
from dash import Output, State, ctx

from vizro.managers import model_manager
from vizro.managers._model_manager import ModelID
Expand Down Expand Up @@ -71,9 +69,7 @@ def inputs(self):

return {
"filters": _get_inputs_of_filters(page=page, action_class=FilterAction),
"filter_interaction": _get_inputs_of_figure_interactions(
page=page, action_class=FilterInteractionAction
),
"filter_interaction": _get_inputs_of_figure_interactions(page=page, action_class=FilterInteractionAction),
"parameters": _get_inputs_of_parameters(page=page, action_class=ParameterAction),
"theme_selector": State("theme_selector", "checked"),
}
Expand Down
1 change: 0 additions & 1 deletion vizro-core/src/vizro/models/_action/_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pydantic import Field

import vizro.actions
from vizro.managers._model_manager import ModelID
from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call
from vizro.models.types import CapturedActionCallable, CapturedCallable
Expand Down
4 changes: 3 additions & 1 deletion vizro-core/src/vizro/models/_controls/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def _set_actions(self):
filter_function = _filter_between if isinstance(self.selector, RangeSlider) else _filter_isin
self.selector.actions = [
Action(
function=filter_action(filter_column=self.column, targets=self.targets, filter_function=filter_function),
function=filter_action(
filter_column=self.column, targets=self.targets, filter_function=filter_function
),
id=f"{FILTER_ACTION_PREFIX}_{self.id}",
)
]
3 changes: 2 additions & 1 deletion vizro-core/src/vizro/models/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def pre_build(self):
),
actions=[
Action(
id=f"{ON_PAGE_LOAD_ACTION_PREFIX}_action_{self.id}", function=update_figures(targets=targets)
id=f"{ON_PAGE_LOAD_ACTION_PREFIX}_action_{self.id}",
function=update_figures(targets=targets),
)
],
)
Expand Down

0 comments on commit d0eac24

Please sign in to comment.