Show More
@@ -1,67 +1,71 | |||
|
1 | 1 | """Bool class. |
|
2 | 2 | |
|
3 | 3 | Represents a boolean using a widget. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from .widget import DOMWidget, register |
|
17 | 17 | from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum |
|
18 | 18 | from IPython.utils.warn import DeprecatedClass |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | class _Bool(DOMWidget): |
|
24 | 24 | """A base class for creating widgets that represent booleans.""" |
|
25 | 25 | value = Bool(False, help="Bool value", sync=True) |
|
26 | 26 | description = Unicode('', help="Description of the boolean (label).", sync=True) |
|
27 | 27 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) |
|
28 | 28 | |
|
29 | def __init__(self, value=None, **kwargs): | |
|
30 | if value is not None: | |
|
31 | kwargs['value'] = value | |
|
32 | super(_Bool, self).__init__(**kwargs) | |
|
29 | 33 | |
|
30 | 34 | @register('IPython.Checkbox') |
|
31 | 35 | class Checkbox(_Bool): |
|
32 | 36 | """Displays a boolean `value` in the form of a checkbox. |
|
33 | 37 | |
|
34 | 38 | Parameters |
|
35 | 39 | ---------- |
|
36 | 40 | value : {True,False} |
|
37 | 41 | value of the checkbox: True-checked, False-unchecked |
|
38 | 42 | description : str |
|
39 | 43 | description displayed next to the checkbox |
|
40 | 44 | """ |
|
41 | 45 | _view_name = Unicode('CheckboxView', sync=True) |
|
42 | 46 | |
|
43 | 47 | |
|
44 | 48 | @register('IPython.ToggleButton') |
|
45 | 49 | class ToggleButton(_Bool): |
|
46 | 50 | """Displays a boolean `value` in the form of a toggle button. |
|
47 | 51 | |
|
48 | 52 | Parameters |
|
49 | 53 | ---------- |
|
50 | 54 | value : {True,False} |
|
51 | 55 | value of the toggle button: True-pressed, False-unpressed |
|
52 | 56 | description : str |
|
53 | 57 | description displayed next to the button |
|
54 | 58 | """ |
|
55 | 59 | |
|
56 | 60 | _view_name = Unicode('ToggleButtonView', sync=True) |
|
57 | 61 | tooltip = Unicode(help="Tooltip caption of the toggle button.", sync=True) |
|
58 | 62 | |
|
59 | 63 | button_style = CaselessStrEnum( |
|
60 | 64 | values=['primary', 'success', 'info', 'warning', 'danger', ''], |
|
61 | 65 | default_value='', allow_none=True, sync=True, help="""Use a |
|
62 | 66 | predefined styling for the button.""") |
|
63 | 67 | |
|
64 | 68 | |
|
65 | 69 | # Remove in IPython 4.0 |
|
66 | 70 | CheckboxWidget = DeprecatedClass(Checkbox, 'CheckboxWidget') |
|
67 | 71 | ToggleButtonWidget = DeprecatedClass(ToggleButton, 'ToggleButtonWidget') |
@@ -1,276 +1,280 | |||
|
1 | 1 | """Float class. |
|
2 | 2 | |
|
3 | 3 | Represents an unbounded float using a widget. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from .widget import DOMWidget, register |
|
17 | 17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple |
|
18 | 18 | from IPython.utils.warn import DeprecatedClass |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | class _Float(DOMWidget): |
|
24 | 24 | value = CFloat(0.0, help="Float value", sync=True) |
|
25 | 25 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
26 | 26 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
27 | 27 | |
|
28 | def __init__(self, value=None, **kwargs): | |
|
29 | if value is not None: | |
|
30 | kwargs['value'] = value | |
|
31 | super(_Float, self).__init__(**kwargs) | |
|
28 | 32 | |
|
29 | 33 | class _BoundedFloat(_Float): |
|
30 | 34 | max = CFloat(100.0, help="Max value", sync=True) |
|
31 | 35 | min = CFloat(0.0, help="Min value", sync=True) |
|
32 | 36 | step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
33 | 37 | |
|
34 | 38 | def __init__(self, *pargs, **kwargs): |
|
35 | 39 | """Constructor""" |
|
36 |
|
|
|
40 | super(_BoundedFloat, self).__init__(*pargs, **kwargs) | |
|
37 | 41 | self._validate('value', None, self.value) |
|
38 | 42 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
39 | 43 | |
|
40 | 44 | def _validate(self, name, old, new): |
|
41 | 45 | """Validate value, max, min.""" |
|
42 | 46 | if self.min > new or new > self.max: |
|
43 | 47 | self.value = min(max(new, self.min), self.max) |
|
44 | 48 | |
|
45 | 49 | |
|
46 | 50 | @register('IPython.FloatText') |
|
47 | 51 | class FloatText(_Float): |
|
48 | 52 | """ Displays a float value within a textbox. For a textbox in |
|
49 | 53 | which the value must be within a specific range, use BoundedFloatText. |
|
50 | 54 | |
|
51 | 55 | Parameters |
|
52 | 56 | ---------- |
|
53 | 57 | value : float |
|
54 | 58 | value displayed |
|
55 | 59 | description : str |
|
56 | 60 | description displayed next to the textbox |
|
57 | 61 | color : str Unicode color code (eg. '#C13535'), optional |
|
58 | 62 | color of the value displayed |
|
59 | 63 | """ |
|
60 | 64 | _view_name = Unicode('FloatTextView', sync=True) |
|
61 | 65 | |
|
62 | 66 | |
|
63 | 67 | @register('IPython.BoundedFloatText') |
|
64 | 68 | class BoundedFloatText(_BoundedFloat): |
|
65 | 69 | """ Displays a float value within a textbox. Value must be within the range specified. |
|
66 | 70 | For a textbox in which the value doesn't need to be within a specific range, use FloatText. |
|
67 | 71 | |
|
68 | 72 | Parameters |
|
69 | 73 | ---------- |
|
70 | 74 | value : float |
|
71 | 75 | value displayed |
|
72 | 76 | min : float |
|
73 | 77 | minimal value of the range of possible values displayed |
|
74 | 78 | max : float |
|
75 | 79 | maximal value of the range of possible values displayed |
|
76 | 80 | description : str |
|
77 | 81 | description displayed next to the textbox |
|
78 | 82 | color : str Unicode color code (eg. '#C13535'), optional |
|
79 | 83 | color of the value displayed |
|
80 | 84 | """ |
|
81 | 85 | _view_name = Unicode('FloatTextView', sync=True) |
|
82 | 86 | |
|
83 | 87 | |
|
84 | 88 | @register('IPython.FloatSlider') |
|
85 | 89 | class FloatSlider(_BoundedFloat): |
|
86 | 90 | """ Slider/trackbar of floating values with the specified range. |
|
87 | 91 | |
|
88 | 92 | Parameters |
|
89 | 93 | ---------- |
|
90 | 94 | value : float |
|
91 | 95 | position of the slider |
|
92 | 96 | min : float |
|
93 | 97 | minimal position of the slider |
|
94 | 98 | max : float |
|
95 | 99 | maximal position of the slider |
|
96 | 100 | step : float |
|
97 | 101 | step of the trackbar |
|
98 | 102 | description : str |
|
99 | 103 | name of the slider |
|
100 | 104 | orientation : {'vertical', 'horizontal}, optional |
|
101 | 105 | default is horizontal |
|
102 | 106 | readout : {True, False}, optional |
|
103 | 107 | default is True, display the current value of the slider next to it |
|
104 | 108 | slider_color : str Unicode color code (eg. '#C13535'), optional |
|
105 | 109 | color of the slider |
|
106 | 110 | color : str Unicode color code (eg. '#C13535'), optional |
|
107 | 111 | color of the value displayed (if readout == True) |
|
108 | 112 | """ |
|
109 | 113 | _view_name = Unicode('FloatSliderView', sync=True) |
|
110 | 114 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
111 | 115 | default_value='horizontal', |
|
112 | 116 | help="Vertical or horizontal.", allow_none=False, sync=True) |
|
113 | 117 | _range = Bool(False, help="Display a range selector", sync=True) |
|
114 | 118 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
115 | 119 | slider_color = Unicode(sync=True) |
|
116 | 120 | |
|
117 | 121 | |
|
118 | 122 | @register('IPython.FloatProgress') |
|
119 | 123 | class FloatProgress(_BoundedFloat): |
|
120 | 124 | """ Displays a progress bar. |
|
121 | 125 | |
|
122 | 126 | Parameters |
|
123 | 127 | ----------- |
|
124 | 128 | value : float |
|
125 | 129 | position within the range of the progress bar |
|
126 | 130 | min : float |
|
127 | 131 | minimal position of the slider |
|
128 | 132 | max : float |
|
129 | 133 | maximal position of the slider |
|
130 | 134 | step : float |
|
131 | 135 | step of the progress bar |
|
132 | 136 | description : str |
|
133 | 137 | name of the progress bar |
|
134 | 138 | bar_style: {'success', 'info', 'warning', 'danger', ''}, optional |
|
135 | 139 | color of the progress bar, default is '' (blue) |
|
136 | 140 | colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red |
|
137 | 141 | """ |
|
138 | 142 | _view_name = Unicode('ProgressView', sync=True) |
|
139 | 143 | |
|
140 | 144 | bar_style = CaselessStrEnum( |
|
141 | 145 | values=['success', 'info', 'warning', 'danger', ''], |
|
142 | 146 | default_value='', allow_none=True, sync=True, help="""Use a |
|
143 | 147 | predefined styling for the progess bar.""") |
|
144 | 148 | |
|
145 | 149 | class _FloatRange(_Float): |
|
146 | 150 | value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True) |
|
147 | 151 | lower = CFloat(0.0, help="Lower bound", sync=False) |
|
148 | 152 | upper = CFloat(1.0, help="Upper bound", sync=False) |
|
149 | 153 | |
|
150 | 154 | def __init__(self, *pargs, **kwargs): |
|
151 | 155 | value_given = 'value' in kwargs |
|
152 | 156 | lower_given = 'lower' in kwargs |
|
153 | 157 | upper_given = 'upper' in kwargs |
|
154 | 158 | if value_given and (lower_given or upper_given): |
|
155 | 159 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
156 | 160 | if lower_given != upper_given: |
|
157 | 161 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") |
|
158 | 162 | |
|
159 | 163 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
160 | 164 | |
|
161 | 165 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
162 | 166 | if value_given: |
|
163 | 167 | self.lower, self.upper = self.value |
|
164 | 168 | else: |
|
165 | 169 | self.value = (self.lower, self.upper) |
|
166 | 170 | |
|
167 | 171 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
168 | 172 | |
|
169 | 173 | def _validate(self, name, old, new): |
|
170 | 174 | if name == 'value': |
|
171 | 175 | self.lower, self.upper = min(new), max(new) |
|
172 | 176 | elif name == 'lower': |
|
173 | 177 | self.value = (new, self.value[1]) |
|
174 | 178 | elif name == 'upper': |
|
175 | 179 | self.value = (self.value[0], new) |
|
176 | 180 | |
|
177 | 181 | class _BoundedFloatRange(_FloatRange): |
|
178 | 182 | step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
179 | 183 | max = CFloat(100.0, help="Max value", sync=True) |
|
180 | 184 | min = CFloat(0.0, help="Min value", sync=True) |
|
181 | 185 | |
|
182 | 186 | def __init__(self, *pargs, **kwargs): |
|
183 | 187 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
184 | 188 | _FloatRange.__init__(self, *pargs, **kwargs) |
|
185 | 189 | |
|
186 | 190 | # ensure a minimal amount of sanity |
|
187 | 191 | if self.min > self.max: |
|
188 | 192 | raise ValueError("min must be <= max") |
|
189 | 193 | |
|
190 | 194 | if any_value_given: |
|
191 | 195 | # if a value was given, clamp it within (min, max) |
|
192 | 196 | self._validate("value", None, self.value) |
|
193 | 197 | else: |
|
194 | 198 | # otherwise, set it to 25-75% to avoid the handles overlapping |
|
195 | 199 | self.value = (0.75*self.min + 0.25*self.max, |
|
196 | 200 | 0.25*self.min + 0.75*self.max) |
|
197 | 201 | # callback already set for 'value', 'lower', 'upper' |
|
198 | 202 | self.on_trait_change(self._validate, ['min', 'max']) |
|
199 | 203 | |
|
200 | 204 | |
|
201 | 205 | def _validate(self, name, old, new): |
|
202 | 206 | if name == "min": |
|
203 | 207 | if new > self.max: |
|
204 | 208 | raise ValueError("setting min > max") |
|
205 | 209 | self.min = new |
|
206 | 210 | elif name == "max": |
|
207 | 211 | if new < self.min: |
|
208 | 212 | raise ValueError("setting max < min") |
|
209 | 213 | self.max = new |
|
210 | 214 | |
|
211 | 215 | low, high = self.value |
|
212 | 216 | if name == "value": |
|
213 | 217 | low, high = min(new), max(new) |
|
214 | 218 | elif name == "upper": |
|
215 | 219 | if new < self.lower: |
|
216 | 220 | raise ValueError("setting upper < lower") |
|
217 | 221 | high = new |
|
218 | 222 | elif name == "lower": |
|
219 | 223 | if new > self.upper: |
|
220 | 224 | raise ValueError("setting lower > upper") |
|
221 | 225 | low = new |
|
222 | 226 | |
|
223 | 227 | low = max(self.min, min(low, self.max)) |
|
224 | 228 | high = min(self.max, max(high, self.min)) |
|
225 | 229 | |
|
226 | 230 | # determine the order in which we should update the |
|
227 | 231 | # lower, upper traits to avoid a temporary inverted overlap |
|
228 | 232 | lower_first = high < self.lower |
|
229 | 233 | |
|
230 | 234 | self.value = (low, high) |
|
231 | 235 | if lower_first: |
|
232 | 236 | self.lower = low |
|
233 | 237 | self.upper = high |
|
234 | 238 | else: |
|
235 | 239 | self.upper = high |
|
236 | 240 | self.lower = low |
|
237 | 241 | |
|
238 | 242 | |
|
239 | 243 | @register('IPython.FloatRangeSlider') |
|
240 | 244 | class FloatRangeSlider(_BoundedFloatRange): |
|
241 | 245 | """ Slider/trackbar for displaying a floating value range (within the specified range of values). |
|
242 | 246 | |
|
243 | 247 | Parameters |
|
244 | 248 | ---------- |
|
245 | 249 | value : float tuple |
|
246 | 250 | range of the slider displayed |
|
247 | 251 | min : float |
|
248 | 252 | minimal position of the slider |
|
249 | 253 | max : float |
|
250 | 254 | maximal position of the slider |
|
251 | 255 | step : float |
|
252 | 256 | step of the trackbar |
|
253 | 257 | description : str |
|
254 | 258 | name of the slider |
|
255 | 259 | orientation : {'vertical', 'horizontal}, optional |
|
256 | 260 | default is horizontal |
|
257 | 261 | readout : {True, False}, optional |
|
258 | 262 | default is True, display the current value of the slider next to it |
|
259 | 263 | slider_color : str Unicode color code (eg. '#C13535'), optional |
|
260 | 264 | color of the slider |
|
261 | 265 | color : str Unicode color code (eg. '#C13535'), optional |
|
262 | 266 | color of the value displayed (if readout == True) |
|
263 | 267 | """ |
|
264 | 268 | _view_name = Unicode('FloatSliderView', sync=True) |
|
265 | 269 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
266 | 270 | default_value='horizontal', allow_none=False, |
|
267 | 271 | help="Vertical or horizontal.", sync=True) |
|
268 | 272 | _range = Bool(True, help="Display a range selector", sync=True) |
|
269 | 273 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
270 | 274 | slider_color = Unicode(sync=True) |
|
271 | 275 | |
|
272 | 276 | # Remove in IPython 4.0 |
|
273 | 277 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') |
|
274 | 278 | BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget') |
|
275 | 279 | FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget') |
|
276 | 280 | FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget') |
@@ -1,197 +1,201 | |||
|
1 | 1 | """Int class. |
|
2 | 2 | |
|
3 | 3 | Represents an unbounded int using a widget. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from .widget import DOMWidget, register |
|
17 | 17 | from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple |
|
18 | 18 | from IPython.utils.warn import DeprecatedClass |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | class _Int(DOMWidget): |
|
24 | 24 | """Base class used to create widgets that represent an int.""" |
|
25 | 25 | value = CInt(0, help="Int value", sync=True) |
|
26 | 26 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | 27 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
28 | 28 | |
|
29 | def __init__(self, value=None, **kwargs): | |
|
30 | if value is not None: | |
|
31 | kwargs['value'] = value | |
|
32 | super(_Int, self).__init__(**kwargs) | |
|
29 | 33 | |
|
30 | 34 | class _BoundedInt(_Int): |
|
31 | 35 | """Base class used to create widgets that represent a int that is bounded |
|
32 | 36 | by a minium and maximum.""" |
|
33 | 37 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
34 | 38 | max = CInt(100, help="Max value", sync=True) |
|
35 | 39 | min = CInt(0, help="Min value", sync=True) |
|
36 | 40 | |
|
37 | 41 | def __init__(self, *pargs, **kwargs): |
|
38 | 42 | """Constructor""" |
|
39 |
|
|
|
43 | super(_BoundedInt, self).__init__(*pargs, **kwargs) | |
|
40 | 44 | self.on_trait_change(self._validate_value, ['value']) |
|
41 | 45 | self.on_trait_change(self._handle_max_changed, ['max']) |
|
42 | 46 | self.on_trait_change(self._handle_min_changed, ['min']) |
|
43 | 47 | |
|
44 | 48 | def _validate_value(self, name, old, new): |
|
45 | 49 | """Validate value.""" |
|
46 | 50 | if self.min > new or new > self.max: |
|
47 | 51 | self.value = min(max(new, self.min), self.max) |
|
48 | 52 | |
|
49 | 53 | def _handle_max_changed(self, name, old, new): |
|
50 | 54 | """Make sure the min is always <= the max.""" |
|
51 | 55 | if new < self.min: |
|
52 | 56 | raise ValueError("setting max < min") |
|
53 | 57 | |
|
54 | 58 | def _handle_min_changed(self, name, old, new): |
|
55 | 59 | """Make sure the max is always >= the min.""" |
|
56 | 60 | if new > self.max: |
|
57 | 61 | raise ValueError("setting min > max") |
|
58 | 62 | |
|
59 | 63 | @register('IPython.IntText') |
|
60 | 64 | class IntText(_Int): |
|
61 | 65 | """Textbox widget that represents a int.""" |
|
62 | 66 | _view_name = Unicode('IntTextView', sync=True) |
|
63 | 67 | |
|
64 | 68 | |
|
65 | 69 | @register('IPython.BoundedIntText') |
|
66 | 70 | class BoundedIntText(_BoundedInt): |
|
67 | 71 | """Textbox widget that represents a int bounded by a minimum and maximum value.""" |
|
68 | 72 | _view_name = Unicode('IntTextView', sync=True) |
|
69 | 73 | |
|
70 | 74 | |
|
71 | 75 | @register('IPython.IntSlider') |
|
72 | 76 | class IntSlider(_BoundedInt): |
|
73 | 77 | """Slider widget that represents a int bounded by a minimum and maximum value.""" |
|
74 | 78 | _view_name = Unicode('IntSliderView', sync=True) |
|
75 | 79 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
76 | 80 | default_value='horizontal', allow_none=False, |
|
77 | 81 | help="Vertical or horizontal.", sync=True) |
|
78 | 82 | _range = Bool(False, help="Display a range selector", sync=True) |
|
79 | 83 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
80 | 84 | slider_color = Unicode(sync=True) |
|
81 | 85 | |
|
82 | 86 | |
|
83 | 87 | @register('IPython.IntProgress') |
|
84 | 88 | class IntProgress(_BoundedInt): |
|
85 | 89 | """Progress bar that represents a int bounded by a minimum and maximum value.""" |
|
86 | 90 | _view_name = Unicode('ProgressView', sync=True) |
|
87 | 91 | |
|
88 | 92 | bar_style = CaselessStrEnum( |
|
89 | 93 | values=['success', 'info', 'warning', 'danger', ''], |
|
90 | 94 | default_value='', allow_none=True, sync=True, help="""Use a |
|
91 | 95 | predefined styling for the progess bar.""") |
|
92 | 96 | |
|
93 | 97 | class _IntRange(_Int): |
|
94 | 98 | value = Tuple(CInt, CInt, default_value=(0, 1), help="Tuple of (lower, upper) bounds", sync=True) |
|
95 | 99 | lower = CInt(0, help="Lower bound", sync=False) |
|
96 | 100 | upper = CInt(1, help="Upper bound", sync=False) |
|
97 | 101 | |
|
98 | 102 | def __init__(self, *pargs, **kwargs): |
|
99 | 103 | value_given = 'value' in kwargs |
|
100 | 104 | lower_given = 'lower' in kwargs |
|
101 | 105 | upper_given = 'upper' in kwargs |
|
102 | 106 | if value_given and (lower_given or upper_given): |
|
103 | 107 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
104 | 108 | if lower_given != upper_given: |
|
105 | 109 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") |
|
106 | 110 | |
|
107 |
|
|
|
111 | super(_IntRange, self).__init__(*pargs, **kwargs) | |
|
108 | 112 | |
|
109 | 113 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
110 | 114 | if value_given: |
|
111 | 115 | self.lower, self.upper = self.value |
|
112 | 116 | else: |
|
113 | 117 | self.value = (self.lower, self.upper) |
|
114 | 118 | |
|
115 | 119 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
116 | 120 | |
|
117 | 121 | def _validate(self, name, old, new): |
|
118 | 122 | if name == 'value': |
|
119 | 123 | self.lower, self.upper = min(new), max(new) |
|
120 | 124 | elif name == 'lower': |
|
121 | 125 | self.value = (new, self.value[1]) |
|
122 | 126 | elif name == 'upper': |
|
123 | 127 | self.value = (self.value[0], new) |
|
124 | 128 | |
|
125 | 129 | class _BoundedIntRange(_IntRange): |
|
126 | 130 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
127 | 131 | max = CInt(100, help="Max value", sync=True) |
|
128 | 132 | min = CInt(0, help="Min value", sync=True) |
|
129 | 133 | |
|
130 | 134 | def __init__(self, *pargs, **kwargs): |
|
131 | 135 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
132 | 136 | _IntRange.__init__(self, *pargs, **kwargs) |
|
133 | 137 | |
|
134 | 138 | # ensure a minimal amount of sanity |
|
135 | 139 | if self.min > self.max: |
|
136 | 140 | raise ValueError("min must be <= max") |
|
137 | 141 | |
|
138 | 142 | if any_value_given: |
|
139 | 143 | # if a value was given, clamp it within (min, max) |
|
140 | 144 | self._validate("value", None, self.value) |
|
141 | 145 | else: |
|
142 | 146 | # otherwise, set it to 25-75% to avoid the handles overlapping |
|
143 | 147 | self.value = (0.75*self.min + 0.25*self.max, |
|
144 | 148 | 0.25*self.min + 0.75*self.max) |
|
145 | 149 | # callback already set for 'value', 'lower', 'upper' |
|
146 | 150 | self.on_trait_change(self._validate, ['min', 'max']) |
|
147 | 151 | |
|
148 | 152 | def _validate(self, name, old, new): |
|
149 | 153 | if name == "min": |
|
150 | 154 | if new > self.max: |
|
151 | 155 | raise ValueError("setting min > max") |
|
152 | 156 | elif name == "max": |
|
153 | 157 | if new < self.min: |
|
154 | 158 | raise ValueError("setting max < min") |
|
155 | 159 | |
|
156 | 160 | low, high = self.value |
|
157 | 161 | if name == "value": |
|
158 | 162 | low, high = min(new), max(new) |
|
159 | 163 | elif name == "upper": |
|
160 | 164 | if new < self.lower: |
|
161 | 165 | raise ValueError("setting upper < lower") |
|
162 | 166 | high = new |
|
163 | 167 | elif name == "lower": |
|
164 | 168 | if new > self.upper: |
|
165 | 169 | raise ValueError("setting lower > upper") |
|
166 | 170 | low = new |
|
167 | 171 | |
|
168 | 172 | low = max(self.min, min(low, self.max)) |
|
169 | 173 | high = min(self.max, max(high, self.min)) |
|
170 | 174 | |
|
171 | 175 | # determine the order in which we should update the |
|
172 | 176 | # lower, upper traits to avoid a temporary inverted overlap |
|
173 | 177 | lower_first = high < self.lower |
|
174 | 178 | |
|
175 | 179 | self.value = (low, high) |
|
176 | 180 | if lower_first: |
|
177 | 181 | self.lower = low |
|
178 | 182 | self.upper = high |
|
179 | 183 | else: |
|
180 | 184 | self.upper = high |
|
181 | 185 | self.lower = low |
|
182 | 186 | |
|
183 | 187 | @register('IPython.IntRangeSlider') |
|
184 | 188 | class IntRangeSlider(_BoundedIntRange): |
|
185 | 189 | _view_name = Unicode('IntSliderView', sync=True) |
|
186 | 190 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
187 | 191 | default_value='horizontal', allow_none=False, |
|
188 | 192 | help="Vertical or horizontal.", sync=True) |
|
189 | 193 | _range = Bool(True, help="Display a range selector", sync=True) |
|
190 | 194 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
191 | 195 | slider_color = Unicode(sync=True) |
|
192 | 196 | |
|
193 | 197 | # Remove in IPython 4.0 |
|
194 | 198 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') |
|
195 | 199 | BoundedIntTextWidget = DeprecatedClass(BoundedIntText, 'BoundedIntTextWidget') |
|
196 | 200 | IntSliderWidget = DeprecatedClass(IntSlider, 'IntSliderWidget') |
|
197 | 201 | IntProgressWidget = DeprecatedClass(IntProgress, 'IntProgressWidget') |
@@ -1,91 +1,95 | |||
|
1 | 1 | """String class. |
|
2 | 2 | |
|
3 | 3 | Represents a unicode string using a widget. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from .widget import DOMWidget, CallbackDispatcher, register |
|
17 | 17 | from IPython.utils.traitlets import Unicode, Bool |
|
18 | 18 | from IPython.utils.warn import DeprecatedClass |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | class _String(DOMWidget): |
|
24 | 24 | """Base class used to create widgets that represent a string.""" |
|
25 | 25 | value = Unicode(help="String value", sync=True) |
|
26 | 26 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | 27 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
28 | 28 | placeholder = Unicode("", help="Placeholder text to display when nothing has been typed", sync=True) |
|
29 | 29 | |
|
30 | def __init__(self, value=None, **kwargs): | |
|
31 | if value is not None: | |
|
32 | kwargs['value'] = value | |
|
33 | super(_String, self).__init__(**kwargs) | |
|
30 | 34 | |
|
31 | 35 | @register('IPython.HTML') |
|
32 | 36 | class HTML(_String): |
|
33 | 37 | """Renders the string `value` as HTML.""" |
|
34 | 38 | _view_name = Unicode('HTMLView', sync=True) |
|
35 | 39 | |
|
36 | 40 | |
|
37 | 41 | @register('IPython.Latex') |
|
38 | 42 | class Latex(_String): |
|
39 | 43 | """Renders math inside the string `value` as Latex (requires $ $ or $$ $$ |
|
40 | 44 | and similar latex tags).""" |
|
41 | 45 | _view_name = Unicode('LatexView', sync=True) |
|
42 | 46 | |
|
43 | 47 | |
|
44 | 48 | @register('IPython.Textarea') |
|
45 | 49 | class Textarea(_String): |
|
46 | 50 | """Multiline text area widget.""" |
|
47 | 51 | _view_name = Unicode('TextareaView', sync=True) |
|
48 | 52 | |
|
49 | 53 | def scroll_to_bottom(self): |
|
50 | 54 | self.send({"method": "scroll_to_bottom"}) |
|
51 | 55 | |
|
52 | 56 | |
|
53 | 57 | @register('IPython.Text') |
|
54 | 58 | class Text(_String): |
|
55 | 59 | """Single line textbox widget.""" |
|
56 | 60 | _view_name = Unicode('TextView', sync=True) |
|
57 | 61 | |
|
58 | def __init__(self, **kwargs): | |
|
59 | super(Text, self).__init__(**kwargs) | |
|
62 | def __init__(self, *args, **kwargs): | |
|
63 | super(Text, self).__init__(*args, **kwargs) | |
|
60 | 64 | self._submission_callbacks = CallbackDispatcher() |
|
61 | 65 | self.on_msg(self._handle_string_msg) |
|
62 | 66 | |
|
63 | 67 | def _handle_string_msg(self, _, content): |
|
64 | 68 | """Handle a msg from the front-end. |
|
65 | 69 | |
|
66 | 70 | Parameters |
|
67 | 71 | ---------- |
|
68 | 72 | content: dict |
|
69 | 73 | Content of the msg.""" |
|
70 | 74 | if content.get('event', '') == 'submit': |
|
71 | 75 | self._submission_callbacks(self) |
|
72 | 76 | |
|
73 | 77 | def on_submit(self, callback, remove=False): |
|
74 | 78 | """(Un)Register a callback to handle text submission. |
|
75 | 79 | |
|
76 | 80 | Triggered when the user clicks enter. |
|
77 | 81 | |
|
78 | 82 | Parameters |
|
79 | 83 | ---------- |
|
80 | 84 | callback: callable |
|
81 | 85 | Will be called with exactly one argument: the Widget instance |
|
82 | 86 | remove: bool (optional) |
|
83 | 87 | Whether to unregister the callback""" |
|
84 | 88 | self._submission_callbacks.register_callback(callback, remove=remove) |
|
85 | 89 | |
|
86 | 90 | |
|
87 | 91 | # Remove in IPython 4.0 |
|
88 | 92 | HTMLWidget = DeprecatedClass(HTML, 'HTMLWidget') |
|
89 | 93 | LatexWidget = DeprecatedClass(Latex, 'LatexWidget') |
|
90 | 94 | TextareaWidget = DeprecatedClass(Textarea, 'TextareaWidget') |
|
91 | 95 | TextWidget = DeprecatedClass(Text, 'TextWidget') |
General Comments 0
You need to be logged in to leave comments.
Login now