Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions examples/serve/panels-demo/demo_panels/panel_histo2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def render_panel(
},
)

progress = CircularProgress(
id="plot_progress",
hidden=True,
size=28,
style={"margin": "2px 0"},
)

control_bar = Box(
children=[place_text, controls],
style={
Expand Down Expand Up @@ -127,6 +134,7 @@ def render_panel(
children=[
instructions,
control_bar,
progress,
plot,
error_message,
],
Expand All @@ -152,6 +160,7 @@ def render_panel(
State("@app", "selectedTimeLabel"),
Input("button", "clicked"),
Output("plot", "chart"),
Output("plot_progress", "hidden"),
)
def update_plot(
ctx: Context,
Expand All @@ -161,7 +170,7 @@ def update_plot(
var_2_name: str | None = None,
time_label: float | None = None,
_clicked: bool | None = None, # trigger, will always be True
) -> alt.Chart | None:
) -> tuple[alt.Chart | None, bool]:
global error_message
dataset = get_dataset(ctx, dataset_id)

Expand All @@ -181,12 +190,12 @@ def update_plot(

if place_geometry is None or isinstance(place_geometry, shapely.geometry.Point):
error_message = "Selected geometry must cover an area."
return None
return None, True

dataset = mask_dataset_by_geometry(dataset, place_geometry)
if dataset is None:
error_message = "Selected geometry produces empty subset"
return None
return None, True

var_1_data: np.ndarray = dataset[var_1_name].values.ravel()
var_2_data: np.ndarray = dataset[var_2_name].values.ravel()
Expand Down Expand Up @@ -268,7 +277,7 @@ def update_plot(
height="container",
)
error_message = ""
return chart
return chart, True


@panel.callback(
Expand Down
34 changes: 25 additions & 9 deletions examples/serve/panels-demo/demo_panels/panel_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from chartlets import Component, Input, State, Output
from chartlets.components import (
Box,
CircularProgress,
Typography,
VegaChart,
Radio,
Expand Down Expand Up @@ -79,6 +80,13 @@ def render_panel(
),
)

progress = CircularProgress(
id="plot_progress",
hidden=True,
size=28,
style={"margin": "2px 0"},
)

control_bar = Box(
children=[place_text, exploration_radio_group],
style={
Expand Down Expand Up @@ -117,6 +125,7 @@ def render_panel(
children=[
instructions,
control_bar,
progress,
error_message,
plot,
],
Expand Down Expand Up @@ -157,10 +166,15 @@ def get_spectra(
result = pd.DataFrame()

for place in places:
i = (dataset_place.name_ref == place).argmax().item()
place_index_by_name = {
place_name: idx for idx, place_name in enumerate(place_group["name"])
}

place_index = place_index_by_name.get(place)

selected_values = (
dataset_place.drop_vars("geometry_ref")
.sel(idx=i)
.sel(idx=place_index)
.compute()
.to_dict()["data_vars"]
)
Expand Down Expand Up @@ -214,6 +228,7 @@ def update_text(
Input("exploration_radio_group", "value"),
State("plot", "chart"),
Output("plot", "chart"),
Output("plot_progress", "hidden"),
Output("error_message", "children"),
Output("@container", "spectrum_list"),
Output("@container", "previous_mode"),
Expand All @@ -228,9 +243,9 @@ def update_plot(
previous_mode: str | None = None,
exploration_radio_group: str | None = None,
current_chart: alt.Chart | None = None,
) -> tuple[alt.Chart | None, str, list, str]:
) -> tuple[alt.Chart | None, bool, str, list, str]:
if exploration_radio_group is None:
return None, "Missing exploration mode choice", spectrum_list, previous_mode
return None, True, "Missing exploration mode choice", spectrum_list, previous_mode

dataset = get_dataset(ctx, dataset_id)
has_point = any(
Expand All @@ -240,15 +255,16 @@ def update_plot(
)

if dataset is None:
return None, "Missing dataset selection", spectrum_list, exploration_radio_group
return None, True, "Missing dataset selection", spectrum_list, exploration_radio_group
elif not place_group or not has_point:
return None, "Missing point selection", spectrum_list, exploration_radio_group
return None, True, "Missing point selection", spectrum_list, exploration_radio_group

label = find_selected_point_label(place_group, place_geo)

if label is None:
return (
None,
True,
"There is no label for the selected point or no point is selected",
spectrum_list,
previous_mode,
Expand All @@ -269,14 +285,14 @@ def update_plot(
]
)
else:
return None, "Selected geometry must be a point", spectrum_list, previous_mode
return None, True, "Selected geometry must be a point", spectrum_list, previous_mode

place_group_geodf["time"] = pd.to_datetime(time_label).tz_localize(None)
places_select = [label]
new_spectrum_data = get_spectra(dataset, place_group_geodf, places_select)

if new_spectrum_data is None or new_spectrum_data.empty:
return None, "No reflectances found in Variables", spectrum_list, previous_mode
return None, True, "No reflectances found in Variables", spectrum_list, previous_mode

new_spectrum_data["Legend"] = new_spectrum_data["places"] + ": " + time_label

Expand Down Expand Up @@ -320,7 +336,7 @@ def update_plot(
)

new_chart = create_chart_from_data(updated_data)
return new_chart, "", spectrum_list, exploration_radio_group
return new_chart, True, "", spectrum_list, exploration_radio_group


def find_selected_point_label(
Expand Down
Loading