From x.com
I love panel! Please show me (for example) how (in python) to create, say, a new kind of slider w/buttons to increment/decrement by ±1, and where I can hold down a control key to slide it at 1/100 the normal rate. This kind of thing is vital for data visualization apps. And with a text box that reflects the current value, and where I can type in a precise value. Mathematica does this.
import param
import holoviews as hv
import panel as pn
pn.extension()
class CustomSliderInput(pn.custom.PyComponent):
value = param.Number(default=5)
min = param.Number(default=0, readonly=True)
max = param.Number(default=10, readonly=True)
step = param.Number(default=0.1)
show_step = param.Boolean(default=False, label="")
switch = param.Boolean(default=False, label="")
def __init__(self, **params):
super().__init__(**params)
self._switch_icon = pn.widgets.ToggleIcon.from_param(
self.param.switch,
icon="exposure-plus-1",
active_icon="adjustments-horizontal",
margin=0
)
self._show_icon = pn.widgets.ToggleIcon.from_param(
self.param.show_step,
icon="eye",
active_icon="eye-off",
margin=0
)
self._placeholder = pn.pane.Placeholder()
self._value_input = pn.widgets.FloatInput(
value=self.param.value,
start=self.param.min,
end=self.param.max,
step=self.param.step,
)
self._value_slider = pn.widgets.FloatSlider.from_param(
self._value_input.param.value,
start=self.param.min,
end=self.param.max,
step=self.param.step,
)
self._step_slider = pn.widgets.FloatSlider.from_param(
self.param.step, start=0.001, step=0.001, visible=self.param.show_step
)
self.param.trigger("switch")
@param.depends("switch", watch=True)
def _swap_widgets(self):
if self.switch:
self._placeholder.update(self._value_input)
else:
self._placeholder.update(pn.Column(self._value_slider, self._step_slider))
def __panel__(self):
return pn.Column(self._placeholder, pn.Row(self._switch_icon, self._show_icon, margin=(1, 10)))
pn.serve(CustomSliderInput())