##// END OF EJS Templates
Add docstring documentation for widget_float
Silvia -
Show More
@@ -1,184 +1,220 b''
1 """Float class.
1 """Float class.
2
2
3 Represents an unbounded float using a widget.
3 Represents an unbounded float using a widget.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget, register
16 from .widget import DOMWidget, register
17 from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple
17 from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple
18 from IPython.utils.warn import DeprecatedClass
18 from IPython.utils.warn import DeprecatedClass
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 class _Float(DOMWidget):
23 class _Float(DOMWidget):
24 value = CFloat(0.0, help="Float value", sync=True)
24 value = CFloat(0.0, help="Float value", sync=True)
25 disabled = Bool(False, help="Enable or disable user changes", sync=True)
25 disabled = Bool(False, help="Enable or disable user changes", sync=True)
26 description = Unicode(help="Description of the value this widget represents", sync=True)
26 description = Unicode(help="Description of the value this widget represents", sync=True)
27
27
28
28
29 class _BoundedFloat(_Float):
29 class _BoundedFloat(_Float):
30 max = CFloat(100.0, help="Max value", sync=True)
30 max = CFloat(100.0, help="Max value", sync=True)
31 min = CFloat(0.0, help="Min value", sync=True)
31 min = CFloat(0.0, help="Min value", sync=True)
32 step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
32 step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
33
33
34 def __init__(self, *pargs, **kwargs):
34 def __init__(self, *pargs, **kwargs):
35 """Constructor"""
35 """Constructor"""
36 DOMWidget.__init__(self, *pargs, **kwargs)
36 DOMWidget.__init__(self, *pargs, **kwargs)
37 self._validate('value', None, self.value)
37 self._validate('value', None, self.value)
38 self.on_trait_change(self._validate, ['value', 'min', 'max'])
38 self.on_trait_change(self._validate, ['value', 'min', 'max'])
39
39
40 def _validate(self, name, old, new):
40 def _validate(self, name, old, new):
41 """Validate value, max, min."""
41 """Validate value, max, min."""
42 if self.min > new or new > self.max:
42 if self.min > new or new > self.max:
43 self.value = min(max(new, self.min), self.max)
43 self.value = min(max(new, self.min), self.max)
44
44
45
45
46 @register('IPython.FloatText')
46 @register('IPython.FloatText')
47 class FloatText(_Float):
47 class FloatText(_Float):
48 _view_name = Unicode('FloatTextView', sync=True)
48 _view_name = Unicode('FloatTextView', sync=True)
49
49
50
50
51 @register('IPython.BoundedFloatText')
51 @register('IPython.BoundedFloatText')
52 class BoundedFloatText(_BoundedFloat):
52 class BoundedFloatText(_BoundedFloat):
53 _view_name = Unicode('FloatTextView', sync=True)
53 _view_name = Unicode('FloatTextView', sync=True)
54
54
55
55
56 @register('IPython.FloatSlider')
56 @register('IPython.FloatSlider')
57 class FloatSlider(_BoundedFloat):
57 class FloatSlider(_BoundedFloat):
58 """ Slider/trackbar of floating values with the specified range.
59
60 Parameters
61 ----------
62 value : float
63 initial position of the slider
64 min : float
65 minimal position of the slider
66 max : float
67 maximal position of the slider
68 step : float
69 step of the trackbar
70 description : str
71 name of the slider
72 orientation : {'vertical', 'horizontal}, optional
73 default is horizontal
74 readout : {True, False}, optional
75 default is True, display the current value of the slider next to it
76
77 """
58 _view_name = Unicode('FloatSliderView', sync=True)
78 _view_name = Unicode('FloatSliderView', sync=True)
59 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
79 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
60 default_value='horizontal',
80 default_value='horizontal',
61 help="Vertical or horizontal.", allow_none=False, sync=True)
81 help="Vertical or horizontal.", allow_none=False, sync=True)
62 _range = Bool(False, help="Display a range selector", sync=True)
82 _range = Bool(False, help="Display a range selector", sync=True)
63 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
83 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
64 slider_color = Unicode(sync=True)
84 slider_color = Unicode(sync=True)
65
85
66
86
67 @register('IPython.FloatProgress')
87 @register('IPython.FloatProgress')
68 class FloatProgress(_BoundedFloat):
88 class FloatProgress(_BoundedFloat):
89 """ Progress bar.
90
91 Parameters
92 -----------
93 value : float
94 current position within the range of the progress bar
95 min : float
96 minimal position of the slider
97 max : float
98 maximal position of the slider
99 step : float
100 step of the progress bar
101 description : str
102 name of the progress bar
103
104 """
69 _view_name = Unicode('ProgressView', sync=True)
105 _view_name = Unicode('ProgressView', sync=True)
70
106
71 bar_style = CaselessStrEnum(
107 bar_style = CaselessStrEnum(
72 values=['success', 'info', 'warning', 'danger', ''],
108 values=['success', 'info', 'warning', 'danger', ''],
73 default_value='', allow_none=True, sync=True, help="""Use a
109 default_value='', allow_none=True, sync=True, help="""Use a
74 predefined styling for the progess bar.""")
110 predefined styling for the progess bar.""")
75
111
76 class _FloatRange(_Float):
112 class _FloatRange(_Float):
77 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True)
113 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True)
78 lower = CFloat(0.0, help="Lower bound", sync=False)
114 lower = CFloat(0.0, help="Lower bound", sync=False)
79 upper = CFloat(1.0, help="Upper bound", sync=False)
115 upper = CFloat(1.0, help="Upper bound", sync=False)
80
116
81 def __init__(self, *pargs, **kwargs):
117 def __init__(self, *pargs, **kwargs):
82 value_given = 'value' in kwargs
118 value_given = 'value' in kwargs
83 lower_given = 'lower' in kwargs
119 lower_given = 'lower' in kwargs
84 upper_given = 'upper' in kwargs
120 upper_given = 'upper' in kwargs
85 if value_given and (lower_given or upper_given):
121 if value_given and (lower_given or upper_given):
86 raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
122 raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
87 if lower_given != upper_given:
123 if lower_given != upper_given:
88 raise ValueError("Must specify both 'lower' and 'upper' for range widget")
124 raise ValueError("Must specify both 'lower' and 'upper' for range widget")
89
125
90 DOMWidget.__init__(self, *pargs, **kwargs)
126 DOMWidget.__init__(self, *pargs, **kwargs)
91
127
92 # ensure the traits match, preferring whichever (if any) was given in kwargs
128 # ensure the traits match, preferring whichever (if any) was given in kwargs
93 if value_given:
129 if value_given:
94 self.lower, self.upper = self.value
130 self.lower, self.upper = self.value
95 else:
131 else:
96 self.value = (self.lower, self.upper)
132 self.value = (self.lower, self.upper)
97
133
98 self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
134 self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
99
135
100 def _validate(self, name, old, new):
136 def _validate(self, name, old, new):
101 if name == 'value':
137 if name == 'value':
102 self.lower, self.upper = min(new), max(new)
138 self.lower, self.upper = min(new), max(new)
103 elif name == 'lower':
139 elif name == 'lower':
104 self.value = (new, self.value[1])
140 self.value = (new, self.value[1])
105 elif name == 'upper':
141 elif name == 'upper':
106 self.value = (self.value[0], new)
142 self.value = (self.value[0], new)
107
143
108 class _BoundedFloatRange(_FloatRange):
144 class _BoundedFloatRange(_FloatRange):
109 step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
145 step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
110 max = CFloat(100.0, help="Max value", sync=True)
146 max = CFloat(100.0, help="Max value", sync=True)
111 min = CFloat(0.0, help="Min value", sync=True)
147 min = CFloat(0.0, help="Min value", sync=True)
112
148
113 def __init__(self, *pargs, **kwargs):
149 def __init__(self, *pargs, **kwargs):
114 any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs
150 any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs
115 _FloatRange.__init__(self, *pargs, **kwargs)
151 _FloatRange.__init__(self, *pargs, **kwargs)
116
152
117 # ensure a minimal amount of sanity
153 # ensure a minimal amount of sanity
118 if self.min > self.max:
154 if self.min > self.max:
119 raise ValueError("min must be <= max")
155 raise ValueError("min must be <= max")
120
156
121 if any_value_given:
157 if any_value_given:
122 # if a value was given, clamp it within (min, max)
158 # if a value was given, clamp it within (min, max)
123 self._validate("value", None, self.value)
159 self._validate("value", None, self.value)
124 else:
160 else:
125 # otherwise, set it to 25-75% to avoid the handles overlapping
161 # otherwise, set it to 25-75% to avoid the handles overlapping
126 self.value = (0.75*self.min + 0.25*self.max,
162 self.value = (0.75*self.min + 0.25*self.max,
127 0.25*self.min + 0.75*self.max)
163 0.25*self.min + 0.75*self.max)
128 # callback already set for 'value', 'lower', 'upper'
164 # callback already set for 'value', 'lower', 'upper'
129 self.on_trait_change(self._validate, ['min', 'max'])
165 self.on_trait_change(self._validate, ['min', 'max'])
130
166
131
167
132 def _validate(self, name, old, new):
168 def _validate(self, name, old, new):
133 if name == "min":
169 if name == "min":
134 if new > self.max:
170 if new > self.max:
135 raise ValueError("setting min > max")
171 raise ValueError("setting min > max")
136 self.min = new
172 self.min = new
137 elif name == "max":
173 elif name == "max":
138 if new < self.min:
174 if new < self.min:
139 raise ValueError("setting max < min")
175 raise ValueError("setting max < min")
140 self.max = new
176 self.max = new
141
177
142 low, high = self.value
178 low, high = self.value
143 if name == "value":
179 if name == "value":
144 low, high = min(new), max(new)
180 low, high = min(new), max(new)
145 elif name == "upper":
181 elif name == "upper":
146 if new < self.lower:
182 if new < self.lower:
147 raise ValueError("setting upper < lower")
183 raise ValueError("setting upper < lower")
148 high = new
184 high = new
149 elif name == "lower":
185 elif name == "lower":
150 if new > self.upper:
186 if new > self.upper:
151 raise ValueError("setting lower > upper")
187 raise ValueError("setting lower > upper")
152 low = new
188 low = new
153
189
154 low = max(self.min, min(low, self.max))
190 low = max(self.min, min(low, self.max))
155 high = min(self.max, max(high, self.min))
191 high = min(self.max, max(high, self.min))
156
192
157 # determine the order in which we should update the
193 # determine the order in which we should update the
158 # lower, upper traits to avoid a temporary inverted overlap
194 # lower, upper traits to avoid a temporary inverted overlap
159 lower_first = high < self.lower
195 lower_first = high < self.lower
160
196
161 self.value = (low, high)
197 self.value = (low, high)
162 if lower_first:
198 if lower_first:
163 self.lower = low
199 self.lower = low
164 self.upper = high
200 self.upper = high
165 else:
201 else:
166 self.upper = high
202 self.upper = high
167 self.lower = low
203 self.lower = low
168
204
169
205
170 @register('IPython.FloatRangeSlider')
206 @register('IPython.FloatRangeSlider')
171 class FloatRangeSlider(_BoundedFloatRange):
207 class FloatRangeSlider(_BoundedFloatRange):
172 _view_name = Unicode('FloatSliderView', sync=True)
208 _view_name = Unicode('FloatSliderView', sync=True)
173 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
209 orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
174 default_value='horizontal', allow_none=False,
210 default_value='horizontal', allow_none=False,
175 help="Vertical or horizontal.", sync=True)
211 help="Vertical or horizontal.", sync=True)
176 _range = Bool(True, help="Display a range selector", sync=True)
212 _range = Bool(True, help="Display a range selector", sync=True)
177 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
213 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
178 slider_color = Unicode(sync=True)
214 slider_color = Unicode(sync=True)
179
215
180 # Remove in IPython 4.0
216 # Remove in IPython 4.0
181 FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget')
217 FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget')
182 BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget')
218 BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget')
183 FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget')
219 FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget')
184 FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget')
220 FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget')
General Comments 0
You need to be logged in to leave comments. Login now