@@ -48,7 +48,7 @@ def _json_to_widget(x, obj):
48
48
class CallbackDispatcher (LoggingConfigurable ):
49
49
"""A structure for registering and running callbacks"""
50
50
callbacks = List ()
51
-
51
+
52
52
def __call__ (self , * args , ** kwargs ):
53
53
"""Call all of the registered callbacks."""
54
54
value = None
@@ -74,7 +74,7 @@ def register_callback(self, callback, remove=False):
74
74
Method to be registered or unregistered.
75
75
remove=False: bool
76
76
Whether to unregister the callback."""
77
-
77
+
78
78
# (Un)Register the callback.
79
79
if remove and callback in self .callbacks :
80
80
self .callbacks .remove (callback )
@@ -96,7 +96,7 @@ def m(self, *args, **kwargs):
96
96
97
97
98
98
def register (key = None ):
99
- """Returns a decorator registering a widget class in the widget registry.
99
+ """Returns a decorator registering a widget class in the widget registry.
100
100
If no key is provided, the class name is used as a key. A key is
101
101
provided for each core IPython widget so that the frontend can use
102
102
this key regardless of the language of the kernel"""
@@ -141,29 +141,29 @@ def handle_comm_opened(comm, msg):
141
141
# Traits
142
142
#-------------------------------------------------------------------------
143
143
_model_module = Unicode (None , allow_none = True , help = """A requirejs module name
144
- in which to find _model_name. If empty, look in the global registry.""" )
145
- _model_name = Unicode ('WidgetModel' , help = """Name of the backbone model
146
- registered in the front-end to create and sync this widget with.""" )
144
+ in which to find _model_name. If empty, look in the global registry.""" , sync = True )
145
+ _model_name = Unicode ('WidgetModel' , help = """Name of the backbone model
146
+ registered in the front-end to create and sync this widget with.""" , sync = True )
147
147
_view_module = Unicode (help = """A requirejs module in which to find _view_name.
148
148
If empty, look in the global registry.""" , sync = True )
149
149
_view_name = Unicode (None , allow_none = True , help = """Default view registered in the front-end
150
150
to use to represent the widget.""" , sync = True )
151
151
comm = Instance ('ipykernel.comm.Comm' , allow_none = True )
152
-
153
- msg_throttle = Int (3 , sync = True , help = """Maximum number of msgs the
152
+
153
+ msg_throttle = Int (3 , sync = True , help = """Maximum number of msgs the
154
154
front-end can send before receiving an idle msg from the back-end.""" )
155
-
155
+
156
156
version = Int (0 , sync = True , help = """Widget's version""" )
157
157
keys = List ()
158
158
def _keys_default (self ):
159
159
return [name for name in self .traits (sync = True )]
160
-
160
+
161
161
_property_lock = Dict ()
162
162
_holding_sync = False
163
163
_states_to_send = Set ()
164
164
_display_callbacks = Instance (CallbackDispatcher , ())
165
165
_msg_callbacks = Instance (CallbackDispatcher , ())
166
-
166
+
167
167
#-------------------------------------------------------------------------
168
168
# (Con/de)structor
169
169
#-------------------------------------------------------------------------
@@ -187,8 +187,7 @@ def open(self):
187
187
"""Open a comm to the frontend if one isn't already open."""
188
188
if self .comm is None :
189
189
args = dict (target_name = 'ipython.widget' ,
190
- data = {'model_name' : self ._model_name ,
191
- 'model_module' : self ._model_module })
190
+ data = self .get_state ())
192
191
if self ._model_id is not None :
193
192
args ['comm_id' ] = self ._model_id
194
193
self .comm = Comm (** args )
@@ -198,12 +197,9 @@ def _comm_changed(self, name, new):
198
197
if new is None :
199
198
return
200
199
self ._model_id = self .model_id
201
-
200
+
202
201
self .comm .on_msg (self ._handle_msg )
203
202
Widget .widgets [self .model_id ] = self
204
-
205
- # first update
206
- self .send_state ()
207
203
208
204
@property
209
205
def model_id (self ):
@@ -220,7 +216,7 @@ def __setattr__(self, name, value):
220
216
"""Overload of HasTraits.__setattr__to handle read-only-ness of widget
221
217
attributes """
222
218
if (self ._read_only_enabled and self .has_trait (name ) and
223
- self .trait_metadata (name , 'read_only' )):
219
+ self .trait_metadata (name , 'read_only' )):
224
220
raise TraitError ('Widget attribute "%s" is read-only.' % name )
225
221
else :
226
222
super (Widget , self ).__setattr__ (name , value )
@@ -236,7 +232,7 @@ def close(self):
236
232
Widget .widgets .pop (self .model_id , None )
237
233
self .comm .close ()
238
234
self .comm = None
239
-
235
+
240
236
def send_state (self , key = None ):
241
237
"""Sends the widget state, or a piece of it, to the front-end.
242
238
@@ -316,9 +312,9 @@ def on_msg(self, callback, remove=False):
316
312
----------
317
313
callback: callable
318
314
callback will be passed three arguments when a message arrives::
319
-
315
+
320
316
callback(widget, content, buffers)
321
-
317
+
322
318
remove: bool
323
319
True if the callback should be unregistered."""
324
320
self ._msg_callbacks .register_callback (callback , remove = remove )
@@ -330,9 +326,9 @@ def on_displayed(self, callback, remove=False):
330
326
----------
331
327
callback: method handler
332
328
Must have a signature of::
333
-
329
+
334
330
callback(widget, **kwargs)
335
-
331
+
336
332
kwargs from display are passed through without modification.
337
333
remove: bool
338
334
True if the callback should be unregistered."""
@@ -356,7 +352,7 @@ def _lock_property(self, **properties):
356
352
The value should be the JSON state of the property.
357
353
358
354
NOTE: This, in addition to the single lock for all state changes, is
359
- flawed. In the future we may want to look into buffering state changes
355
+ flawed. In the future we may want to look into buffering state changes
360
356
back to the front-end."""
361
357
self ._property_lock = properties
362
358
try :
@@ -373,7 +369,7 @@ def _allow_write(self):
373
369
self ._read_only_enabled = False
374
370
yield
375
371
finally :
376
- self ._read_only_enabled = True
372
+ self ._read_only_enabled = True
377
373
378
374
@contextmanager
379
375
def hold_sync (self ):
@@ -385,7 +381,7 @@ def hold_sync(self):
385
381
self ._holding_sync = True
386
382
yield
387
383
finally :
388
- self ._holding_sync = False
384
+ self ._holding_sync = False
389
385
self .send_state (self ._states_to_send )
390
386
self ._states_to_send .clear ()
391
387
@@ -400,7 +396,7 @@ def _should_send_property(self, key, value):
400
396
return False
401
397
else :
402
398
return True
403
-
399
+
404
400
# Event handlers
405
401
@_show_traceback
406
402
def _handle_msg (self , msg ):
@@ -479,7 +475,7 @@ class DOMWidget(Widget):
479
475
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 )
480
476
_css = Tuple (sync = True , help = "CSS property list: (selector, key, value)" )
481
477
_dom_classes = Tuple (sync = True , help = "DOM classes applied to widget.$el." )
482
-
478
+
483
479
width = CUnicode (sync = True )
484
480
height = CUnicode (sync = True )
485
481
# A default padding of 2.5 px makes the widgets look nice when displayed inline.
@@ -493,33 +489,33 @@ class DOMWidget(Widget):
493
489
border_width = CUnicode (sync = True )
494
490
border_radius = CUnicode (sync = True )
495
491
border_style = CaselessStrEnum (values = [ # http://www.w3schools.com/cssref/pr_border-style.asp
496
- 'none' ,
497
- 'hidden' ,
498
- 'dotted' ,
499
- 'dashed' ,
500
- 'solid' ,
501
- 'double' ,
502
- 'groove' ,
503
- 'ridge' ,
504
- 'inset' ,
505
- 'outset' ,
506
- 'initial' ,
492
+ 'none' ,
493
+ 'hidden' ,
494
+ 'dotted' ,
495
+ 'dashed' ,
496
+ 'solid' ,
497
+ 'double' ,
498
+ 'groove' ,
499
+ 'ridge' ,
500
+ 'inset' ,
501
+ 'outset' ,
502
+ 'initial' ,
507
503
'inherit' , '' ],
508
504
default_value = '' , sync = True )
509
505
510
506
font_style = CaselessStrEnum (values = [ # http://www.w3schools.com/cssref/pr_font_font-style.asp
511
- 'normal' ,
512
- 'italic' ,
513
- 'oblique' ,
514
- 'initial' ,
515
- 'inherit' , '' ],
507
+ 'normal' ,
508
+ 'italic' ,
509
+ 'oblique' ,
510
+ 'initial' ,
511
+ 'inherit' , '' ],
516
512
default_value = '' , sync = True )
517
513
font_weight = CaselessStrEnum (values = [ # http://www.w3schools.com/cssref/pr_font_weight.asp
518
- 'normal' ,
519
- 'bold' ,
520
- 'bolder' ,
514
+ 'normal' ,
515
+ 'bold' ,
516
+ 'bolder' ,
521
517
'lighter' ,
522
- 'initial' ,
518
+ 'initial' ,
523
519
'inherit' , '' ] + list (map (str , range (100 ,1000 ,100 ))),
524
520
default_value = '' , sync = True )
525
521
font_size = CUnicode (sync = True )
0 commit comments