@@ -291,6 +291,7 @@ class Widget(LoggingHasTraits):
291
291
# Class attributes
292
292
#-------------------------------------------------------------------------
293
293
_widget_construction_callback = None
294
+ _control_comm = None
294
295
295
296
# widgets is a dictionary of all active widget objects
296
297
widgets = {}
@@ -303,7 +304,6 @@ def close_all(cls):
303
304
for widget in list (cls .widgets .values ()):
304
305
widget .close ()
305
306
306
-
307
307
@staticmethod
308
308
def on_widget_constructed (callback ):
309
309
"""Registers a callback to be called when a widget is constructed.
@@ -319,25 +319,37 @@ def _call_widget_constructed(widget):
319
319
Widget ._widget_construction_callback (widget )
320
320
321
321
@classmethod
322
- def handle_comm_opened_control (cls , comm , msg ):
322
+ def handle_control_comm_opened (cls , comm , msg ):
323
323
version = msg .get ('metadata' , {}).get ('version' , '' )
324
324
if version .split ('.' )[0 ] != CONTROL_PROTOCOL_VERSION_MAJOR :
325
325
raise ValueError ("Incompatible widget control protocol versions: received version %r, expected version %r" % (version , __control_protocol_version__ ))
326
326
327
- cls .get_manager_state ()
328
- widgets = Widget .widgets .values ()
329
- # build a single dict with the full widget state
330
- full_state = {}
331
- drop_defaults = False
332
- for widget in widgets :
333
- full_state [widget .model_id ] = {
334
- 'model_name' : widget ._model_name ,
335
- 'model_module' : widget ._model_module ,
336
- 'model_module_version' : widget ._model_module_version ,
337
- 'state' : widget .get_state (drop_defaults = drop_defaults ),
338
- }
339
- full_state , buffer_paths , buffers = _remove_buffers (full_state )
340
- comm .send ([full_state , buffer_paths ], buffers = buffers )
327
+ cls ._control_comm = comm
328
+ cls ._control_comm .on_msg (cls .handle_control_comm_msg )
329
+
330
+ @classmethod
331
+ def handle_control_comm_msg (cls , msg ):
332
+ # This shouldn't happen unless someone calls this method manually
333
+ if cls ._control_comm is None :
334
+ raise RuntimeError ('Control comm has not been properly opened' )
335
+
336
+ if msg ['content' ]['data' ]['type' ] == 'models-request' :
337
+ # Send back the full widgets state
338
+ cls .get_manager_state ()
339
+ widgets = Widget .widgets .values ()
340
+ full_state = {}
341
+ drop_defaults = False
342
+ for widget in widgets :
343
+ full_state [widget .model_id ] = {
344
+ 'model_name' : widget ._model_name ,
345
+ 'model_module' : widget ._model_module ,
346
+ 'model_module_version' : widget ._model_module_version ,
347
+ 'state' : widget .get_state (drop_defaults = drop_defaults ),
348
+ }
349
+ full_state , buffer_paths , buffers = _remove_buffers (full_state )
350
+ cls ._control_comm .send ([full_state , buffer_paths ], buffers = buffers )
351
+ else :
352
+ raise RuntimeError ('Control comm msg "{}" not implemented' .format (msg ['content' ]['data' ]['type' ]))
341
353
342
354
@staticmethod
343
355
def handle_comm_opened (comm , msg ):
0 commit comments