Skip to content

Commit

Permalink
Backport PR jupyter-widgets#3811: Replace ipykernel dependency by the…
Browse files Browse the repository at this point in the history
… comm dependency
  • Loading branch information
SylvainCorlay authored and martinRenou committed Jul 31, 2023
1 parent 5acbc62 commit b02d083
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
6 changes: 3 additions & 3 deletions dev-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ echo -n "widgetsnbextension"
cd widgetsnbextension
pip install -v -e .
if [[ "$OSTYPE" == "msys" ]]; then
jupyter nbextension install --overwrite --py $nbExtFlags widgetsnbextension
jupyter nbextension install --overwrite --py $nbExtFlags widgetsnbextension || true
else
jupyter nbextension install --overwrite --py --symlink $nbExtFlags widgetsnbextension
jupyter nbextension install --overwrite --py --symlink $nbExtFlags widgetsnbextension || true
fi
jupyter nbextension enable --py $nbExtFlags widgetsnbextension
jupyter nbextension enable --py $nbExtFlags widgetsnbextension || true
cd ..

if test "$skip_jupyter_lab" != yes; then
Expand Down
12 changes: 3 additions & 9 deletions ipywidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@
"""

import os
import sys

from traitlets import link, dlink
from IPython import get_ipython

try:
from comm import get_comm_manager
except ImportError:
def get_comm_manager():
ip = get_ipython()

if ip is not None and getattr(ip, "kernel", None) is not None:
return get_ipython().kernel.comm_manager

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

def register_comm_target(kernel=None):
"""Register the jupyter.widget comm target"""
comm_manager = get_comm_manager()
from . import comm
comm_manager = comm.get_comm_manager()
if comm_manager is None:
return
comm_manager.register_target('jupyter.widget', Widget.handle_comm_opened)
Expand Down
33 changes: 33 additions & 0 deletions ipywidgets/comm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# compatibility shim for ipykernel < 6.18
import sys
from IPython import get_ipython
import comm


def requires_ipykernel_shim():
if "ipykernel" in sys.modules:
import ipykernel

version = ipykernel.version_info
return version < (6, 18)
else:
return False


def get_comm_manager():
if requires_ipykernel_shim():
ip = get_ipython()

if ip is not None and getattr(ip, "kernel", None) is not None:
return get_ipython().kernel.comm_manager
else:
return comm.get_comm_manager()


def create_comm(*args, **kwargs):
if requires_ipykernel_shim():
from ipykernel.comm import Comm

return Comm(*args, **kwargs)
else:
return comm.create_comm(*args, **kwargs)
3 changes: 0 additions & 3 deletions ipywidgets/tests/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

import traitlets

# This has a byproduct of setting up the comms
import ipykernel.ipkernel

from ..widgets import IntSlider, IntText, Text, Widget, jslink, HBox, widget_serialization, widget
from ..embed import embed_data, embed_snippet, embed_minimal_html, dependency_state

Expand Down
11 changes: 2 additions & 9 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ipython_genutils.py3compat import string_types, PY3
from IPython.display import display
from json import loads as jsonloads, dumps as jsondumps
from .. import comm

from base64 import standard_b64encode

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

try:
from comm import create_comm
except ImportError:
def create_comm(**kwargs):
from ipykernel.comm import Comm

return Comm(**kwargs)

self.comm = create_comm(**args)
self.comm = comm.create_comm(**args)

@observe('comm')
def _comm_changed(self, change):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

setuptools_args = {}
install_requires = setuptools_args['install_requires'] = [
'ipykernel>=4.5.1',
'comm>=0.1.3',
'ipython_genutils~=0.2.0',
'traitlets>=4.3.1',
# TODO: Dynamically add this dependency
Expand Down

0 comments on commit b02d083

Please sign in to comment.