Skip to content

Commit 2ef7e16

Browse files
add shim module
1 parent dd250bf commit 2ef7e16

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

python/ipywidgets/ipywidgets/__init__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@
2626
from traitlets import link, dlink
2727
from IPython import get_ipython
2828

29-
# ipykernel <6.18 when the comm package did not exist
30-
if "ipykernel" in sys.modules and "comm" not in sys.modules:
31-
def get_comm_manager():
32-
ip = get_ipython()
33-
34-
if ip is not None and getattr(ip, "kernel", None) is not None:
35-
return get_ipython().kernel.comm_manager
36-
# Using the comm package
37-
else:
38-
from comm import get_comm_manager
39-
4029
from .widgets import *
4130

4231

@@ -48,7 +37,8 @@ def load_ipython_extension(ip):
4837

4938
def register_comm_target(kernel=None):
5039
"""Register the jupyter.widget comm target"""
51-
comm_manager = get_comm_manager()
40+
from . import comm
41+
comm_manager = comm.get_comm_manager()
5242
if comm_manager is None:
5343
return
5444
comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)

python/ipywidgets/ipywidgets/comm.py

Lines changed: 33 additions & 0 deletions
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)

python/ipywidgets/ipywidgets/widgets/widget.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Any, HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
1616
Undefined)
1717
from json import loads as jsonloads, dumps as jsondumps
18+
from .. import comm
1819

1920
from base64 import standard_b64encode
2021

@@ -525,16 +526,7 @@ def open(self):
525526
if self._model_id is not None:
526527
args['comm_id'] = self._model_id
527528

528-
# ipykernel <6.18 when the comm package did not exist
529-
if "ipykernel" in sys.modules and "comm" not in sys.modules:
530-
def create_comm(**kwargs):
531-
from ipykernel.comm import Comm
532-
533-
return Comm(**kwargs)
534-
else:
535-
from comm import create_comm
536-
537-
self.comm = create_comm(**args)
529+
self.comm = comm.create_comm(**args)
538530

539531
@observe('comm')
540532
def _comm_changed(self, change):

0 commit comments

Comments
 (0)