Skip to content

Commit

Permalink
value and value_threshold are now DateRange Parameters (#3515)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored May 16, 2022
1 parent 18e0f47 commit 435d539
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions panel/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ def _process_property_change(self, msg):
return msg


class DateRangeSlider(_RangeSliderBase):
class DateRangeSlider(_SliderBase):
"""
The DateRangeSlider widget allows selecting a date range using a
slider with two handles. Supports datetime.datetime, datetime.data
slider with two handles. Supports datetime.datetime, datetime.date
and np.datetime64 ranges.
Reference: https://panel.holoviz.org/reference/widgets/DateRangeSlider.html
Expand All @@ -594,8 +594,8 @@ class DateRangeSlider(_RangeSliderBase):
... )
"""

value = param.Tuple(default=(None, None), length=2, doc=
"""The selected range as a tuple of values. Updated when one of the handles is
value = param.DateRange(default=None, doc="""
The selected range as a tuple of values. Updated when one of the handles is
dragged. Supports datetime.datetime, datetime.date, and np.datetime64 ranges.""")

value_start = param.Date(default=None, readonly=True, doc="""
Expand Down Expand Up @@ -624,9 +624,35 @@ class DateRangeSlider(_RangeSliderBase):

_widget_type = _BkDateRangeSlider

def __init__(self, **params):
if 'value' not in params:
value_to_None = False
for attr in ['start', 'end']:
if params.get(attr, getattr(self, attr)):
continue
self.param.warning(
f'Parameter {attr!r} must be set for the widget to be rendered.'
)
value_to_None = True
if value_to_None:
params['value'] = None
else:
params['value'] = (params.get('start', self.start),
params.get('end', self.end))
if params['value'] is not None:
params['value_start'], params['value_end'] = params['value']
with edit_readonly(self):
super().__init__(**params)

@param.depends('value', watch=True)
def _sync_values(self):
vs, ve = self.value
with edit_readonly(self):
self.param.update(value_start=vs, value_end=ve)

def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if msg.get('value') == (None, None):
if msg.get('value', 'unchanged') is None:
del msg['value']
elif 'value' in msg:
v1, v2 = msg['value']
Expand All @@ -635,7 +661,7 @@ def _process_param_change(self, msg):
if isinstance(v2, dt.datetime):
v2 = datetime_as_utctimestamp(v2)
msg['value'] = (v1, v2)
if msg.get('value_throttled') == (None, None):
if msg.get('value_throttled', 'unchanged') is None:
del msg['value_throttled']
return msg

Expand Down

0 comments on commit 435d539

Please sign in to comment.