##// END OF EJS Templates
move DeprecatedClass to widgets, where it's used...
move DeprecatedClass to widgets, where it's used avoids need to add warn to genutils

File last commit:

r21081:5104f42c
r21081:5104f42c
Show More
widget_float.py
287 lines | 10.1 KiB | text/x-python | PythonLexer
Sylvain Corlay
Hold validation with context manager and validate slider
r20950 """Float class.
Jonathan Frederic
Cleaned up Python widget code.
r14283
Represents an unbounded float using a widget.
"""
Min RK
move DeprecatedClass to widgets, where it's used...
r21081
# Copyright (c) IPython Development Team.
Jonathan Frederic
Cleaned up Python widget code.
r14283 # Distributed under the terms of the Modified BSD License.
Sylvain Corlay
registering core widgets
r18531 from .widget import DOMWidget, register
Sylvain Corlay
Move color trait type to the widget package
r20797 from .trait_types import Color
Sylvain Corlay
Hold validation with context manager and validate slider
r20950 from IPython.utils.traitlets import (Unicode, CFloat, Bool, CaselessStrEnum,
Tuple, TraitError)
Min RK
move DeprecatedClass to widgets, where it's used...
r21081 from .deprecated import DeprecatedClass
Jonathan Frederic
Added float widget
r14265
Jonathan Frederic
Renamed *Widget to *,...
r17598 class _Float(DOMWidget):
Sylvain Corlay
Not using _**_changed handles anymore
r20227 value = CFloat(0.0, help="Float value", sync=True)
Jonathan Frederic
sync=True isntead of a keys list
r14588 disabled = Bool(False, help="Enable or disable user changes", sync=True)
description = Unicode(help="Description of the value this widget represents", sync=True)
Jonathan Frederic
Create base widget classes
r14670
Jason Goad
added value positional argument to applicable widgets
r19621 def __init__(self, value=None, **kwargs):
if value is not None:
kwargs['value'] = value
super(_Float, self).__init__(**kwargs)
Jonathan Frederic
Create base widget classes
r14670
Sylvain Corlay
Hold validation with context manager and validate slider
r20950
Jonathan Frederic
Renamed *Widget to *,...
r17598 class _BoundedFloat(_Float):
Jonathan Frederic
Create base widget classes
r14670 max = CFloat(100.0, help="Max value", sync=True)
min = CFloat(0.0, help="Min value", sync=True)
Sylvain Corlay
Hold validation with context manager and validate slider
r20950 step = CFloat(0.1, help="Minimum step to increment the value (ignored by some views)", sync=True)
Jonathan Frederic
Create base widget classes
r14670
def __init__(self, *pargs, **kwargs):
"""Constructor"""
Jason Goad
added value positional argument to applicable widgets
r19621 super(_BoundedFloat, self).__init__(*pargs, **kwargs)
Sylvain Corlay
Hold validation with context manager and validate slider
r20950
def _value_validate(self, value, trait):
"""Cap and floor value"""
if self.min > value or self.max < value:
value = min(max(value, self.min), self.max)
return value
def _min_validate(self, min, trait):
"""Enforce min <= value <= max"""
if min > self.max:
raise TraitError("Setting min > max")
if min > self.value:
self.value = min
return min
def _max_validate(self, max, trait):
"""Enforce min <= value <= max"""
if max < self.min:
raise TraitError("setting max < min")
if max < self.value:
self.value = max
return max
Jonathan Frederic
Validation on the python side
r20224
Jonathan Frederic
Create base widget classes
r14670
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.FloatText')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class FloatText(_Float):
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 """ Displays a float value within a textbox. For a textbox in
which the value must be within a specific range, use BoundedFloatText.
Parameters
----------
value : float
value displayed
description : str
Sylvain Corlay
Hold validation with context manager and validate slider
r20950 description displayed next to the text box
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 color : str Unicode color code (eg. '#C13535'), optional
Sylvain Corlay
Hold validation with context manager and validate slider
r20950 color of the value displayed
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 """
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatTextView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.BoundedFloatText')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class BoundedFloatText(_BoundedFloat):
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 """ Displays a float value within a textbox. Value must be within the range specified.
For a textbox in which the value doesn't need to be within a specific range, use FloatText.
Parameters
----------
value : float
value displayed
min : float
minimal value of the range of possible values displayed
max : float
maximal value of the range of possible values displayed
description : str
description displayed next to the textbox
color : str Unicode color code (eg. '#C13535'), optional
color of the value displayed
"""
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatTextView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.FloatSlider')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class FloatSlider(_BoundedFloat):
Silvia
Add docstring documentation for widget_float
r19104 """ Slider/trackbar of floating values with the specified range.
Parameters
----------
value : float
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 position of the slider
Silvia
Add docstring documentation for widget_float
r19104 min : float
minimal position of the slider
max : float
maximal position of the slider
step : float
step of the trackbar
description : str
name of the slider
orientation : {'vertical', 'horizontal}, optional
default is horizontal
readout : {True, False}, optional
default is True, display the current value of the slider next to it
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 slider_color : str Unicode color code (eg. '#C13535'), optional
color of the slider
color : str Unicode color code (eg. '#C13535'), optional
color of the value displayed (if readout == True)
Silvia
Add docstring documentation for widget_float
r19104 """
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatSliderView', sync=True)
Jonathan Frederic
Added bootstrap3 progress bar classes
r17729 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='horizontal', help="Vertical or horizontal.", sync=True)
Gordon Ball
Change `range` trait to `_range`
r17703 _range = Bool(False, help="Display a range selector", sync=True)
MinRK
set readout=True as default on SliderWidgets
r15159 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
Sylvain Corlay
Using the Color trait type for styling widgets
r20796 slider_color = Color(None, allow_none=True, sync=True)
Jonathan Frederic
Create base widget classes
r14670
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.FloatProgress')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class FloatProgress(_BoundedFloat):
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 """ Displays a progress bar.
Silvia
Add docstring documentation for widget_float
r19104
Parameters
-----------
value : float
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 position within the range of the progress bar
Silvia
Add docstring documentation for widget_float
r19104 min : float
minimal position of the slider
max : float
maximal position of the slider
step : float
step of the progress bar
description : str
name of the progress bar
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 bar_style: {'success', 'info', 'warning', 'danger', ''}, optional
color of the progress bar, default is '' (blue)
colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red
Silvia
Add docstring documentation for widget_float
r19104 """
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('ProgressView', sync=True)
Gordon Ball
Add float implementation of range widget
r17060
Jonathan Frederic
Added bootstrap3 progress bar classes
r17729 bar_style = CaselessStrEnum(
values=['success', 'info', 'warning', 'danger', ''],
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the progess bar.""")
Gordon Ball
Merge master
r17698 class _FloatRange(_Float):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True)
lower = CFloat(0.0, help="Lower bound", sync=False)
upper = CFloat(1.0, help="Upper bound", sync=False)
def __init__(self, *pargs, **kwargs):
value_given = 'value' in kwargs
Gordon Ball
Clean up validation in __init__
r17705 lower_given = 'lower' in kwargs
upper_given = 'upper' in kwargs
if value_given and (lower_given or upper_given):
raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
if lower_given != upper_given:
raise ValueError("Must specify both 'lower' and 'upper' for range widget")
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682
DOMWidget.__init__(self, *pargs, **kwargs)
# ensure the traits match, preferring whichever (if any) was given in kwargs
if value_given:
self.lower, self.upper = self.value
else:
self.value = (self.lower, self.upper)
self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
def _validate(self, name, old, new):
if name == 'value':
self.lower, self.upper = min(new), max(new)
elif name == 'lower':
self.value = (new, self.value[1])
elif name == 'upper':
self.value = (self.value[0], new)
Gordon Ball
Add float implementation of range widget
r17060
Gordon Ball
Merge master
r17698 class _BoundedFloatRange(_FloatRange):
Gordon Ball
Add float implementation of range widget
r17060 step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
max = CFloat(100.0, help="Max value", sync=True)
min = CFloat(0.0, help="Min value", sync=True)
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682
Gordon Ball
Add float implementation of range widget
r17060 def __init__(self, *pargs, **kwargs):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs
Gordon Ball
Merge master
r17698 _FloatRange.__init__(self, *pargs, **kwargs)
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682
# ensure a minimal amount of sanity
if self.min > self.max:
raise ValueError("min must be <= max")
Gordon Ball
Clean up validation in __init__
r17705 if any_value_given:
# if a value was given, clamp it within (min, max)
self._validate("value", None, self.value)
else:
# otherwise, set it to 25-75% to avoid the handles overlapping
Gordon Ball
Default to 25-75% of min-max if no value is set instead of 0-1
r17591 self.value = (0.75*self.min + 0.25*self.max,
0.25*self.min + 0.75*self.max)
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 # callback already set for 'value', 'lower', 'upper'
self.on_trait_change(self._validate, ['min', 'max'])
Gordon Ball
Add float implementation of range widget
r17060
def _validate(self, name, old, new):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 if name == "min":
if new > self.max:
raise ValueError("setting min > max")
self.min = new
elif name == "max":
if new < self.min:
raise ValueError("setting max < min")
self.max = new
low, high = self.value
Gordon Ball
Add float implementation of range widget
r17060 if name == "value":
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 low, high = min(new), max(new)
elif name == "upper":
if new < self.lower:
raise ValueError("setting upper < lower")
high = new
elif name == "lower":
if new > self.upper:
raise ValueError("setting lower > upper")
low = new
low = max(self.min, min(low, self.max))
high = min(self.max, max(high, self.min))
# determine the order in which we should update the
# lower, upper traits to avoid a temporary inverted overlap
lower_first = high < self.lower
self.value = (low, high)
if lower_first:
self.lower = low
self.upper = high
Gordon Ball
Fix validate logic if min/max are changed
r17127 else:
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 self.upper = high
self.lower = low
Gordon Ball
Add float implementation of range widget
r17060
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.FloatRangeSlider')
Gordon Ball
Merge master
r17698 class FloatRangeSlider(_BoundedFloatRange):
silviav12
Added changes to dosctring documentation in widget_float.py (previous docstring written by me)....
r19113 """ Slider/trackbar for displaying a floating value range (within the specified range of values).
Parameters
----------
value : float tuple
range of the slider displayed
min : float
minimal position of the slider
max : float
maximal position of the slider
step : float
step of the trackbar
description : str
name of the slider
orientation : {'vertical', 'horizontal}, optional
default is horizontal
readout : {True, False}, optional
default is True, display the current value of the slider next to it
slider_color : str Unicode color code (eg. '#C13535'), optional
color of the slider
color : str Unicode color code (eg. '#C13535'), optional
color of the value displayed (if readout == True)
"""
Gordon Ball
Add float implementation of range widget
r17060 _view_name = Unicode('FloatSliderView', sync=True)
Jonathan Frederic
Fixed rebase bugs and other bugs.
r17731 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='horizontal', help="Vertical or horizontal.", sync=True)
Gordon Ball
Change `range` trait to `_range`
r17703 _range = Bool(True, help="Display a range selector", sync=True)
Gordon Ball
Add float implementation of range widget
r17060 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
Sylvain Corlay
Using the Color trait type for styling widgets
r20796 slider_color = Color(None, allow_none=True, sync=True)
Jonathan Frederic
Added some doc strings on the widgets....
r17602
# Remove in IPython 4.0
Jonathan Frederic
Renamed *Widget to *,...
r17598 FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget')
BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget')
FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget')
FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget')