Skip to content

Commit 01cd1a8

Browse files
authored
Merge pull request #3814 from martinRenou/backport-of-pr-3811-on-7.x
Backport PR #3811: Replace ipykernel dependency by the comm dependency
2 parents 5acbc62 + da11933 commit 01cd1a8

File tree

7 files changed

+43
-26
lines changed

7 files changed

+43
-26
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Install node
1616
uses: actions/setup-node@v1
1717
with:
18-
node-version: '14.x'
18+
node-version: '16.x'
1919
- name: Install Python
2020
uses: actions/setup-python@v1
2121
with:

dev-install.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ echo -n "widgetsnbextension"
4141
cd widgetsnbextension
4242
pip install -v -e .
4343
if [[ "$OSTYPE" == "msys" ]]; then
44-
jupyter nbextension install --overwrite --py $nbExtFlags widgetsnbextension
44+
jupyter nbextension install --overwrite --py $nbExtFlags widgetsnbextension || true
4545
else
46-
jupyter nbextension install --overwrite --py --symlink $nbExtFlags widgetsnbextension
46+
jupyter nbextension install --overwrite --py --symlink $nbExtFlags widgetsnbextension || true
4747
fi
48-
jupyter nbextension enable --py $nbExtFlags widgetsnbextension
48+
jupyter nbextension enable --py $nbExtFlags widgetsnbextension || true
4949
cd ..
5050

5151
if test "$skip_jupyter_lab" != yes; then

ipywidgets/__init__.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,11 @@
1919
"""
2020

2121
import os
22+
import sys
2223

2324
from traitlets import link, dlink
2425
from IPython import get_ipython
2526

26-
try:
27-
from comm import get_comm_manager
28-
except ImportError:
29-
def get_comm_manager():
30-
ip = get_ipython()
31-
32-
if ip is not None and getattr(ip, "kernel", None) is not None:
33-
return get_ipython().kernel.comm_manager
3427

3528
from ._version import version_info, __version__, __protocol_version__, __jupyter_widgets_controls_version__, __jupyter_widgets_base_version__
3629
from .widgets import *
@@ -46,7 +39,8 @@ def load_ipython_extension(ip):
4639

4740
def register_comm_target(kernel=None):
4841
"""Register the jupyter.widget comm target"""
49-
comm_manager = get_comm_manager()
42+
from . import comm
43+
comm_manager = comm.get_comm_manager()
5044
if comm_manager is None:
5145
return
5246
comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)

ipywidgets/comm.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# compatibility shim for ipykernel < 6.18
2+
import sys
3+
from IPython import get_ipython
4+
import comm
5+
6+
7+
def requires_ipykernel_shim():
8+
if "ipykernel" in sys.modules:
9+
import ipykernel
10+
11+
version = ipykernel.version_info
12+
return version < (6, 18)
13+
else:
14+
return False
15+
16+
17+
def get_comm_manager():
18+
if requires_ipykernel_shim():
19+
ip = get_ipython()
20+
21+
if ip is not None and getattr(ip, "kernel", None) is not None:
22+
return get_ipython().kernel.comm_manager
23+
else:
24+
return comm.get_comm_manager()
25+
26+
27+
def create_comm(*args, **kwargs):
28+
if requires_ipykernel_shim():
29+
from ipykernel.comm import Comm
30+
31+
return Comm(*args, **kwargs)
32+
else:
33+
return comm.create_comm(*args, **kwargs)

ipywidgets/tests/test_embed.py

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
import traitlets
99

10-
# This has a byproduct of setting up the comms
11-
import ipykernel.ipkernel
12-
1310
from ..widgets import IntSlider, IntText, Text, Widget, jslink, HBox, widget_serialization, widget
1411
from ..embed import embed_data, embed_snippet, embed_minimal_html, dependency_state
1512

ipywidgets/widgets/widget.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ipython_genutils.py3compat import string_types, PY3
2424
from IPython.display import display
2525
from json import loads as jsonloads, dumps as jsondumps
26+
from .. import comm
2627

2728
from base64 import standard_b64encode
2829

@@ -500,15 +501,7 @@ def open(self):
500501
if self._model_id is not None:
501502
args['comm_id'] = self._model_id
502503

503-
try:
504-
from comm import create_comm
505-
except ImportError:
506-
def create_comm(**kwargs):
507-
from ipykernel.comm import Comm
508-
509-
return Comm(**kwargs)
510-
511-
self.comm = create_comm(**args)
504+
self.comm = comm.create_comm(**args)
512505

513506
@observe('comm')
514507
def _comm_changed(self, change):

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109

110110
setuptools_args = {}
111111
install_requires = setuptools_args['install_requires'] = [
112-
'ipykernel>=4.5.1',
112+
'comm>=0.1.3',
113113
'ipython_genutils~=0.2.0',
114114
'traitlets>=4.3.1',
115115
# TODO: Dynamically add this dependency

0 commit comments

Comments
 (0)