Skip to content

Commit c8049a1

Browse files
refactor: replace hardcoding widget names by registry
1 parent 2d88880 commit c8049a1

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

nbclient/client.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ def __init__(self, nb, km=None, **kw):
301301
self.nb = nb
302302
self.km = km
303303
self.reset_execution_trackers()
304+
self.widget_registry = {
305+
'@jupyter-widgets/output': {
306+
'OutputModel': OutputWidget
307+
}
308+
}
304309

305310
def reset_execution_trackers(self):
306311
"""Resets any per-execution trackers.
@@ -313,7 +318,7 @@ def reset_execution_trackers(self):
313318
# to support nested use of output widgets.
314319
self.output_hook_stack = collections.defaultdict(list)
315320
# our front-end mimicing Output widgets
316-
self.output_widget_objects = {}
321+
self.widget_objects = {}
317322

318323
def start_kernel_manager(self):
319324
"""Creates a new kernel manager.
@@ -863,17 +868,18 @@ def handle_comm_msg(self, outs, msg, cell_index):
863868
data = content['data']
864869
state = data['state']
865870
comm_id = msg['content']['comm_id']
866-
if state['_model_module'] == '@jupyter-widgets/output' and\
867-
state['_model_name'] == 'OutputModel':
868-
self.output_widget_objects[comm_id] = OutputWidget(comm_id, state, self.kc, self)
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)
869875
elif msg['msg_type'] == 'comm_msg':
870876
content = msg['content']
871877
data = content['data']
872878
if 'state' in data:
873879
state = data['state']
874880
comm_id = msg['content']['comm_id']
875-
if comm_id in self.output_widget_objects:
876-
self.output_widget_objects[comm_id].set_state(state)
881+
if comm_id in self.widget_objects:
882+
self.widget_objects[comm_id].set_state(state)
877883

878884
def _serialize_widget_state(self, state):
879885
"""Serialize a widget state, following format in @jupyter-widgets/schema."""

0 commit comments

Comments
 (0)