Show More
@@ -1,489 +1,489 | |||||
1 | """Base Widget class. Allows user to create widgets in the back-end that render |
|
1 | """Base Widget class. Allows user to create widgets in the back-end that render | |
2 | in the IPython notebook front-end. |
|
2 | in the IPython notebook front-end. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | from contextlib import contextmanager |
|
15 | from contextlib import contextmanager | |
16 | import collections |
|
16 | import collections | |
17 |
|
17 | |||
18 | from IPython.core.getipython import get_ipython |
|
18 | from IPython.core.getipython import get_ipython | |
19 | from IPython.kernel.comm import Comm |
|
19 | from IPython.kernel.comm import Comm | |
20 | from IPython.config import LoggingConfigurable |
|
20 | from IPython.config import LoggingConfigurable | |
21 | from IPython.utils.importstring import import_item |
|
21 | from IPython.utils.importstring import import_item | |
22 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ |
|
22 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ | |
23 | CaselessStrEnum, Tuple, CUnicode, Int, Set |
|
23 | CaselessStrEnum, Tuple, CUnicode, Int, Set, Color | |
24 | from IPython.utils.py3compat import string_types |
|
24 | from IPython.utils.py3compat import string_types | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Classes |
|
27 | # Classes | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | class CallbackDispatcher(LoggingConfigurable): |
|
29 | class CallbackDispatcher(LoggingConfigurable): | |
30 | """A structure for registering and running callbacks""" |
|
30 | """A structure for registering and running callbacks""" | |
31 | callbacks = List() |
|
31 | callbacks = List() | |
32 |
|
32 | |||
33 | def __call__(self, *args, **kwargs): |
|
33 | def __call__(self, *args, **kwargs): | |
34 | """Call all of the registered callbacks.""" |
|
34 | """Call all of the registered callbacks.""" | |
35 | value = None |
|
35 | value = None | |
36 | for callback in self.callbacks: |
|
36 | for callback in self.callbacks: | |
37 | try: |
|
37 | try: | |
38 | local_value = callback(*args, **kwargs) |
|
38 | local_value = callback(*args, **kwargs) | |
39 | except Exception as e: |
|
39 | except Exception as e: | |
40 | ip = get_ipython() |
|
40 | ip = get_ipython() | |
41 | if ip is None: |
|
41 | if ip is None: | |
42 | self.log.warn("Exception in callback %s: %s", callback, e, exc_info=True) |
|
42 | self.log.warn("Exception in callback %s: %s", callback, e, exc_info=True) | |
43 | else: |
|
43 | else: | |
44 | ip.showtraceback() |
|
44 | ip.showtraceback() | |
45 | else: |
|
45 | else: | |
46 | value = local_value if local_value is not None else value |
|
46 | value = local_value if local_value is not None else value | |
47 | return value |
|
47 | return value | |
48 |
|
48 | |||
49 | def register_callback(self, callback, remove=False): |
|
49 | def register_callback(self, callback, remove=False): | |
50 | """(Un)Register a callback |
|
50 | """(Un)Register a callback | |
51 |
|
51 | |||
52 | Parameters |
|
52 | Parameters | |
53 | ---------- |
|
53 | ---------- | |
54 | callback: method handle |
|
54 | callback: method handle | |
55 | Method to be registered or unregistered. |
|
55 | Method to be registered or unregistered. | |
56 | remove=False: bool |
|
56 | remove=False: bool | |
57 | Whether to unregister the callback.""" |
|
57 | Whether to unregister the callback.""" | |
58 |
|
58 | |||
59 | # (Un)Register the callback. |
|
59 | # (Un)Register the callback. | |
60 | if remove and callback in self.callbacks: |
|
60 | if remove and callback in self.callbacks: | |
61 | self.callbacks.remove(callback) |
|
61 | self.callbacks.remove(callback) | |
62 | elif not remove and callback not in self.callbacks: |
|
62 | elif not remove and callback not in self.callbacks: | |
63 | self.callbacks.append(callback) |
|
63 | self.callbacks.append(callback) | |
64 |
|
64 | |||
65 | def _show_traceback(method): |
|
65 | def _show_traceback(method): | |
66 | """decorator for showing tracebacks in IPython""" |
|
66 | """decorator for showing tracebacks in IPython""" | |
67 | def m(self, *args, **kwargs): |
|
67 | def m(self, *args, **kwargs): | |
68 | try: |
|
68 | try: | |
69 | return(method(self, *args, **kwargs)) |
|
69 | return(method(self, *args, **kwargs)) | |
70 | except Exception as e: |
|
70 | except Exception as e: | |
71 | ip = get_ipython() |
|
71 | ip = get_ipython() | |
72 | if ip is None: |
|
72 | if ip is None: | |
73 | self.log.warn("Exception in widget method %s: %s", method, e, exc_info=True) |
|
73 | self.log.warn("Exception in widget method %s: %s", method, e, exc_info=True) | |
74 | else: |
|
74 | else: | |
75 | ip.showtraceback() |
|
75 | ip.showtraceback() | |
76 | return m |
|
76 | return m | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | def register(key=None): |
|
79 | def register(key=None): | |
80 | """Returns a decorator registering a widget class in the widget registry. |
|
80 | """Returns a decorator registering a widget class in the widget registry. | |
81 | If no key is provided, the class name is used as a key. A key is |
|
81 | If no key is provided, the class name is used as a key. A key is | |
82 | provided for each core IPython widget so that the frontend can use |
|
82 | provided for each core IPython widget so that the frontend can use | |
83 | this key regardless of the language of the kernel""" |
|
83 | this key regardless of the language of the kernel""" | |
84 | def wrap(widget): |
|
84 | def wrap(widget): | |
85 | l = key if key is not None else widget.__module__ + widget.__name__ |
|
85 | l = key if key is not None else widget.__module__ + widget.__name__ | |
86 | Widget.widget_types[l] = widget |
|
86 | Widget.widget_types[l] = widget | |
87 | return widget |
|
87 | return widget | |
88 | return wrap |
|
88 | return wrap | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | class Widget(LoggingConfigurable): |
|
91 | class Widget(LoggingConfigurable): | |
92 | #------------------------------------------------------------------------- |
|
92 | #------------------------------------------------------------------------- | |
93 | # Class attributes |
|
93 | # Class attributes | |
94 | #------------------------------------------------------------------------- |
|
94 | #------------------------------------------------------------------------- | |
95 | _widget_construction_callback = None |
|
95 | _widget_construction_callback = None | |
96 | widgets = {} |
|
96 | widgets = {} | |
97 | widget_types = {} |
|
97 | widget_types = {} | |
98 |
|
98 | |||
99 | @staticmethod |
|
99 | @staticmethod | |
100 | def on_widget_constructed(callback): |
|
100 | def on_widget_constructed(callback): | |
101 | """Registers a callback to be called when a widget is constructed. |
|
101 | """Registers a callback to be called when a widget is constructed. | |
102 |
|
102 | |||
103 | The callback must have the following signature: |
|
103 | The callback must have the following signature: | |
104 | callback(widget)""" |
|
104 | callback(widget)""" | |
105 | Widget._widget_construction_callback = callback |
|
105 | Widget._widget_construction_callback = callback | |
106 |
|
106 | |||
107 | @staticmethod |
|
107 | @staticmethod | |
108 | def _call_widget_constructed(widget): |
|
108 | def _call_widget_constructed(widget): | |
109 | """Static method, called when a widget is constructed.""" |
|
109 | """Static method, called when a widget is constructed.""" | |
110 | if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback): |
|
110 | if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback): | |
111 | Widget._widget_construction_callback(widget) |
|
111 | Widget._widget_construction_callback(widget) | |
112 |
|
112 | |||
113 | @staticmethod |
|
113 | @staticmethod | |
114 | def handle_comm_opened(comm, msg): |
|
114 | def handle_comm_opened(comm, msg): | |
115 | """Static method, called when a widget is constructed.""" |
|
115 | """Static method, called when a widget is constructed.""" | |
116 | widget_class = import_item(msg['content']['data']['widget_class']) |
|
116 | widget_class = import_item(msg['content']['data']['widget_class']) | |
117 | widget = widget_class(comm=comm) |
|
117 | widget = widget_class(comm=comm) | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | #------------------------------------------------------------------------- |
|
120 | #------------------------------------------------------------------------- | |
121 | # Traits |
|
121 | # Traits | |
122 | #------------------------------------------------------------------------- |
|
122 | #------------------------------------------------------------------------- | |
123 | _model_module = Unicode(None, allow_none=True, help="""A requirejs module name |
|
123 | _model_module = Unicode(None, allow_none=True, help="""A requirejs module name | |
124 | in which to find _model_name. If empty, look in the global registry.""") |
|
124 | in which to find _model_name. If empty, look in the global registry.""") | |
125 | _model_name = Unicode('WidgetModel', help="""Name of the backbone model |
|
125 | _model_name = Unicode('WidgetModel', help="""Name of the backbone model | |
126 | registered in the front-end to create and sync this widget with.""") |
|
126 | registered in the front-end to create and sync this widget with.""") | |
127 | _view_module = Unicode(help="""A requirejs module in which to find _view_name. |
|
127 | _view_module = Unicode(help="""A requirejs module in which to find _view_name. | |
128 | If empty, look in the global registry.""", sync=True) |
|
128 | If empty, look in the global registry.""", sync=True) | |
129 | _view_name = Unicode(None, allow_none=True, help="""Default view registered in the front-end |
|
129 | _view_name = Unicode(None, allow_none=True, help="""Default view registered in the front-end | |
130 | to use to represent the widget.""", sync=True) |
|
130 | to use to represent the widget.""", sync=True) | |
131 | comm = Instance('IPython.kernel.comm.Comm') |
|
131 | comm = Instance('IPython.kernel.comm.Comm') | |
132 |
|
132 | |||
133 | msg_throttle = Int(3, sync=True, help="""Maximum number of msgs the |
|
133 | msg_throttle = Int(3, sync=True, help="""Maximum number of msgs the | |
134 | front-end can send before receiving an idle msg from the back-end.""") |
|
134 | front-end can send before receiving an idle msg from the back-end.""") | |
135 |
|
135 | |||
136 | version = Int(0, sync=True, help="""Widget's version""") |
|
136 | version = Int(0, sync=True, help="""Widget's version""") | |
137 | keys = List() |
|
137 | keys = List() | |
138 | def _keys_default(self): |
|
138 | def _keys_default(self): | |
139 | return [name for name in self.traits(sync=True)] |
|
139 | return [name for name in self.traits(sync=True)] | |
140 |
|
140 | |||
141 | _property_lock = Tuple((None, None)) |
|
141 | _property_lock = Tuple((None, None)) | |
142 | _send_state_lock = Int(0) |
|
142 | _send_state_lock = Int(0) | |
143 | _states_to_send = Set() |
|
143 | _states_to_send = Set() | |
144 | _display_callbacks = Instance(CallbackDispatcher, ()) |
|
144 | _display_callbacks = Instance(CallbackDispatcher, ()) | |
145 | _msg_callbacks = Instance(CallbackDispatcher, ()) |
|
145 | _msg_callbacks = Instance(CallbackDispatcher, ()) | |
146 |
|
146 | |||
147 | #------------------------------------------------------------------------- |
|
147 | #------------------------------------------------------------------------- | |
148 | # (Con/de)structor |
|
148 | # (Con/de)structor | |
149 | #------------------------------------------------------------------------- |
|
149 | #------------------------------------------------------------------------- | |
150 | def __init__(self, **kwargs): |
|
150 | def __init__(self, **kwargs): | |
151 | """Public constructor""" |
|
151 | """Public constructor""" | |
152 | self._model_id = kwargs.pop('model_id', None) |
|
152 | self._model_id = kwargs.pop('model_id', None) | |
153 | super(Widget, self).__init__(**kwargs) |
|
153 | super(Widget, self).__init__(**kwargs) | |
154 |
|
154 | |||
155 | Widget._call_widget_constructed(self) |
|
155 | Widget._call_widget_constructed(self) | |
156 | self.open() |
|
156 | self.open() | |
157 |
|
157 | |||
158 | def __del__(self): |
|
158 | def __del__(self): | |
159 | """Object disposal""" |
|
159 | """Object disposal""" | |
160 | self.close() |
|
160 | self.close() | |
161 |
|
161 | |||
162 | #------------------------------------------------------------------------- |
|
162 | #------------------------------------------------------------------------- | |
163 | # Properties |
|
163 | # Properties | |
164 | #------------------------------------------------------------------------- |
|
164 | #------------------------------------------------------------------------- | |
165 |
|
165 | |||
166 | def open(self): |
|
166 | def open(self): | |
167 | """Open a comm to the frontend if one isn't already open.""" |
|
167 | """Open a comm to the frontend if one isn't already open.""" | |
168 | if self.comm is None: |
|
168 | if self.comm is None: | |
169 | args = dict(target_name='ipython.widget', |
|
169 | args = dict(target_name='ipython.widget', | |
170 | data={'model_name': self._model_name, |
|
170 | data={'model_name': self._model_name, | |
171 | 'model_module': self._model_module}) |
|
171 | 'model_module': self._model_module}) | |
172 | if self._model_id is not None: |
|
172 | if self._model_id is not None: | |
173 | args['comm_id'] = self._model_id |
|
173 | args['comm_id'] = self._model_id | |
174 | self.comm = Comm(**args) |
|
174 | self.comm = Comm(**args) | |
175 |
|
175 | |||
176 | def _comm_changed(self, name, new): |
|
176 | def _comm_changed(self, name, new): | |
177 | """Called when the comm is changed.""" |
|
177 | """Called when the comm is changed.""" | |
178 | if new is None: |
|
178 | if new is None: | |
179 | return |
|
179 | return | |
180 | self._model_id = self.model_id |
|
180 | self._model_id = self.model_id | |
181 |
|
181 | |||
182 | self.comm.on_msg(self._handle_msg) |
|
182 | self.comm.on_msg(self._handle_msg) | |
183 | Widget.widgets[self.model_id] = self |
|
183 | Widget.widgets[self.model_id] = self | |
184 |
|
184 | |||
185 | # first update |
|
185 | # first update | |
186 | self.send_state() |
|
186 | self.send_state() | |
187 |
|
187 | |||
188 | @property |
|
188 | @property | |
189 | def model_id(self): |
|
189 | def model_id(self): | |
190 | """Gets the model id of this widget. |
|
190 | """Gets the model id of this widget. | |
191 |
|
191 | |||
192 | If a Comm doesn't exist yet, a Comm will be created automagically.""" |
|
192 | If a Comm doesn't exist yet, a Comm will be created automagically.""" | |
193 | return self.comm.comm_id |
|
193 | return self.comm.comm_id | |
194 |
|
194 | |||
195 | #------------------------------------------------------------------------- |
|
195 | #------------------------------------------------------------------------- | |
196 | # Methods |
|
196 | # Methods | |
197 | #------------------------------------------------------------------------- |
|
197 | #------------------------------------------------------------------------- | |
198 |
|
198 | |||
199 | def close(self): |
|
199 | def close(self): | |
200 | """Close method. |
|
200 | """Close method. | |
201 |
|
201 | |||
202 | Closes the underlying comm. |
|
202 | Closes the underlying comm. | |
203 | When the comm is closed, all of the widget views are automatically |
|
203 | When the comm is closed, all of the widget views are automatically | |
204 | removed from the front-end.""" |
|
204 | removed from the front-end.""" | |
205 | if self.comm is not None: |
|
205 | if self.comm is not None: | |
206 | Widget.widgets.pop(self.model_id, None) |
|
206 | Widget.widgets.pop(self.model_id, None) | |
207 | self.comm.close() |
|
207 | self.comm.close() | |
208 | self.comm = None |
|
208 | self.comm = None | |
209 |
|
209 | |||
210 | def send_state(self, key=None): |
|
210 | def send_state(self, key=None): | |
211 | """Sends the widget state, or a piece of it, to the front-end. |
|
211 | """Sends the widget state, or a piece of it, to the front-end. | |
212 |
|
212 | |||
213 | Parameters |
|
213 | Parameters | |
214 | ---------- |
|
214 | ---------- | |
215 | key : unicode, or iterable (optional) |
|
215 | key : unicode, or iterable (optional) | |
216 | A single property's name or iterable of property names to sync with the front-end. |
|
216 | A single property's name or iterable of property names to sync with the front-end. | |
217 | """ |
|
217 | """ | |
218 | self._send({ |
|
218 | self._send({ | |
219 | "method" : "update", |
|
219 | "method" : "update", | |
220 | "state" : self.get_state(key=key) |
|
220 | "state" : self.get_state(key=key) | |
221 | }) |
|
221 | }) | |
222 |
|
222 | |||
223 | def get_state(self, key=None): |
|
223 | def get_state(self, key=None): | |
224 | """Gets the widget state, or a piece of it. |
|
224 | """Gets the widget state, or a piece of it. | |
225 |
|
225 | |||
226 | Parameters |
|
226 | Parameters | |
227 | ---------- |
|
227 | ---------- | |
228 | key : unicode or iterable (optional) |
|
228 | key : unicode or iterable (optional) | |
229 | A single property's name or iterable of property names to get. |
|
229 | A single property's name or iterable of property names to get. | |
230 | """ |
|
230 | """ | |
231 | if key is None: |
|
231 | if key is None: | |
232 | keys = self.keys |
|
232 | keys = self.keys | |
233 | elif isinstance(key, string_types): |
|
233 | elif isinstance(key, string_types): | |
234 | keys = [key] |
|
234 | keys = [key] | |
235 | elif isinstance(key, collections.Iterable): |
|
235 | elif isinstance(key, collections.Iterable): | |
236 | keys = key |
|
236 | keys = key | |
237 | else: |
|
237 | else: | |
238 | raise ValueError("key must be a string, an iterable of keys, or None") |
|
238 | raise ValueError("key must be a string, an iterable of keys, or None") | |
239 | state = {} |
|
239 | state = {} | |
240 | for k in keys: |
|
240 | for k in keys: | |
241 | f = self.trait_metadata(k, 'to_json', self._trait_to_json) |
|
241 | f = self.trait_metadata(k, 'to_json', self._trait_to_json) | |
242 | value = getattr(self, k) |
|
242 | value = getattr(self, k) | |
243 | state[k] = f(value) |
|
243 | state[k] = f(value) | |
244 | return state |
|
244 | return state | |
245 |
|
245 | |||
246 | def set_state(self, sync_data): |
|
246 | def set_state(self, sync_data): | |
247 | """Called when a state is received from the front-end.""" |
|
247 | """Called when a state is received from the front-end.""" | |
248 | for name in self.keys: |
|
248 | for name in self.keys: | |
249 | if name in sync_data: |
|
249 | if name in sync_data: | |
250 | json_value = sync_data[name] |
|
250 | json_value = sync_data[name] | |
251 | from_json = self.trait_metadata(name, 'from_json', self._trait_from_json) |
|
251 | from_json = self.trait_metadata(name, 'from_json', self._trait_from_json) | |
252 | with self._lock_property(name, json_value): |
|
252 | with self._lock_property(name, json_value): | |
253 | setattr(self, name, from_json(json_value)) |
|
253 | setattr(self, name, from_json(json_value)) | |
254 |
|
254 | |||
255 | def send(self, content): |
|
255 | def send(self, content): | |
256 | """Sends a custom msg to the widget model in the front-end. |
|
256 | """Sends a custom msg to the widget model in the front-end. | |
257 |
|
257 | |||
258 | Parameters |
|
258 | Parameters | |
259 | ---------- |
|
259 | ---------- | |
260 | content : dict |
|
260 | content : dict | |
261 | Content of the message to send. |
|
261 | Content of the message to send. | |
262 | """ |
|
262 | """ | |
263 | self._send({"method": "custom", "content": content}) |
|
263 | self._send({"method": "custom", "content": content}) | |
264 |
|
264 | |||
265 | def on_msg(self, callback, remove=False): |
|
265 | def on_msg(self, callback, remove=False): | |
266 | """(Un)Register a custom msg receive callback. |
|
266 | """(Un)Register a custom msg receive callback. | |
267 |
|
267 | |||
268 | Parameters |
|
268 | Parameters | |
269 | ---------- |
|
269 | ---------- | |
270 | callback: callable |
|
270 | callback: callable | |
271 | callback will be passed two arguments when a message arrives:: |
|
271 | callback will be passed two arguments when a message arrives:: | |
272 |
|
272 | |||
273 | callback(widget, content) |
|
273 | callback(widget, content) | |
274 |
|
274 | |||
275 | remove: bool |
|
275 | remove: bool | |
276 | True if the callback should be unregistered.""" |
|
276 | True if the callback should be unregistered.""" | |
277 | self._msg_callbacks.register_callback(callback, remove=remove) |
|
277 | self._msg_callbacks.register_callback(callback, remove=remove) | |
278 |
|
278 | |||
279 | def on_displayed(self, callback, remove=False): |
|
279 | def on_displayed(self, callback, remove=False): | |
280 | """(Un)Register a widget displayed callback. |
|
280 | """(Un)Register a widget displayed callback. | |
281 |
|
281 | |||
282 | Parameters |
|
282 | Parameters | |
283 | ---------- |
|
283 | ---------- | |
284 | callback: method handler |
|
284 | callback: method handler | |
285 | Must have a signature of:: |
|
285 | Must have a signature of:: | |
286 |
|
286 | |||
287 | callback(widget, **kwargs) |
|
287 | callback(widget, **kwargs) | |
288 |
|
288 | |||
289 | kwargs from display are passed through without modification. |
|
289 | kwargs from display are passed through without modification. | |
290 | remove: bool |
|
290 | remove: bool | |
291 | True if the callback should be unregistered.""" |
|
291 | True if the callback should be unregistered.""" | |
292 | self._display_callbacks.register_callback(callback, remove=remove) |
|
292 | self._display_callbacks.register_callback(callback, remove=remove) | |
293 |
|
293 | |||
294 | #------------------------------------------------------------------------- |
|
294 | #------------------------------------------------------------------------- | |
295 | # Support methods |
|
295 | # Support methods | |
296 | #------------------------------------------------------------------------- |
|
296 | #------------------------------------------------------------------------- | |
297 | @contextmanager |
|
297 | @contextmanager | |
298 | def _lock_property(self, key, value): |
|
298 | def _lock_property(self, key, value): | |
299 | """Lock a property-value pair. |
|
299 | """Lock a property-value pair. | |
300 |
|
300 | |||
301 | The value should be the JSON state of the property. |
|
301 | The value should be the JSON state of the property. | |
302 |
|
302 | |||
303 | NOTE: This, in addition to the single lock for all state changes, is |
|
303 | NOTE: This, in addition to the single lock for all state changes, is | |
304 | flawed. In the future we may want to look into buffering state changes |
|
304 | flawed. In the future we may want to look into buffering state changes | |
305 | back to the front-end.""" |
|
305 | back to the front-end.""" | |
306 | self._property_lock = (key, value) |
|
306 | self._property_lock = (key, value) | |
307 | try: |
|
307 | try: | |
308 | yield |
|
308 | yield | |
309 | finally: |
|
309 | finally: | |
310 | self._property_lock = (None, None) |
|
310 | self._property_lock = (None, None) | |
311 |
|
311 | |||
312 | @contextmanager |
|
312 | @contextmanager | |
313 | def hold_sync(self): |
|
313 | def hold_sync(self): | |
314 | """Hold syncing any state until the context manager is released""" |
|
314 | """Hold syncing any state until the context manager is released""" | |
315 | # We increment a value so that this can be nested. Syncing will happen when |
|
315 | # We increment a value so that this can be nested. Syncing will happen when | |
316 | # all levels have been released. |
|
316 | # all levels have been released. | |
317 | self._send_state_lock += 1 |
|
317 | self._send_state_lock += 1 | |
318 | try: |
|
318 | try: | |
319 | yield |
|
319 | yield | |
320 | finally: |
|
320 | finally: | |
321 | self._send_state_lock -=1 |
|
321 | self._send_state_lock -=1 | |
322 | if self._send_state_lock == 0: |
|
322 | if self._send_state_lock == 0: | |
323 | self.send_state(self._states_to_send) |
|
323 | self.send_state(self._states_to_send) | |
324 | self._states_to_send.clear() |
|
324 | self._states_to_send.clear() | |
325 |
|
325 | |||
326 | def _should_send_property(self, key, value): |
|
326 | def _should_send_property(self, key, value): | |
327 | """Check the property lock (property_lock)""" |
|
327 | """Check the property lock (property_lock)""" | |
328 | to_json = self.trait_metadata(key, 'to_json', self._trait_to_json) |
|
328 | to_json = self.trait_metadata(key, 'to_json', self._trait_to_json) | |
329 | if (key == self._property_lock[0] |
|
329 | if (key == self._property_lock[0] | |
330 | and to_json(value) == self._property_lock[1]): |
|
330 | and to_json(value) == self._property_lock[1]): | |
331 | return False |
|
331 | return False | |
332 | elif self._send_state_lock > 0: |
|
332 | elif self._send_state_lock > 0: | |
333 | self._states_to_send.add(key) |
|
333 | self._states_to_send.add(key) | |
334 | return False |
|
334 | return False | |
335 | else: |
|
335 | else: | |
336 | return True |
|
336 | return True | |
337 |
|
337 | |||
338 | # Event handlers |
|
338 | # Event handlers | |
339 | @_show_traceback |
|
339 | @_show_traceback | |
340 | def _handle_msg(self, msg): |
|
340 | def _handle_msg(self, msg): | |
341 | """Called when a msg is received from the front-end""" |
|
341 | """Called when a msg is received from the front-end""" | |
342 | data = msg['content']['data'] |
|
342 | data = msg['content']['data'] | |
343 | method = data['method'] |
|
343 | method = data['method'] | |
344 |
|
344 | |||
345 | # Handle backbone sync methods CREATE, PATCH, and UPDATE all in one. |
|
345 | # Handle backbone sync methods CREATE, PATCH, and UPDATE all in one. | |
346 | if method == 'backbone': |
|
346 | if method == 'backbone': | |
347 | if 'sync_data' in data: |
|
347 | if 'sync_data' in data: | |
348 | sync_data = data['sync_data'] |
|
348 | sync_data = data['sync_data'] | |
349 | self.set_state(sync_data) # handles all methods |
|
349 | self.set_state(sync_data) # handles all methods | |
350 |
|
350 | |||
351 | # Handle a state request. |
|
351 | # Handle a state request. | |
352 | elif method == 'request_state': |
|
352 | elif method == 'request_state': | |
353 | self.send_state() |
|
353 | self.send_state() | |
354 |
|
354 | |||
355 | # Handle a custom msg from the front-end. |
|
355 | # Handle a custom msg from the front-end. | |
356 | elif method == 'custom': |
|
356 | elif method == 'custom': | |
357 | if 'content' in data: |
|
357 | if 'content' in data: | |
358 | self._handle_custom_msg(data['content']) |
|
358 | self._handle_custom_msg(data['content']) | |
359 |
|
359 | |||
360 | # Catch remainder. |
|
360 | # Catch remainder. | |
361 | else: |
|
361 | else: | |
362 | self.log.error('Unknown front-end to back-end widget msg with method "%s"' % method) |
|
362 | self.log.error('Unknown front-end to back-end widget msg with method "%s"' % method) | |
363 |
|
363 | |||
364 | def _handle_custom_msg(self, content): |
|
364 | def _handle_custom_msg(self, content): | |
365 | """Called when a custom msg is received.""" |
|
365 | """Called when a custom msg is received.""" | |
366 | self._msg_callbacks(self, content) |
|
366 | self._msg_callbacks(self, content) | |
367 |
|
367 | |||
368 | def _notify_trait(self, name, old_value, new_value): |
|
368 | def _notify_trait(self, name, old_value, new_value): | |
369 | """Called when a property has been changed.""" |
|
369 | """Called when a property has been changed.""" | |
370 | # Trigger default traitlet callback machinery. This allows any user |
|
370 | # Trigger default traitlet callback machinery. This allows any user | |
371 | # registered validation to be processed prior to allowing the widget |
|
371 | # registered validation to be processed prior to allowing the widget | |
372 | # machinery to handle the state. |
|
372 | # machinery to handle the state. | |
373 | LoggingConfigurable._notify_trait(self, name, old_value, new_value) |
|
373 | LoggingConfigurable._notify_trait(self, name, old_value, new_value) | |
374 |
|
374 | |||
375 | # Send the state after the user registered callbacks for trait changes |
|
375 | # Send the state after the user registered callbacks for trait changes | |
376 | # have all fired (allows for user to validate values). |
|
376 | # have all fired (allows for user to validate values). | |
377 | if self.comm is not None and name in self.keys: |
|
377 | if self.comm is not None and name in self.keys: | |
378 | # Make sure this isn't information that the front-end just sent us. |
|
378 | # Make sure this isn't information that the front-end just sent us. | |
379 | if self._should_send_property(name, new_value): |
|
379 | if self._should_send_property(name, new_value): | |
380 | # Send new state to front-end |
|
380 | # Send new state to front-end | |
381 | self.send_state(key=name) |
|
381 | self.send_state(key=name) | |
382 |
|
382 | |||
383 | def _handle_displayed(self, **kwargs): |
|
383 | def _handle_displayed(self, **kwargs): | |
384 | """Called when a view has been displayed for this widget instance""" |
|
384 | """Called when a view has been displayed for this widget instance""" | |
385 | self._display_callbacks(self, **kwargs) |
|
385 | self._display_callbacks(self, **kwargs) | |
386 |
|
386 | |||
387 | def _trait_to_json(self, x): |
|
387 | def _trait_to_json(self, x): | |
388 | """Convert a trait value to json |
|
388 | """Convert a trait value to json | |
389 |
|
389 | |||
390 | Traverse lists/tuples and dicts and serialize their values as well. |
|
390 | Traverse lists/tuples and dicts and serialize their values as well. | |
391 | Replace any widgets with their model_id |
|
391 | Replace any widgets with their model_id | |
392 | """ |
|
392 | """ | |
393 | if isinstance(x, dict): |
|
393 | if isinstance(x, dict): | |
394 | return {k: self._trait_to_json(v) for k, v in x.items()} |
|
394 | return {k: self._trait_to_json(v) for k, v in x.items()} | |
395 | elif isinstance(x, (list, tuple)): |
|
395 | elif isinstance(x, (list, tuple)): | |
396 | return [self._trait_to_json(v) for v in x] |
|
396 | return [self._trait_to_json(v) for v in x] | |
397 | elif isinstance(x, Widget): |
|
397 | elif isinstance(x, Widget): | |
398 | return "IPY_MODEL_" + x.model_id |
|
398 | return "IPY_MODEL_" + x.model_id | |
399 | else: |
|
399 | else: | |
400 | return x # Value must be JSON-able |
|
400 | return x # Value must be JSON-able | |
401 |
|
401 | |||
402 | def _trait_from_json(self, x): |
|
402 | def _trait_from_json(self, x): | |
403 | """Convert json values to objects |
|
403 | """Convert json values to objects | |
404 |
|
404 | |||
405 | Replace any strings representing valid model id values to Widget references. |
|
405 | Replace any strings representing valid model id values to Widget references. | |
406 | """ |
|
406 | """ | |
407 | if isinstance(x, dict): |
|
407 | if isinstance(x, dict): | |
408 | return {k: self._trait_from_json(v) for k, v in x.items()} |
|
408 | return {k: self._trait_from_json(v) for k, v in x.items()} | |
409 | elif isinstance(x, (list, tuple)): |
|
409 | elif isinstance(x, (list, tuple)): | |
410 | return [self._trait_from_json(v) for v in x] |
|
410 | return [self._trait_from_json(v) for v in x] | |
411 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: |
|
411 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: | |
412 | # we want to support having child widgets at any level in a hierarchy |
|
412 | # we want to support having child widgets at any level in a hierarchy | |
413 | # trusting that a widget UUID will not appear out in the wild |
|
413 | # trusting that a widget UUID will not appear out in the wild | |
414 | return Widget.widgets[x[10:]] |
|
414 | return Widget.widgets[x[10:]] | |
415 | else: |
|
415 | else: | |
416 | return x |
|
416 | return x | |
417 |
|
417 | |||
418 | def _ipython_display_(self, **kwargs): |
|
418 | def _ipython_display_(self, **kwargs): | |
419 | """Called when `IPython.display.display` is called on the widget.""" |
|
419 | """Called when `IPython.display.display` is called on the widget.""" | |
420 | # Show view. |
|
420 | # Show view. | |
421 | if self._view_name is not None: |
|
421 | if self._view_name is not None: | |
422 | self._send({"method": "display"}) |
|
422 | self._send({"method": "display"}) | |
423 | self._handle_displayed(**kwargs) |
|
423 | self._handle_displayed(**kwargs) | |
424 |
|
424 | |||
425 | def _send(self, msg): |
|
425 | def _send(self, msg): | |
426 | """Sends a message to the model in the front-end.""" |
|
426 | """Sends a message to the model in the front-end.""" | |
427 | self.comm.send(msg) |
|
427 | self.comm.send(msg) | |
428 |
|
428 | |||
429 |
|
429 | |||
430 | class DOMWidget(Widget): |
|
430 | class DOMWidget(Widget): | |
431 | visible = Bool(True, allow_none=True, help="Whether the widget is visible. False collapses the empty space, while None preserves the empty space.", sync=True) |
|
431 | visible = Bool(True, allow_none=True, help="Whether the widget is visible. False collapses the empty space, while None preserves the empty space.", sync=True) | |
432 | _css = Tuple(sync=True, help="CSS property list: (selector, key, value)") |
|
432 | _css = Tuple(sync=True, help="CSS property list: (selector, key, value)") | |
433 | _dom_classes = Tuple(sync=True, help="DOM classes applied to widget.$el.") |
|
433 | _dom_classes = Tuple(sync=True, help="DOM classes applied to widget.$el.") | |
434 |
|
434 | |||
435 | width = CUnicode(sync=True) |
|
435 | width = CUnicode(sync=True) | |
436 | height = CUnicode(sync=True) |
|
436 | height = CUnicode(sync=True) | |
437 | # A default padding of 2.5 px makes the widgets look nice when displayed inline. |
|
437 | # A default padding of 2.5 px makes the widgets look nice when displayed inline. | |
438 | padding = CUnicode(sync=True) |
|
438 | padding = CUnicode(sync=True) | |
439 | margin = CUnicode(sync=True) |
|
439 | margin = CUnicode(sync=True) | |
440 |
|
440 | |||
441 |
color = |
|
441 | color = Color(None, allow_none=True, sync=True) | |
442 |
background_color = |
|
442 | background_color = Color(None, allow_none=True, sync=True) | |
443 |
border_color = |
|
443 | border_color = Color(None, allow_none=True, sync=True) | |
444 |
|
444 | |||
445 | border_width = CUnicode(sync=True) |
|
445 | border_width = CUnicode(sync=True) | |
446 | border_radius = CUnicode(sync=True) |
|
446 | border_radius = CUnicode(sync=True) | |
447 | border_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_border-style.asp |
|
447 | border_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_border-style.asp | |
448 | 'none', |
|
448 | 'none', | |
449 | 'hidden', |
|
449 | 'hidden', | |
450 | 'dotted', |
|
450 | 'dotted', | |
451 | 'dashed', |
|
451 | 'dashed', | |
452 | 'solid', |
|
452 | 'solid', | |
453 | 'double', |
|
453 | 'double', | |
454 | 'groove', |
|
454 | 'groove', | |
455 | 'ridge', |
|
455 | 'ridge', | |
456 | 'inset', |
|
456 | 'inset', | |
457 | 'outset', |
|
457 | 'outset', | |
458 | 'initial', |
|
458 | 'initial', | |
459 | 'inherit', ''], |
|
459 | 'inherit', ''], | |
460 | default_value='', sync=True) |
|
460 | default_value='', sync=True) | |
461 |
|
461 | |||
462 | font_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_font-style.asp |
|
462 | font_style = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_font-style.asp | |
463 | 'normal', |
|
463 | 'normal', | |
464 | 'italic', |
|
464 | 'italic', | |
465 | 'oblique', |
|
465 | 'oblique', | |
466 | 'initial', |
|
466 | 'initial', | |
467 | 'inherit', ''], |
|
467 | 'inherit', ''], | |
468 | default_value='', sync=True) |
|
468 | default_value='', sync=True) | |
469 | font_weight = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_weight.asp |
|
469 | font_weight = CaselessStrEnum(values=[ # http://www.w3schools.com/cssref/pr_font_weight.asp | |
470 | 'normal', |
|
470 | 'normal', | |
471 | 'bold', |
|
471 | 'bold', | |
472 | 'bolder', |
|
472 | 'bolder', | |
473 | 'lighter', |
|
473 | 'lighter', | |
474 | 'initial', |
|
474 | 'initial', | |
475 | 'inherit', ''] + list(map(str, range(100,1000,100))), |
|
475 | 'inherit', ''] + list(map(str, range(100,1000,100))), | |
476 | default_value='', sync=True) |
|
476 | default_value='', sync=True) | |
477 | font_size = CUnicode(sync=True) |
|
477 | font_size = CUnicode(sync=True) | |
478 | font_family = Unicode(sync=True) |
|
478 | font_family = Unicode(sync=True) | |
479 |
|
479 | |||
480 | def __init__(self, *pargs, **kwargs): |
|
480 | def __init__(self, *pargs, **kwargs): | |
481 | super(DOMWidget, self).__init__(*pargs, **kwargs) |
|
481 | super(DOMWidget, self).__init__(*pargs, **kwargs) | |
482 |
|
482 | |||
483 | def _validate_border(name, old, new): |
|
483 | def _validate_border(name, old, new): | |
484 | if new is not None and new != '': |
|
484 | if new is not None and new != '': | |
485 | if name != 'border_width' and not self.border_width: |
|
485 | if name != 'border_width' and not self.border_width: | |
486 | self.border_width = 1 |
|
486 | self.border_width = 1 | |
487 | if name != 'border_style' and self.border_style == '': |
|
487 | if name != 'border_style' and self.border_style == '': | |
488 | self.border_style = 'solid' |
|
488 | self.border_style = 'solid' | |
489 | self.on_trait_change(_validate_border, ['border_width', 'border_style', 'border_color']) |
|
489 | self.on_trait_change(_validate_border, ['border_width', 'border_style', 'border_color']) |
@@ -1,296 +1,296 | |||||
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, Color | |
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): |
|
28 | def __init__(self, value=None, **kwargs): | |
29 | if value is not None: |
|
29 | if value is not None: | |
30 | kwargs['value'] = value |
|
30 | kwargs['value'] = value | |
31 | super(_Float, self).__init__(**kwargs) |
|
31 | super(_Float, self).__init__(**kwargs) | |
32 |
|
32 | |||
33 | class _BoundedFloat(_Float): |
|
33 | class _BoundedFloat(_Float): | |
34 | max = CFloat(100.0, help="Max value", sync=True) |
|
34 | max = CFloat(100.0, help="Max value", sync=True) | |
35 | min = CFloat(0.0, help="Min value", sync=True) |
|
35 | min = CFloat(0.0, help="Min value", sync=True) | |
36 | 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) | |
37 |
|
37 | |||
38 | def __init__(self, *pargs, **kwargs): |
|
38 | def __init__(self, *pargs, **kwargs): | |
39 | """Constructor""" |
|
39 | """Constructor""" | |
40 | super(_BoundedFloat, self).__init__(*pargs, **kwargs) |
|
40 | super(_BoundedFloat, self).__init__(*pargs, **kwargs) | |
41 | self._handle_value_changed('value', None, self.value) |
|
41 | self._handle_value_changed('value', None, self.value) | |
42 | self._handle_max_changed('max', None, self.max) |
|
42 | self._handle_max_changed('max', None, self.max) | |
43 | self._handle_min_changed('min', None, self.min) |
|
43 | self._handle_min_changed('min', None, self.min) | |
44 | self.on_trait_change(self._handle_value_changed, 'value') |
|
44 | self.on_trait_change(self._handle_value_changed, 'value') | |
45 | self.on_trait_change(self._handle_max_changed, 'max') |
|
45 | self.on_trait_change(self._handle_max_changed, 'max') | |
46 | self.on_trait_change(self._handle_min_changed, 'min') |
|
46 | self.on_trait_change(self._handle_min_changed, 'min') | |
47 |
|
47 | |||
48 | def _handle_value_changed(self, name, old, new): |
|
48 | def _handle_value_changed(self, name, old, new): | |
49 | """Validate value.""" |
|
49 | """Validate value.""" | |
50 | if self.min > new or new > self.max: |
|
50 | if self.min > new or new > self.max: | |
51 | self.value = min(max(new, self.min), self.max) |
|
51 | self.value = min(max(new, self.min), self.max) | |
52 |
|
52 | |||
53 | def _handle_max_changed(self, name, old, new): |
|
53 | def _handle_max_changed(self, name, old, new): | |
54 | """Make sure the min is always <= the max.""" |
|
54 | """Make sure the min is always <= the max.""" | |
55 | if new < self.min: |
|
55 | if new < self.min: | |
56 | raise ValueError("setting max < min") |
|
56 | raise ValueError("setting max < min") | |
57 | if new < self.value: |
|
57 | if new < self.value: | |
58 | self.value = new |
|
58 | self.value = new | |
59 |
|
59 | |||
60 | def _handle_min_changed(self, name, old, new): |
|
60 | def _handle_min_changed(self, name, old, new): | |
61 | """Make sure the max is always >= the min.""" |
|
61 | """Make sure the max is always >= the min.""" | |
62 | if new > self.max: |
|
62 | if new > self.max: | |
63 | raise ValueError("setting min > max") |
|
63 | raise ValueError("setting min > max") | |
64 | if new > self.value: |
|
64 | if new > self.value: | |
65 | self.value = new |
|
65 | self.value = new | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | @register('IPython.FloatText') |
|
68 | @register('IPython.FloatText') | |
69 | class FloatText(_Float): |
|
69 | class FloatText(_Float): | |
70 | """ Displays a float value within a textbox. For a textbox in |
|
70 | """ Displays a float value within a textbox. For a textbox in | |
71 | which the value must be within a specific range, use BoundedFloatText. |
|
71 | which the value must be within a specific range, use BoundedFloatText. | |
72 |
|
72 | |||
73 | Parameters |
|
73 | Parameters | |
74 | ---------- |
|
74 | ---------- | |
75 | value : float |
|
75 | value : float | |
76 | value displayed |
|
76 | value displayed | |
77 | description : str |
|
77 | description : str | |
78 | description displayed next to the textbox |
|
78 | description displayed next to the textbox | |
79 | color : str Unicode color code (eg. '#C13535'), optional |
|
79 | color : str Unicode color code (eg. '#C13535'), optional | |
80 | color of the value displayed |
|
80 | color of the value displayed | |
81 | """ |
|
81 | """ | |
82 | _view_name = Unicode('FloatTextView', sync=True) |
|
82 | _view_name = Unicode('FloatTextView', sync=True) | |
83 |
|
83 | |||
84 |
|
84 | |||
85 | @register('IPython.BoundedFloatText') |
|
85 | @register('IPython.BoundedFloatText') | |
86 | class BoundedFloatText(_BoundedFloat): |
|
86 | class BoundedFloatText(_BoundedFloat): | |
87 | """ Displays a float value within a textbox. Value must be within the range specified. |
|
87 | """ Displays a float value within a textbox. Value must be within the range specified. | |
88 | For a textbox in which the value doesn't need to be within a specific range, use FloatText. |
|
88 | For a textbox in which the value doesn't need to be within a specific range, use FloatText. | |
89 |
|
89 | |||
90 | Parameters |
|
90 | Parameters | |
91 | ---------- |
|
91 | ---------- | |
92 | value : float |
|
92 | value : float | |
93 | value displayed |
|
93 | value displayed | |
94 | min : float |
|
94 | min : float | |
95 | minimal value of the range of possible values displayed |
|
95 | minimal value of the range of possible values displayed | |
96 | max : float |
|
96 | max : float | |
97 | maximal value of the range of possible values displayed |
|
97 | maximal value of the range of possible values displayed | |
98 | description : str |
|
98 | description : str | |
99 | description displayed next to the textbox |
|
99 | description displayed next to the textbox | |
100 | color : str Unicode color code (eg. '#C13535'), optional |
|
100 | color : str Unicode color code (eg. '#C13535'), optional | |
101 | color of the value displayed |
|
101 | color of the value displayed | |
102 | """ |
|
102 | """ | |
103 | _view_name = Unicode('FloatTextView', sync=True) |
|
103 | _view_name = Unicode('FloatTextView', sync=True) | |
104 |
|
104 | |||
105 |
|
105 | |||
106 | @register('IPython.FloatSlider') |
|
106 | @register('IPython.FloatSlider') | |
107 | class FloatSlider(_BoundedFloat): |
|
107 | class FloatSlider(_BoundedFloat): | |
108 | """ Slider/trackbar of floating values with the specified range. |
|
108 | """ Slider/trackbar of floating values with the specified range. | |
109 |
|
109 | |||
110 | Parameters |
|
110 | Parameters | |
111 | ---------- |
|
111 | ---------- | |
112 | value : float |
|
112 | value : float | |
113 | position of the slider |
|
113 | position of the slider | |
114 | min : float |
|
114 | min : float | |
115 | minimal position of the slider |
|
115 | minimal position of the slider | |
116 | max : float |
|
116 | max : float | |
117 | maximal position of the slider |
|
117 | maximal position of the slider | |
118 | step : float |
|
118 | step : float | |
119 | step of the trackbar |
|
119 | step of the trackbar | |
120 | description : str |
|
120 | description : str | |
121 | name of the slider |
|
121 | name of the slider | |
122 | orientation : {'vertical', 'horizontal}, optional |
|
122 | orientation : {'vertical', 'horizontal}, optional | |
123 | default is horizontal |
|
123 | default is horizontal | |
124 | readout : {True, False}, optional |
|
124 | readout : {True, False}, optional | |
125 | default is True, display the current value of the slider next to it |
|
125 | default is True, display the current value of the slider next to it | |
126 | slider_color : str Unicode color code (eg. '#C13535'), optional |
|
126 | slider_color : str Unicode color code (eg. '#C13535'), optional | |
127 | color of the slider |
|
127 | color of the slider | |
128 | color : str Unicode color code (eg. '#C13535'), optional |
|
128 | color : str Unicode color code (eg. '#C13535'), optional | |
129 | color of the value displayed (if readout == True) |
|
129 | color of the value displayed (if readout == True) | |
130 | """ |
|
130 | """ | |
131 | _view_name = Unicode('FloatSliderView', sync=True) |
|
131 | _view_name = Unicode('FloatSliderView', sync=True) | |
132 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
132 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | |
133 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
133 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
134 | _range = Bool(False, help="Display a range selector", sync=True) |
|
134 | _range = Bool(False, help="Display a range selector", sync=True) | |
135 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
135 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
136 |
slider_color = |
|
136 | slider_color = Color(None, allow_none=True, sync=True) | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | @register('IPython.FloatProgress') |
|
139 | @register('IPython.FloatProgress') | |
140 | class FloatProgress(_BoundedFloat): |
|
140 | class FloatProgress(_BoundedFloat): | |
141 | """ Displays a progress bar. |
|
141 | """ Displays a progress bar. | |
142 |
|
142 | |||
143 | Parameters |
|
143 | Parameters | |
144 | ----------- |
|
144 | ----------- | |
145 | value : float |
|
145 | value : float | |
146 | position within the range of the progress bar |
|
146 | position within the range of the progress bar | |
147 | min : float |
|
147 | min : float | |
148 | minimal position of the slider |
|
148 | minimal position of the slider | |
149 | max : float |
|
149 | max : float | |
150 | maximal position of the slider |
|
150 | maximal position of the slider | |
151 | step : float |
|
151 | step : float | |
152 | step of the progress bar |
|
152 | step of the progress bar | |
153 | description : str |
|
153 | description : str | |
154 | name of the progress bar |
|
154 | name of the progress bar | |
155 | bar_style: {'success', 'info', 'warning', 'danger', ''}, optional |
|
155 | bar_style: {'success', 'info', 'warning', 'danger', ''}, optional | |
156 | color of the progress bar, default is '' (blue) |
|
156 | color of the progress bar, default is '' (blue) | |
157 | colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red |
|
157 | colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red | |
158 | """ |
|
158 | """ | |
159 | _view_name = Unicode('ProgressView', sync=True) |
|
159 | _view_name = Unicode('ProgressView', sync=True) | |
160 |
|
160 | |||
161 | bar_style = CaselessStrEnum( |
|
161 | bar_style = CaselessStrEnum( | |
162 | values=['success', 'info', 'warning', 'danger', ''], |
|
162 | values=['success', 'info', 'warning', 'danger', ''], | |
163 | default_value='', allow_none=True, sync=True, help="""Use a |
|
163 | default_value='', allow_none=True, sync=True, help="""Use a | |
164 | predefined styling for the progess bar.""") |
|
164 | predefined styling for the progess bar.""") | |
165 |
|
165 | |||
166 | class _FloatRange(_Float): |
|
166 | class _FloatRange(_Float): | |
167 | value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True) |
|
167 | value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True) | |
168 | lower = CFloat(0.0, help="Lower bound", sync=False) |
|
168 | lower = CFloat(0.0, help="Lower bound", sync=False) | |
169 | upper = CFloat(1.0, help="Upper bound", sync=False) |
|
169 | upper = CFloat(1.0, help="Upper bound", sync=False) | |
170 |
|
170 | |||
171 | def __init__(self, *pargs, **kwargs): |
|
171 | def __init__(self, *pargs, **kwargs): | |
172 | value_given = 'value' in kwargs |
|
172 | value_given = 'value' in kwargs | |
173 | lower_given = 'lower' in kwargs |
|
173 | lower_given = 'lower' in kwargs | |
174 | upper_given = 'upper' in kwargs |
|
174 | upper_given = 'upper' in kwargs | |
175 | if value_given and (lower_given or upper_given): |
|
175 | if value_given and (lower_given or upper_given): | |
176 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
176 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") | |
177 | if lower_given != upper_given: |
|
177 | if lower_given != upper_given: | |
178 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") |
|
178 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") | |
179 |
|
179 | |||
180 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
180 | DOMWidget.__init__(self, *pargs, **kwargs) | |
181 |
|
181 | |||
182 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
182 | # ensure the traits match, preferring whichever (if any) was given in kwargs | |
183 | if value_given: |
|
183 | if value_given: | |
184 | self.lower, self.upper = self.value |
|
184 | self.lower, self.upper = self.value | |
185 | else: |
|
185 | else: | |
186 | self.value = (self.lower, self.upper) |
|
186 | self.value = (self.lower, self.upper) | |
187 |
|
187 | |||
188 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
188 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) | |
189 |
|
189 | |||
190 | def _validate(self, name, old, new): |
|
190 | def _validate(self, name, old, new): | |
191 | if name == 'value': |
|
191 | if name == 'value': | |
192 | self.lower, self.upper = min(new), max(new) |
|
192 | self.lower, self.upper = min(new), max(new) | |
193 | elif name == 'lower': |
|
193 | elif name == 'lower': | |
194 | self.value = (new, self.value[1]) |
|
194 | self.value = (new, self.value[1]) | |
195 | elif name == 'upper': |
|
195 | elif name == 'upper': | |
196 | self.value = (self.value[0], new) |
|
196 | self.value = (self.value[0], new) | |
197 |
|
197 | |||
198 | class _BoundedFloatRange(_FloatRange): |
|
198 | class _BoundedFloatRange(_FloatRange): | |
199 | step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
199 | step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
200 | max = CFloat(100.0, help="Max value", sync=True) |
|
200 | max = CFloat(100.0, help="Max value", sync=True) | |
201 | min = CFloat(0.0, help="Min value", sync=True) |
|
201 | min = CFloat(0.0, help="Min value", sync=True) | |
202 |
|
202 | |||
203 | def __init__(self, *pargs, **kwargs): |
|
203 | def __init__(self, *pargs, **kwargs): | |
204 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
204 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs | |
205 | _FloatRange.__init__(self, *pargs, **kwargs) |
|
205 | _FloatRange.__init__(self, *pargs, **kwargs) | |
206 |
|
206 | |||
207 | # ensure a minimal amount of sanity |
|
207 | # ensure a minimal amount of sanity | |
208 | if self.min > self.max: |
|
208 | if self.min > self.max: | |
209 | raise ValueError("min must be <= max") |
|
209 | raise ValueError("min must be <= max") | |
210 |
|
210 | |||
211 | if any_value_given: |
|
211 | if any_value_given: | |
212 | # if a value was given, clamp it within (min, max) |
|
212 | # if a value was given, clamp it within (min, max) | |
213 | self._validate("value", None, self.value) |
|
213 | self._validate("value", None, self.value) | |
214 | else: |
|
214 | else: | |
215 | # otherwise, set it to 25-75% to avoid the handles overlapping |
|
215 | # otherwise, set it to 25-75% to avoid the handles overlapping | |
216 | self.value = (0.75*self.min + 0.25*self.max, |
|
216 | self.value = (0.75*self.min + 0.25*self.max, | |
217 | 0.25*self.min + 0.75*self.max) |
|
217 | 0.25*self.min + 0.75*self.max) | |
218 | # callback already set for 'value', 'lower', 'upper' |
|
218 | # callback already set for 'value', 'lower', 'upper' | |
219 | self.on_trait_change(self._validate, ['min', 'max']) |
|
219 | self.on_trait_change(self._validate, ['min', 'max']) | |
220 |
|
220 | |||
221 |
|
221 | |||
222 | def _validate(self, name, old, new): |
|
222 | def _validate(self, name, old, new): | |
223 | if name == "min": |
|
223 | if name == "min": | |
224 | if new > self.max: |
|
224 | if new > self.max: | |
225 | raise ValueError("setting min > max") |
|
225 | raise ValueError("setting min > max") | |
226 | self.min = new |
|
226 | self.min = new | |
227 | elif name == "max": |
|
227 | elif name == "max": | |
228 | if new < self.min: |
|
228 | if new < self.min: | |
229 | raise ValueError("setting max < min") |
|
229 | raise ValueError("setting max < min") | |
230 | self.max = new |
|
230 | self.max = new | |
231 |
|
231 | |||
232 | low, high = self.value |
|
232 | low, high = self.value | |
233 | if name == "value": |
|
233 | if name == "value": | |
234 | low, high = min(new), max(new) |
|
234 | low, high = min(new), max(new) | |
235 | elif name == "upper": |
|
235 | elif name == "upper": | |
236 | if new < self.lower: |
|
236 | if new < self.lower: | |
237 | raise ValueError("setting upper < lower") |
|
237 | raise ValueError("setting upper < lower") | |
238 | high = new |
|
238 | high = new | |
239 | elif name == "lower": |
|
239 | elif name == "lower": | |
240 | if new > self.upper: |
|
240 | if new > self.upper: | |
241 | raise ValueError("setting lower > upper") |
|
241 | raise ValueError("setting lower > upper") | |
242 | low = new |
|
242 | low = new | |
243 |
|
243 | |||
244 | low = max(self.min, min(low, self.max)) |
|
244 | low = max(self.min, min(low, self.max)) | |
245 | high = min(self.max, max(high, self.min)) |
|
245 | high = min(self.max, max(high, self.min)) | |
246 |
|
246 | |||
247 | # determine the order in which we should update the |
|
247 | # determine the order in which we should update the | |
248 | # lower, upper traits to avoid a temporary inverted overlap |
|
248 | # lower, upper traits to avoid a temporary inverted overlap | |
249 | lower_first = high < self.lower |
|
249 | lower_first = high < self.lower | |
250 |
|
250 | |||
251 | self.value = (low, high) |
|
251 | self.value = (low, high) | |
252 | if lower_first: |
|
252 | if lower_first: | |
253 | self.lower = low |
|
253 | self.lower = low | |
254 | self.upper = high |
|
254 | self.upper = high | |
255 | else: |
|
255 | else: | |
256 | self.upper = high |
|
256 | self.upper = high | |
257 | self.lower = low |
|
257 | self.lower = low | |
258 |
|
258 | |||
259 |
|
259 | |||
260 | @register('IPython.FloatRangeSlider') |
|
260 | @register('IPython.FloatRangeSlider') | |
261 | class FloatRangeSlider(_BoundedFloatRange): |
|
261 | class FloatRangeSlider(_BoundedFloatRange): | |
262 | """ Slider/trackbar for displaying a floating value range (within the specified range of values). |
|
262 | """ Slider/trackbar for displaying a floating value range (within the specified range of values). | |
263 |
|
263 | |||
264 | Parameters |
|
264 | Parameters | |
265 | ---------- |
|
265 | ---------- | |
266 | value : float tuple |
|
266 | value : float tuple | |
267 | range of the slider displayed |
|
267 | range of the slider displayed | |
268 | min : float |
|
268 | min : float | |
269 | minimal position of the slider |
|
269 | minimal position of the slider | |
270 | max : float |
|
270 | max : float | |
271 | maximal position of the slider |
|
271 | maximal position of the slider | |
272 | step : float |
|
272 | step : float | |
273 | step of the trackbar |
|
273 | step of the trackbar | |
274 | description : str |
|
274 | description : str | |
275 | name of the slider |
|
275 | name of the slider | |
276 | orientation : {'vertical', 'horizontal}, optional |
|
276 | orientation : {'vertical', 'horizontal}, optional | |
277 | default is horizontal |
|
277 | default is horizontal | |
278 | readout : {True, False}, optional |
|
278 | readout : {True, False}, optional | |
279 | default is True, display the current value of the slider next to it |
|
279 | default is True, display the current value of the slider next to it | |
280 | slider_color : str Unicode color code (eg. '#C13535'), optional |
|
280 | slider_color : str Unicode color code (eg. '#C13535'), optional | |
281 | color of the slider |
|
281 | color of the slider | |
282 | color : str Unicode color code (eg. '#C13535'), optional |
|
282 | color : str Unicode color code (eg. '#C13535'), optional | |
283 | color of the value displayed (if readout == True) |
|
283 | color of the value displayed (if readout == True) | |
284 | """ |
|
284 | """ | |
285 | _view_name = Unicode('FloatSliderView', sync=True) |
|
285 | _view_name = Unicode('FloatSliderView', sync=True) | |
286 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
286 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | |
287 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
287 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
288 | _range = Bool(True, help="Display a range selector", sync=True) |
|
288 | _range = Bool(True, help="Display a range selector", sync=True) | |
289 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
289 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
290 |
slider_color = |
|
290 | slider_color = Color(None, allow_none=True, sync=True) | |
291 |
|
291 | |||
292 | # Remove in IPython 4.0 |
|
292 | # Remove in IPython 4.0 | |
293 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') |
|
293 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') | |
294 | BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget') |
|
294 | BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget') | |
295 | FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget') |
|
295 | FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget') | |
296 | FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget') |
|
296 | FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget') |
@@ -1,207 +1,207 | |||||
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, Color | |
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): |
|
29 | def __init__(self, value=None, **kwargs): | |
30 | if value is not None: |
|
30 | if value is not None: | |
31 | kwargs['value'] = value |
|
31 | kwargs['value'] = value | |
32 | super(_Int, self).__init__(**kwargs) |
|
32 | super(_Int, self).__init__(**kwargs) | |
33 |
|
33 | |||
34 | class _BoundedInt(_Int): |
|
34 | class _BoundedInt(_Int): | |
35 | """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 | |
36 | by a minium and maximum.""" |
|
36 | by a minium and maximum.""" | |
37 | 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) | |
38 | max = CInt(100, help="Max value", sync=True) |
|
38 | max = CInt(100, help="Max value", sync=True) | |
39 | min = CInt(0, help="Min value", sync=True) |
|
39 | min = CInt(0, help="Min value", sync=True) | |
40 |
|
40 | |||
41 | def __init__(self, *pargs, **kwargs): |
|
41 | def __init__(self, *pargs, **kwargs): | |
42 | """Constructor""" |
|
42 | """Constructor""" | |
43 | super(_BoundedInt, self).__init__(*pargs, **kwargs) |
|
43 | super(_BoundedInt, self).__init__(*pargs, **kwargs) | |
44 | self._handle_value_changed('value', None, self.value) |
|
44 | self._handle_value_changed('value', None, self.value) | |
45 | self._handle_max_changed('max', None, self.max) |
|
45 | self._handle_max_changed('max', None, self.max) | |
46 | self._handle_min_changed('min', None, self.min) |
|
46 | self._handle_min_changed('min', None, self.min) | |
47 | self.on_trait_change(self._handle_value_changed, 'value') |
|
47 | self.on_trait_change(self._handle_value_changed, 'value') | |
48 | self.on_trait_change(self._handle_max_changed, 'max') |
|
48 | self.on_trait_change(self._handle_max_changed, 'max') | |
49 | self.on_trait_change(self._handle_min_changed, 'min') |
|
49 | self.on_trait_change(self._handle_min_changed, 'min') | |
50 |
|
50 | |||
51 | def _handle_value_changed(self, name, old, new): |
|
51 | def _handle_value_changed(self, name, old, new): | |
52 | """Validate value.""" |
|
52 | """Validate value.""" | |
53 | if self.min > new or new > self.max: |
|
53 | if self.min > new or new > self.max: | |
54 | self.value = min(max(new, self.min), self.max) |
|
54 | self.value = min(max(new, self.min), self.max) | |
55 |
|
55 | |||
56 | def _handle_max_changed(self, name, old, new): |
|
56 | def _handle_max_changed(self, name, old, new): | |
57 | """Make sure the min is always <= the max.""" |
|
57 | """Make sure the min is always <= the max.""" | |
58 | if new < self.min: |
|
58 | if new < self.min: | |
59 | raise ValueError("setting max < min") |
|
59 | raise ValueError("setting max < min") | |
60 | if new < self.value: |
|
60 | if new < self.value: | |
61 | self.value = new |
|
61 | self.value = new | |
62 |
|
62 | |||
63 | def _handle_min_changed(self, name, old, new): |
|
63 | def _handle_min_changed(self, name, old, new): | |
64 | """Make sure the max is always >= the min.""" |
|
64 | """Make sure the max is always >= the min.""" | |
65 | if new > self.max: |
|
65 | if new > self.max: | |
66 | raise ValueError("setting min > max") |
|
66 | raise ValueError("setting min > max") | |
67 | if new > self.value: |
|
67 | if new > self.value: | |
68 | self.value = new |
|
68 | self.value = new | |
69 |
|
69 | |||
70 | @register('IPython.IntText') |
|
70 | @register('IPython.IntText') | |
71 | class IntText(_Int): |
|
71 | class IntText(_Int): | |
72 | """Textbox widget that represents a int.""" |
|
72 | """Textbox widget that represents a int.""" | |
73 | _view_name = Unicode('IntTextView', sync=True) |
|
73 | _view_name = Unicode('IntTextView', sync=True) | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | @register('IPython.BoundedIntText') |
|
76 | @register('IPython.BoundedIntText') | |
77 | class BoundedIntText(_BoundedInt): |
|
77 | class BoundedIntText(_BoundedInt): | |
78 | """Textbox widget that represents a int bounded by a minimum and maximum value.""" |
|
78 | """Textbox widget that represents a int bounded by a minimum and maximum value.""" | |
79 | _view_name = Unicode('IntTextView', sync=True) |
|
79 | _view_name = Unicode('IntTextView', sync=True) | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | @register('IPython.IntSlider') |
|
82 | @register('IPython.IntSlider') | |
83 | class IntSlider(_BoundedInt): |
|
83 | class IntSlider(_BoundedInt): | |
84 | """Slider widget that represents a int bounded by a minimum and maximum value.""" |
|
84 | """Slider widget that represents a int bounded by a minimum and maximum value.""" | |
85 | _view_name = Unicode('IntSliderView', sync=True) |
|
85 | _view_name = Unicode('IntSliderView', sync=True) | |
86 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
86 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | |
87 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
87 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
88 | _range = Bool(False, help="Display a range selector", sync=True) |
|
88 | _range = Bool(False, help="Display a range selector", sync=True) | |
89 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
89 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
90 |
slider_color = |
|
90 | slider_color = Color(None, allow_none=True, sync=True) | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | @register('IPython.IntProgress') |
|
93 | @register('IPython.IntProgress') | |
94 | class IntProgress(_BoundedInt): |
|
94 | class IntProgress(_BoundedInt): | |
95 | """Progress bar that represents a int bounded by a minimum and maximum value.""" |
|
95 | """Progress bar that represents a int bounded by a minimum and maximum value.""" | |
96 | _view_name = Unicode('ProgressView', sync=True) |
|
96 | _view_name = Unicode('ProgressView', sync=True) | |
97 |
|
97 | |||
98 | bar_style = CaselessStrEnum( |
|
98 | bar_style = CaselessStrEnum( | |
99 | values=['success', 'info', 'warning', 'danger', ''], |
|
99 | values=['success', 'info', 'warning', 'danger', ''], | |
100 | default_value='', allow_none=True, sync=True, help="""Use a |
|
100 | default_value='', allow_none=True, sync=True, help="""Use a | |
101 | predefined styling for the progess bar.""") |
|
101 | predefined styling for the progess bar.""") | |
102 |
|
102 | |||
103 | class _IntRange(_Int): |
|
103 | class _IntRange(_Int): | |
104 | value = Tuple(CInt, CInt, default_value=(0, 1), help="Tuple of (lower, upper) bounds", sync=True) |
|
104 | value = Tuple(CInt, CInt, default_value=(0, 1), help="Tuple of (lower, upper) bounds", sync=True) | |
105 | lower = CInt(0, help="Lower bound", sync=False) |
|
105 | lower = CInt(0, help="Lower bound", sync=False) | |
106 | upper = CInt(1, help="Upper bound", sync=False) |
|
106 | upper = CInt(1, help="Upper bound", sync=False) | |
107 |
|
107 | |||
108 | def __init__(self, *pargs, **kwargs): |
|
108 | def __init__(self, *pargs, **kwargs): | |
109 | value_given = 'value' in kwargs |
|
109 | value_given = 'value' in kwargs | |
110 | lower_given = 'lower' in kwargs |
|
110 | lower_given = 'lower' in kwargs | |
111 | upper_given = 'upper' in kwargs |
|
111 | upper_given = 'upper' in kwargs | |
112 | if value_given and (lower_given or upper_given): |
|
112 | if value_given and (lower_given or upper_given): | |
113 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
113 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") | |
114 | if lower_given != upper_given: |
|
114 | if lower_given != upper_given: | |
115 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") |
|
115 | raise ValueError("Must specify both 'lower' and 'upper' for range widget") | |
116 |
|
116 | |||
117 | super(_IntRange, self).__init__(*pargs, **kwargs) |
|
117 | super(_IntRange, self).__init__(*pargs, **kwargs) | |
118 |
|
118 | |||
119 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
119 | # ensure the traits match, preferring whichever (if any) was given in kwargs | |
120 | if value_given: |
|
120 | if value_given: | |
121 | self.lower, self.upper = self.value |
|
121 | self.lower, self.upper = self.value | |
122 | else: |
|
122 | else: | |
123 | self.value = (self.lower, self.upper) |
|
123 | self.value = (self.lower, self.upper) | |
124 |
|
124 | |||
125 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
125 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) | |
126 |
|
126 | |||
127 | def _validate(self, name, old, new): |
|
127 | def _validate(self, name, old, new): | |
128 | if name == 'value': |
|
128 | if name == 'value': | |
129 | self.lower, self.upper = min(new), max(new) |
|
129 | self.lower, self.upper = min(new), max(new) | |
130 | elif name == 'lower': |
|
130 | elif name == 'lower': | |
131 | self.value = (new, self.value[1]) |
|
131 | self.value = (new, self.value[1]) | |
132 | elif name == 'upper': |
|
132 | elif name == 'upper': | |
133 | self.value = (self.value[0], new) |
|
133 | self.value = (self.value[0], new) | |
134 |
|
134 | |||
135 | class _BoundedIntRange(_IntRange): |
|
135 | class _BoundedIntRange(_IntRange): | |
136 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
136 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
137 | max = CInt(100, help="Max value", sync=True) |
|
137 | max = CInt(100, help="Max value", sync=True) | |
138 | min = CInt(0, help="Min value", sync=True) |
|
138 | min = CInt(0, help="Min value", sync=True) | |
139 |
|
139 | |||
140 | def __init__(self, *pargs, **kwargs): |
|
140 | def __init__(self, *pargs, **kwargs): | |
141 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
141 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs | |
142 | _IntRange.__init__(self, *pargs, **kwargs) |
|
142 | _IntRange.__init__(self, *pargs, **kwargs) | |
143 |
|
143 | |||
144 | # ensure a minimal amount of sanity |
|
144 | # ensure a minimal amount of sanity | |
145 | if self.min > self.max: |
|
145 | if self.min > self.max: | |
146 | raise ValueError("min must be <= max") |
|
146 | raise ValueError("min must be <= max") | |
147 |
|
147 | |||
148 | if any_value_given: |
|
148 | if any_value_given: | |
149 | # if a value was given, clamp it within (min, max) |
|
149 | # if a value was given, clamp it within (min, max) | |
150 | self._validate("value", None, self.value) |
|
150 | self._validate("value", None, self.value) | |
151 | else: |
|
151 | else: | |
152 | # otherwise, set it to 25-75% to avoid the handles overlapping |
|
152 | # otherwise, set it to 25-75% to avoid the handles overlapping | |
153 | self.value = (0.75*self.min + 0.25*self.max, |
|
153 | self.value = (0.75*self.min + 0.25*self.max, | |
154 | 0.25*self.min + 0.75*self.max) |
|
154 | 0.25*self.min + 0.75*self.max) | |
155 | # callback already set for 'value', 'lower', 'upper' |
|
155 | # callback already set for 'value', 'lower', 'upper' | |
156 | self.on_trait_change(self._validate, ['min', 'max']) |
|
156 | self.on_trait_change(self._validate, ['min', 'max']) | |
157 |
|
157 | |||
158 | def _validate(self, name, old, new): |
|
158 | def _validate(self, name, old, new): | |
159 | if name == "min": |
|
159 | if name == "min": | |
160 | if new > self.max: |
|
160 | if new > self.max: | |
161 | raise ValueError("setting min > max") |
|
161 | raise ValueError("setting min > max") | |
162 | elif name == "max": |
|
162 | elif name == "max": | |
163 | if new < self.min: |
|
163 | if new < self.min: | |
164 | raise ValueError("setting max < min") |
|
164 | raise ValueError("setting max < min") | |
165 |
|
165 | |||
166 | low, high = self.value |
|
166 | low, high = self.value | |
167 | if name == "value": |
|
167 | if name == "value": | |
168 | low, high = min(new), max(new) |
|
168 | low, high = min(new), max(new) | |
169 | elif name == "upper": |
|
169 | elif name == "upper": | |
170 | if new < self.lower: |
|
170 | if new < self.lower: | |
171 | raise ValueError("setting upper < lower") |
|
171 | raise ValueError("setting upper < lower") | |
172 | high = new |
|
172 | high = new | |
173 | elif name == "lower": |
|
173 | elif name == "lower": | |
174 | if new > self.upper: |
|
174 | if new > self.upper: | |
175 | raise ValueError("setting lower > upper") |
|
175 | raise ValueError("setting lower > upper") | |
176 | low = new |
|
176 | low = new | |
177 |
|
177 | |||
178 | low = max(self.min, min(low, self.max)) |
|
178 | low = max(self.min, min(low, self.max)) | |
179 | high = min(self.max, max(high, self.min)) |
|
179 | high = min(self.max, max(high, self.min)) | |
180 |
|
180 | |||
181 | # determine the order in which we should update the |
|
181 | # determine the order in which we should update the | |
182 | # lower, upper traits to avoid a temporary inverted overlap |
|
182 | # lower, upper traits to avoid a temporary inverted overlap | |
183 | lower_first = high < self.lower |
|
183 | lower_first = high < self.lower | |
184 |
|
184 | |||
185 | self.value = (low, high) |
|
185 | self.value = (low, high) | |
186 | if lower_first: |
|
186 | if lower_first: | |
187 | self.lower = low |
|
187 | self.lower = low | |
188 | self.upper = high |
|
188 | self.upper = high | |
189 | else: |
|
189 | else: | |
190 | self.upper = high |
|
190 | self.upper = high | |
191 | self.lower = low |
|
191 | self.lower = low | |
192 |
|
192 | |||
193 | @register('IPython.IntRangeSlider') |
|
193 | @register('IPython.IntRangeSlider') | |
194 | class IntRangeSlider(_BoundedIntRange): |
|
194 | class IntRangeSlider(_BoundedIntRange): | |
195 | """Slider widget that represents a pair of ints between a minimum and maximum value.""" |
|
195 | """Slider widget that represents a pair of ints between a minimum and maximum value.""" | |
196 | _view_name = Unicode('IntSliderView', sync=True) |
|
196 | _view_name = Unicode('IntSliderView', sync=True) | |
197 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], |
|
197 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | |
198 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
198 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
199 | _range = Bool(True, help="Display a range selector", sync=True) |
|
199 | _range = Bool(True, help="Display a range selector", sync=True) | |
200 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
200 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
201 |
slider_color = |
|
201 | slider_color = Color(None, allow_none=True, sync=True) | |
202 |
|
202 | |||
203 | # Remove in IPython 4.0 |
|
203 | # Remove in IPython 4.0 | |
204 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') |
|
204 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') | |
205 | BoundedIntTextWidget = DeprecatedClass(BoundedIntText, 'BoundedIntTextWidget') |
|
205 | BoundedIntTextWidget = DeprecatedClass(BoundedIntText, 'BoundedIntTextWidget') | |
206 | IntSliderWidget = DeprecatedClass(IntSlider, 'IntSliderWidget') |
|
206 | IntSliderWidget = DeprecatedClass(IntSlider, 'IntSliderWidget') | |
207 | IntProgressWidget = DeprecatedClass(IntProgress, 'IntProgressWidget') |
|
207 | IntProgressWidget = DeprecatedClass(IntProgress, 'IntProgressWidget') |
General Comments 0
You need to be logged in to leave comments.
Login now