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