Skip to content

Commit 9bc4742

Browse files
committed
Add num and round parameters to FloatLogSlider and FloatRangeSlider
1 parent fd221a4 commit 9bc4742

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

docs/source/examples/Widget List.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
},
8686
"source": [
8787
"### FloatSlider\n",
88-
"- In addition to the `step` parameter, a `FloatSlider` also accepts a `num` parameter, which specifies the number of values in the `[min, max]` interval. `num` takes precedence over `step`, and ensures that the `max` value is always included (which might not be the case due to floating point precision issues if the `step` parameter is used).\n",
88+
"- In addition to the `step` parameter, float sliders (`FloatSlider`, `FloatLogSlider` and `FloatRangeSlider`) also accept a `num` parameter, which specifies the number of values in the `[min, max]` interval. `num` takes precedence over `step`, and ensures that the `max` value is always included (which might not be the case due to floating point precision issues if the `step` parameter is used).\n",
8989
"- A `round` parameter allows to round values that are set from the Python side to the closest value in the set of possible values. If `round` is `True`, values will be silently rounded (default value is `False`)."
9090
]
9191
},

ipywidgets/widgets/widget_float.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class FloatSlider(_BoundedFloat):
155155
step of the trackbar
156156
num : integer
157157
number of values in the [min, max] interval, takes precedence over
158-
the step parameter.
158+
the step parameter
159159
round : bool
160160
default is False, round the value set from Python to the closest value
161161
in the interval
@@ -229,6 +229,12 @@ class FloatLogSlider(_BoundedLogFloat):
229229
maximal position of the slider in log scale, i.e., actual maximum is base ** max
230230
step : float
231231
step of the trackbar, denotes steps for the exponent, not the actual value
232+
num : integer
233+
number of values in the [min, max] interval, takes precedence over
234+
the step parameter
235+
round : bool
236+
default is False, round the value set from Python to the closest value
237+
in the interval
232238
description : str
233239
name of the slider
234240
orientation : {'horizontal', 'vertical'}
@@ -243,6 +249,8 @@ class FloatLogSlider(_BoundedLogFloat):
243249
_view_name = Unicode('FloatLogSliderView').tag(sync=True)
244250
_model_name = Unicode('FloatLogSliderModel').tag(sync=True)
245251
step = CFloat(0.1, allow_none=True, help="Minimum step in the exponent to increment the value").tag(sync=True)
252+
num = CInt(help="Number of values in the [min, max] interval").tag(sync=True)
253+
round = Bool(False, help="Round the value set from Python to the closest value in the interval").tag(sync=True)
246254
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
247255
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
248256
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
@@ -254,6 +262,33 @@ class FloatLogSlider(_BoundedLogFloat):
254262

255263
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)
256264

265+
@validate('value')
266+
def _validate_value(self, proposal):
267+
value = proposal['value']
268+
if not self.round:
269+
return value
270+
# round value to closest one in interval
271+
values = [self.min]
272+
while values[-1] <= self.max:
273+
values.append(values[-1] + self.step)
274+
rounded_value = min(values, key=lambda x:abs(x-value))
275+
return rounded_value
276+
277+
@validate('num')
278+
def _validate_num(self, proposal):
279+
# step value is computed from num
280+
# num is not used in the front-end
281+
num = proposal['value']
282+
if self.min == self.max:
283+
if num != 1:
284+
raise TraitError('num must be 1 because min == max')
285+
self.step = 0
286+
elif num <= 1:
287+
raise TraitError('num must be greater than 1')
288+
else:
289+
self.step = (self.max - self.min) / (num - 1)
290+
return num
291+
257292

258293
@register
259294
class FloatProgress(_BoundedFloat):
@@ -374,7 +409,9 @@ class FloatRangeSlider(_BoundedFloatRange):
374409
step of the trackbar
375410
num : integer
376411
number of values in the [min, max] interval, takes precedence over
377-
the step parameter.
412+
the step parameter
413+
round : bool
414+
default is False, round the value set from Python to the closest value
378415
description : str
379416
name of the slider
380417
orientation : {'horizontal', 'vertical'}
@@ -390,6 +427,7 @@ class FloatRangeSlider(_BoundedFloatRange):
390427
_model_name = Unicode('FloatRangeSliderModel').tag(sync=True)
391428
step = CFloat(0.1, allow_none=True, help="Minimum step to increment the value").tag(sync=True)
392429
num = CInt(help="Number of values in the [min, max] interval").tag(sync=True)
430+
round = Bool(False, help="Round the value set from Python to the closest value in the interval").tag(sync=True)
393431
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
394432
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
395433
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
@@ -400,6 +438,18 @@ class FloatRangeSlider(_BoundedFloatRange):
400438

401439
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)
402440

441+
@validate('value')
442+
def _validate_value(self, proposal):
443+
value = proposal['value']
444+
if not self.round:
445+
return value
446+
# round value to closest one in interval
447+
values = [self.min]
448+
while values[-1] <= self.max:
449+
values.append(values[-1] + self.step)
450+
rounded_value = min(values, key=lambda x:abs(x-value))
451+
return rounded_value
452+
403453
@validate('num')
404454
def _validate_num(self, proposal):
405455
# step value is computed from num

packages/schema/jupyterwidgetmodels.latest.md

+3
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,11 @@ Attribute | Type | Default | Help
461461
`layout` | reference to Layout widget | reference to new instance |
462462
`max` | number (float) | `4.0` | Max value for the exponent
463463
`min` | number (float) | `0.0` | Min value for the exponent
464+
`num` | number (integer) | `0` | Number of values in the [min, max] interval
464465
`orientation` | string (one of `'horizontal'`, `'vertical'`) | `'horizontal'` | Vertical or horizontal.
465466
`readout` | boolean | `true` | Display the current value of the slider next to it.
466467
`readout_format` | string | `'.3g'` | Format for the readout
468+
`round` | boolean | `false` | Round the value set from Python to the closest value in the interval
467469
`step` | `null` or number (float) | `0.1` | Minimum step in the exponent to increment the value
468470
`style` | reference to SliderStyle widget | reference to new instance |
469471
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
@@ -513,6 +515,7 @@ Attribute | Type | Default | Help
513515
`orientation` | string (one of `'horizontal'`, `'vertical'`) | `'horizontal'` | Vertical or horizontal.
514516
`readout` | boolean | `true` | Display the current value of the slider next to it.
515517
`readout_format` | string | `'.2f'` | Format for the readout
518+
`round` | boolean | `false` | Round the value set from Python to the closest value in the interval
516519
`step` | `null` or number (float) | `0.1` | Minimum step to increment the value
517520
`style` | reference to SliderStyle widget | reference to new instance |
518521
`tabbable` | `null` or boolean | `null` | Is widget tabbable?

0 commit comments

Comments
 (0)