Skip to content

Commit

Permalink
Solving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Nov 15, 2024
2 parents 8ca5382 + cfdf4fc commit 472f7a8
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 69 deletions.
95 changes: 54 additions & 41 deletions vizro-core/examples/scratch_dev/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Dev app to try things out."""

import time
import yaml

Expand Down Expand Up @@ -27,24 +28,35 @@
def load_from_file(filter_column=None, parametrized_species=None):
# Load the full iris dataset
df = px.data.iris()
df['date_column'] = pd.date_range(start=pd.to_datetime("2024-01-01"), periods=len(df), freq='D')
df["date_column"] = pd.date_range(start=pd.to_datetime("2024-01-01"), periods=len(df), freq="D")

if parametrized_species:
return df[df["species"].isin(parametrized_species)]

with open('data.yaml', 'r') as file:
with open("data.yaml", "r") as file:
data = yaml.safe_load(file)
data = data or {}

filter_column = filter_column or FILTER_COLUMN
if filter_column == "species":
final_df = pd.concat(objs=[
df[df[filter_column] == 'setosa'].head(data.get("setosa", 0)),
df[df[filter_column] == 'versicolor'].head(data.get("versicolor", 0)),
df[df[filter_column] == 'virginica'].head(data.get("virginica", 0)),
], ignore_index=True)
final_df = pd.concat(
objs=[
df[df[filter_column] == "setosa"].head(data.get("setosa", 0)),
df[df[filter_column] == "versicolor"].head(data.get("versicolor", 0)),
df[df[filter_column] == "virginica"].head(data.get("virginica", 0)),
],
ignore_index=True,
)
elif filter_column == "sepal_length":
final_df = df[df[filter_column].between(data.get("min"), data.get("max",), inclusive="both")]
final_df = df[
df[filter_column].between(
data.get("min"),
data.get(
"max",
),
inclusive="both",
)
]
elif filter_column == "date_column":
date_min = pd.to_datetime(data.get("date_min"))
date_max = pd.to_datetime(data.get("date_max"))
Expand Down Expand Up @@ -82,16 +94,18 @@ def load_from_file(filter_column=None, parametrized_species=None):
vm.Graph(
id="p1-G-2",
figure=px.scatter(data_frame=px.data.iris(), **SCATTER_CHART_CONF),
)
),
],
controls=[
vm.Filter(id="p1-F-1", column="species", targets=["p1-G-1"], selector=vm.Dropdown(title="Dynamic filter")),
vm.Filter(id="p1-F-2", column="species", targets=["p1-G-2"], selector=vm.Dropdown(title="Static filter")),
vm.Parameter(
targets=["p1-G-1.x", "p1-G-2.x"],
selector=vm.RadioItems(options=["species", "sepal_width"], value="species", title="Simple X-axis parameter")
)
]
selector=vm.RadioItems(
options=["species", "sepal_width"], value="species", title="Simple X-axis parameter"
),
),
],
)


Expand All @@ -110,9 +124,11 @@ def load_from_file(filter_column=None, parametrized_species=None):
vm.Filter(id="p2-F-4", column="species", selector=vm.RadioItems()),
vm.Parameter(
targets=["p2-G-1.x"],
selector=vm.RadioItems(options=["species", "sepal_width"], value="species", title="Simple X-axis parameter")
)
]
selector=vm.RadioItems(
options=["species", "sepal_width"], value="species", title="Simple X-axis parameter"
),
),
],
)


Expand All @@ -129,9 +145,11 @@ def load_from_file(filter_column=None, parametrized_species=None):
vm.Filter(id="p3-F-2", column="sepal_length", selector=vm.RangeSlider()),
vm.Parameter(
targets=["p3-G-1.x"],
selector=vm.RadioItems(options=["species", "sepal_width"], value="species", title="Simple X-axis parameter")
)
]
selector=vm.RadioItems(
options=["species", "sepal_width"], value="species", title="Simple X-axis parameter"
),
),
],
)

page_4 = vm.Page(
Expand All @@ -147,9 +165,11 @@ def load_from_file(filter_column=None, parametrized_species=None):
vm.Filter(id="p4-F-2", column="date_column", selector=vm.DatePicker()),
vm.Parameter(
targets=["p4-G-1.x"],
selector=vm.RadioItems(options=["species", "sepal_width"], value="species", title="Simple X-axis parameter")
)
]
selector=vm.RadioItems(
options=["species", "sepal_width"], value="species", title="Simple X-axis parameter"
),
),
],
)

page_5 = vm.Page(
Expand All @@ -170,73 +190,66 @@ def load_from_file(filter_column=None, parametrized_species=None):
# "p5-F-1.",
],
selector=vm.Dropdown(
options=["setosa", "versicolor", "virginica"],
multi=True,
title="Parametrized species"
)
options=["setosa", "versicolor", "virginica"], multi=True, title="Parametrized species"
),
),
vm.Parameter(
targets=[
"p5-G-1.x",
# TODO: Uncomment the following target and see the magic :D
# "p5-F-1.",
],
selector=vm.RadioItems(options=["species", "sepal_width"], value="species", title="Simple X-axis parameter")
selector=vm.RadioItems(
options=["species", "sepal_width"], value="species", title="Simple X-axis parameter"
),
),
]
],
)


page_6 = vm.Page(
title="Page to test things out",
components=[
vm.Graph(
id="graph_dynamic",
figure=px.bar(data_frame="load_from_file", **BAR_CHART_CONF)
),
vm.Graph(id="graph_dynamic", figure=px.bar(data_frame="load_from_file", **BAR_CHART_CONF)),
vm.Graph(
id="graph_static",
figure=px.scatter(data_frame=px.data.iris(), **SCATTER_CHART_CONF),
)
),
],
controls=[
vm.Filter(
id="filter_container_id",
column=FILTER_COLUMN,
targets=["graph_dynamic"],
# targets=["graph_static"],

# selector=vm.Dropdown(id="filter_id"),
# selector=vm.Dropdown(id="filter_id", value=["setosa"]),

# selector=vm.Checklist(id="filter_id"),
# selector=vm.Checklist(id="filter_id", value=["setosa"]),

# TODO-BUG: vm.Dropdown(multi=False) Doesn't work if value is cleared. The persistence storage become
# "null" and our placeholder component dmc.DateRangePicker can't process null value. It expects a value or
# a list of values.
# SOLUTION -> Create the "Universal Vizro placeholder component".
# TEMPORARY SOLUTION -> set clearable=False for the dynamic Dropdown(multi=False)
# selector=vm.Dropdown(id="filter_id", multi=False), ->
# selector=vm.Dropdown(id="filter_id", multi=False, value="setosa"),

# selector=vm.RadioItems(id="filter_id"),
# selector=vm.RadioItems(id="filter_id", value="setosa"),

# selector=vm.Slider(id="filter_id"),
# selector=vm.Slider(id="filter_id", value=5),

# selector=vm.RangeSlider(id="filter_id"),
# selector=vm.RangeSlider(id="filter_id", value=[5, 7]),
),
),
vm.Parameter(
id="parameter_x",
targets=["graph_dynamic.x",],
targets=[
"graph_dynamic.x",
],
selector=vm.Dropdown(
options=["species", "sepal_width"],
value="species",
multi=False,
)
),
),
],
)
Expand Down
3 changes: 2 additions & 1 deletion vizro-core/src/vizro/actions/_actions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from vizro.models.types import MultiValueType, SelectorType, SingleValueType

if TYPE_CHECKING:
from vizro.models import Action, VizroBaseModel, Filter
from vizro.models import Action, VizroBaseModel

ValidatedNoneValueType = Union[SingleValueType, MultiValueType, None, list[None]]

Expand Down Expand Up @@ -162,6 +162,7 @@ def _update_nested_figure_properties(
current_property[keys[-1]] = value
return figure_config


def _get_parametrized_config(
ctds_parameter: list[CallbackTriggerDict], target: ModelID, data_frame: bool
) -> dict[str, Any]:
Expand Down
1 change: 0 additions & 1 deletion vizro-core/src/vizro/actions/_on_page_load_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from vizro.actions._actions_utils import _get_modified_page_figures
from vizro.managers._model_manager import ModelID
from vizro.models.types import capture
from vizro.managers import model_manager, data_manager


@capture("action")
Expand Down
8 changes: 2 additions & 6 deletions vizro-core/src/vizro/models/_components/form/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,8 @@ def _build_dynamic_placeholder(self):
children=[
dbc.Label(self.title, html_for=self.id) if self.title else None,
dmc.DateRangePicker(
id=self.id,
value=self.value,
persistence=True,
persistence_type="session",
style={'opacity': 0}
)
id=self.id, value=self.value, persistence=True, persistence_type="session", style={"opacity": 0}
),
]
)

Expand Down
4 changes: 2 additions & 2 deletions vizro-core/src/vizro/models/_components/form/range_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _build_static(self, current_value=None, new_min=None, new_max=None, **kwargs
persistence_type="session",
className="slider-text-input-field",
),
dcc.Store(id=f"{self.id}_input_store", storage_type="session")
dcc.Store(id=f"{self.id}_input_store", storage_type="session"),
],
className="slider-text-input-container",
),
Expand Down Expand Up @@ -152,4 +152,4 @@ def _build_dynamic_placeholder(self):
def build(self):
# We don't have to implement _build_dynamic_placeholder, _build_static here. It's possible to: if dynamic and
# self.value is None -> set self.value + return standard build (static), but let's align it with the Dropdown.
return self._build_dynamic_placeholder() if self._dynamic else self._build_static()
return self._build_dynamic_placeholder() if self._dynamic else self._build_static()
2 changes: 1 addition & 1 deletion vizro-core/src/vizro/models/_components/form/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _build_static(self, current_value=None, new_min=None, new_max=None, **kwargs
persistence_type="session",
className="slider-text-input-field",
),
dcc.Store(id=f"{self.id}_input_store", storage_type="session")
dcc.Store(id=f"{self.id}_input_store", storage_type="session"),
],
className="slider-text-input-container",
),
Expand Down
6 changes: 2 additions & 4 deletions vizro-core/src/vizro/models/_controls/filter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from dash import dcc, html

from typing import Any, Literal, Union

import numpy as np
import pandas as pd
from dash import dcc
from pandas.api.types import is_datetime64_any_dtype, is_numeric_dtype

from vizro.managers._data_manager import DataSourceName
Expand All @@ -18,6 +17,7 @@
from vizro._constants import FILTER_ACTION_PREFIX
from vizro.actions import _filter
from vizro.managers import data_manager, model_manager
from vizro.managers._data_manager import _DynamicData
from vizro.managers._model_manager import ModelID
from vizro.models import Action, VizroBaseModel
from vizro.models._components.form import (
Expand All @@ -30,8 +30,6 @@
)
from vizro.models._models_utils import _log_call
from vizro.models.types import MultiValueType, SelectorType
from vizro.models._components.form._form_utils import get_options_and_default
from vizro.managers._data_manager import _DynamicData

# Ideally we might define these as NumericalSelectorType = Union[RangeSlider, Slider] etc., but that will not work
# with isinstance checks.
Expand Down
28 changes: 21 additions & 7 deletions vizro-core/src/vizro/static/js/models/range_slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ function update_range_slider_values(
}
return [start, end, [start, end], [start, end]];

// slider component is the trigger
// slider component is the trigger
} else if (trigger_id === self_data["id"]) {
return [slider[0], slider[1], slider, slider];

// on_page_load is the trigger
// on_page_load is the trigger
} else {
if (input_store === null) {
return [dash_clientside.no_update, dash_clientside.no_update, dash_clientside.no_update, slider];
}
else {
if (slider[0] === start && input_store[0] === start && slider[1] === end && input_store[1] === end){
return [
dash_clientside.no_update,
dash_clientside.no_update,
dash_clientside.no_update,
slider,
];
} else {
if (
slider[0] === start &&
input_store[0] === start &&
slider[1] === end &&
input_store[1] === end
) {
// To prevent filter_action to be triggered after on_page_load
return [dash_clientside.no_update, dash_clientside.no_update, dash_clientside.no_update, dash_clientside.no_update];
return [
dash_clientside.no_update,
dash_clientside.no_update,
dash_clientside.no_update,
dash_clientside.no_update,
];
}
return [input_store[0], input_store[1], input_store, input_store];
}
Expand Down
14 changes: 8 additions & 6 deletions vizro-core/src/vizro/static/js/models/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ function update_slider_values(start, slider, input_store, self_data) {
}
return [start, start, start];

// slider component is the trigger
// slider component is the trigger
} else if (trigger_id === self_data["id"]) {
return [slider, slider, slider];

// on_page_load is the trigger
// on_page_load is the trigger
} else {
if (input_store === null) {
return [dash_clientside.no_update, dash_clientside.no_update, slider];
}
else {
} else {
if (slider === start && start === input_store) {
// To prevent filter_action to be triggered after on_page_load
return [dash_clientside.no_update, dash_clientside.no_update, dash_clientside.no_update];
return [
dash_clientside.no_update,
dash_clientside.no_update,
dash_clientside.no_update,
];
}
return [input_store, input_store, input_store];
}
}
}


window.dash_clientside = {
...window.dash_clientside,
slider: { update_slider_values: update_slider_values },
Expand Down

0 comments on commit 472f7a8

Please sign in to comment.