@@ -306,6 +306,10 @@ def __init__(self, nb, km=None, **kw):
306
306
'OutputModel' : OutputWidget
307
307
}
308
308
}
309
+ # comm_open_handlers should return an object with a .handle_msg(msg) method or None
310
+ self .comm_open_handlers = {
311
+ 'jupyter.widget' : self .on_comm_open_jupyter_widget
312
+ }
309
313
310
314
def reset_execution_trackers (self ):
311
315
"""Resets any per-execution trackers.
@@ -318,7 +322,7 @@ def reset_execution_trackers(self):
318
322
# to support nested use of output widgets.
319
323
self .output_hook_stack = collections .defaultdict (list )
320
324
# our front-end mimicing Output widgets
321
- self .widget_objects = {}
325
+ self .comm_objects = {}
322
326
323
327
def start_kernel_manager (self ):
324
328
"""Creates a new kernel manager.
@@ -863,23 +867,17 @@ def handle_comm_msg(self, outs, msg, cell_index):
863
867
self .widget_buffers [content ['comm_id' ]] = self ._get_buffer_data (msg )
864
868
# There are cases where we need to mimic a frontend, to get similar behaviour as
865
869
# when using the Output widget from Jupyter lab/notebook
866
- if msg ['msg_type' ] == 'comm_open' and msg ['content' ].get ('target_name' ) == 'jupyter.widget' :
867
- content = msg ['content' ]
868
- data = content ['data' ]
869
- state = data ['state' ]
870
+ if msg ['msg_type' ] == 'comm_open' :
871
+ handler = self .comm_open_handlers .get (msg ['content' ].get ('target_name' ))
870
872
comm_id = msg ['content' ]['comm_id' ]
871
- module = self .widget_registry .get (state ['_model_module' ])
872
- if module :
873
- widget_class = module .get (state ['_model_name' ])
874
- self .widget_objects [comm_id ] = widget_class (comm_id , state , self .kc , self )
873
+ comm_object = handler (msg )
874
+ if comm_object :
875
+ self .comm_objects [comm_id ] = comm_object
875
876
elif msg ['msg_type' ] == 'comm_msg' :
876
877
content = msg ['content' ]
877
- data = content ['data' ]
878
- if 'state' in data :
879
- state = data ['state' ]
880
- comm_id = msg ['content' ]['comm_id' ]
881
- if comm_id in self .widget_objects :
882
- self .widget_objects [comm_id ].set_state (state )
878
+ comm_id = msg ['content' ]['comm_id' ]
879
+ if comm_id in self .comm_objects :
880
+ self .comm_objects [comm_id ].handle_msg (msg )
883
881
884
882
def _serialize_widget_state (self , state ):
885
883
"""Serialize a widget state, following format in @jupyter-widgets/schema."""
@@ -920,6 +918,17 @@ def remove_output_hook(self, msg_id, hook):
920
918
removed_hook = self .output_hook_stack [msg_id ].pop ()
921
919
assert removed_hook == hook
922
920
921
+ def on_comm_open_jupyter_widget (self , msg ):
922
+ content = msg ['content' ]
923
+ data = content ['data' ]
924
+ state = data ['state' ]
925
+ comm_id = msg ['content' ]['comm_id' ]
926
+ module = self .widget_registry .get (state ['_model_module' ])
927
+ if module :
928
+ widget_class = module .get (state ['_model_name' ])
929
+ if widget_class :
930
+ return widget_class (comm_id , state , self .kc , self )
931
+
923
932
924
933
def execute (nb , cwd = None , km = None , ** kwargs ):
925
934
"""Execute a notebook's code, updating outputs within the notebook object.
0 commit comments