Skip to content

Commit 0f36487

Browse files
committed
Move comm package
1 parent ad14ad1 commit 0f36487

File tree

6 files changed

+23
-41
lines changed

6 files changed

+23
-41
lines changed

python/ipywidgets/ipywidgets/__init__.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,26 @@
2121
from ._version import __version__, __protocol_version__, __jupyter_widgets_controls_version__, __jupyter_widgets_base_version__
2222

2323
import os
24-
from IPython import get_ipython
25-
from .widgets import *
24+
2625
from traitlets import link, dlink
26+
from comm import get_comm_manager
27+
28+
from .widgets import *
29+
2730

2831
def load_ipython_extension(ip):
2932
"""Set up Jupyter to work with widgets"""
30-
if not hasattr(ip, 'kernel'):
31-
return
32-
register_comm_target(ip.kernel)
33+
register_comm_target()
3334

34-
def register_comm_target(kernel=None):
35+
def register_comm_target():
3536
"""Register the jupyter.widget comm target"""
36-
if kernel is None:
37-
kernel = get_ipython().kernel
38-
kernel.comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)
39-
kernel.comm_manager.register_target('jupyter.widget.control', Widget.handle_control_comm_opened)
37+
comm_manager = get_comm_manager()
38+
39+
comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)
40+
comm_manager.register_target('jupyter.widget.control', Widget.handle_control_comm_opened)
4041

4142
def _handle_ipython():
4243
"""Register with the comm target at import if running in Jupyter"""
43-
ip = get_ipython()
44-
if ip is None:
45-
return
46-
load_ipython_extension(ip)
44+
register_comm_target()
4745

4846
_handle_ipython()

python/ipywidgets/ipywidgets/widgets/tests/test_widget_templates.py

-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ def test_update_dynamically(self, send_state): #pylint: disable=no-self-use
220220
assert box.layout.grid_template_areas == ('"top-left top-right"\n' +
221221
'"top-left bottom-right"')
222222

223-
box.layout.comm.kernel = mock.MagicMock(spec=Kernel) #for mocking purposes
224223
send_state.reset_mock()
225224
box.bottom_left = button2
226225

@@ -235,7 +234,6 @@ def test_update_dynamically(self, send_state): #pylint: disable=no-self-use
235234
bottom_left=None, bottom_right=button4)
236235
assert box.layout.grid_template_areas == ('"top-left top-right"\n' +
237236
'"top-left bottom-right"')
238-
box.layout.comm.kernel = mock.MagicMock(spec=Kernel) #for mocking purposes
239237
send_state.reset_mock()
240238
box.merge = False
241239
assert box.layout.grid_template_areas == ('"top-left top-right"\n' +

python/ipywidgets/ipywidgets/widgets/tests/test_widget_upload.py

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ def test_serialization_deserialization_integrity(self):
7676
from ipykernel.comm import Comm
7777
uploader = FileUpload()
7878
mock_comm = MagicMock(spec=Comm)
79-
mock_comm.kernel = 'does not matter'
8079
mock_comm.send = MagicMock()
8180
uploader.comm = mock_comm
8281
message = {'value': [FILE_UPLOAD_FRONTEND_CONTENT]}

python/ipywidgets/ipywidgets/widgets/tests/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from ipykernel.comm import Comm
54
from ipywidgets import Widget
65

7-
class DummyComm(Comm):
6+
class DummyComm():
87
comm_id = 'a-b-c-d'
98
kernel = 'Truthy'
109

python/ipywidgets/ipywidgets/widgets/widget.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from contextlib import contextmanager
1010
from collections.abc import Iterable
1111
from IPython import get_ipython
12-
from ipykernel.comm import Comm
12+
from comm import create_comm
1313
from traitlets import (
14-
HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
14+
Any, HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
1515
Undefined)
1616
from json import loads as jsonloads, dumps as jsondumps
1717

@@ -421,7 +421,7 @@ def get_view_spec(self):
421421

422422
_view_count = Int(None, allow_none=True,
423423
help="EXPERIMENTAL: The number of views of the model displayed in the frontend. This attribute is experimental and may change or be removed in the future. None signifies that views will not be tracked. Set this to 0 to start tracking view creation/deletion.").tag(sync=True)
424-
comm = Instance('ipykernel.comm.Comm', allow_none=True)
424+
comm = Any(None, allow_none=True)
425425

426426
keys = List(help="The traits which are synced.")
427427

@@ -466,7 +466,7 @@ def open(self):
466466
if self._model_id is not None:
467467
args['comm_id'] = self._model_id
468468

469-
self.comm = Comm(**args)
469+
self.comm = create_comm(**args)
470470

471471
@observe('comm')
472472
def _comm_changed(self, change):
@@ -627,11 +627,10 @@ def notify_change(self, change):
627627
# Send the state to the frontend before the user-registered callbacks
628628
# are called.
629629
name = change['name']
630-
if self.comm is not None and self.comm.kernel is not None:
631-
# Make sure this isn't information that the front-end just sent us.
632-
if name in self.keys and self._should_send_property(name, getattr(self, name)):
633-
# Send new state to front-end
634-
self.send_state(key=name)
630+
# Make sure this isn't information that the front-end just sent us.
631+
if name in self.keys and self._should_send_property(name, getattr(self, name)):
632+
# Send new state to front-end
633+
self.send_state(key=name)
635634
super().notify_change(change)
636635

637636
def __repr__(self):
@@ -755,7 +754,7 @@ def _repr_mimebundle_(self, **kwargs):
755754

756755
def _send(self, msg, buffers=None):
757756
"""Sends a message to the model in the front-end."""
758-
if self.comm is not None and self.comm.kernel is not None:
757+
if self.comm is not None:
759758
self.comm.send(data=msg, buffers=buffers)
760759

761760
def _repr_keys(self):

python/ipywidgets/ipywidgets/widgets/widget_output.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ def __enter__(self):
111111
kernel = None
112112
if ip and getattr(ip, "kernel", None) is not None:
113113
kernel = ip.kernel
114-
elif self.comm is not None and self.comm.kernel is not None:
115-
kernel = self.comm.kernel
116-
114+
117115
if kernel:
118116
parent = None
119117
if hasattr(kernel, "get_parent"):
@@ -134,15 +132,6 @@ def __exit__(self, etype, evalue, tb):
134132
if ip:
135133
kernel = ip
136134
ip.showtraceback((etype, evalue, tb), tb_offset=0)
137-
elif self.comm is not None and self.comm.kernel is not None:
138-
kernel = self.comm.kernel
139-
kernel.send_response(kernel.iopub_socket,
140-
u'error',
141-
{
142-
u'traceback': ["".join(traceback.format_exception(etype, evalue, tb))],
143-
u'evalue': repr(evalue.args),
144-
u'ename': etype.__name__
145-
})
146135
self._flush()
147136
self.__counter -= 1
148137
if self.__counter == 0:

0 commit comments

Comments
 (0)