Skip to content

Commit

Permalink
fix: Turn bool values into List[str] | None to make pyvuetify work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lundez committed Sep 16, 2023
1 parent 87f57ef commit 97d5bf2
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions solara/components/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import ipyvue
import ipyvuetify
import numpy as np
import reacton.core
import traitlets
from typing_extensions import Literal
Expand Down Expand Up @@ -66,9 +67,7 @@ def Page():
def set_value_cast(value):
reactive_value.value = int(value)

if tick_labels == "end_points":
num_repeats = int(math.ceil((max - min) / step)) - 1
tick_labels = [str(min), *([""] * num_repeats), str(max)]
tick_labels = _produce_tick_labels(tick_labels, min, max, step)

return rv.Slider(
v_model=reactive_value.value,
Expand Down Expand Up @@ -135,9 +134,7 @@ def set_value_cast(value):
v1, v2 = value
reactive_value.set((int(v1), int(v2)))

if tick_labels == "end_points":
num_repeats = int(math.ceil((max - min) / step)) - 1
tick_labels = [str(min), *([""] * num_repeats), str(max)]
tick_labels = _produce_tick_labels(tick_labels, min, max, step)

return cast(
reacton.core.ValueElement[ipyvuetify.RangeSlider, Tuple[int, int]],
Expand Down Expand Up @@ -206,9 +203,7 @@ def Page():
def set_value_cast(value):
reactive_value.set(float(value))

if tick_labels == "end_points":
num_repeats = int(math.ceil((max - min) / step)) - 1
tick_labels = [str(min), *([""] * num_repeats), str(max)]
tick_labels = _produce_tick_labels(tick_labels, min, max, step)

return rv.Slider(
v_model=reactive_value.value,
Expand Down Expand Up @@ -275,9 +270,7 @@ def set_value_cast(value):
v1, v2 = value
reactive_value.set((float(v1), float(v2)))

if tick_labels == "end_points":
num_repeats = int(math.ceil((max - min) / step)) - 1
tick_labels = [str(min), *([""] * num_repeats), str(max)]
tick_labels = _produce_tick_labels(tick_labels, min, max, step)

return cast(
reacton.core.ValueElement[ipyvuetify.RangeSlider, Tuple[float, float]],
Expand Down Expand Up @@ -425,6 +418,18 @@ def set_value_cast(value):
return DateSliderWidget.element(label=label, min=dt_min, days=days, on_value=set_value_cast, value=days_value, disabled=disabled)


def _produce_tick_labels(tick_labels: Union[List[str], Literal["end_points"], bool], min: float, max: float, step: float) -> Optional[List[str]]:
if tick_labels == "end_points":
num_repeats = int(math.ceil((max - min) / step)) - 1
tick_labels = [str(min), *([""] * num_repeats), str(max)]
elif tick_labels is False:
tick_labels = None
elif tick_labels is True:
tick_labels = list(map(str, np.arange(min, max, step=step)))

return tick_labels


FloatSlider = SliderFloat
IntSlider = SliderInt
ValueSlider = SliderValue
Expand Down

0 comments on commit 97d5bf2

Please sign in to comment.