Skip to content

Commit

Permalink
Merge pull request #3811 from martinRenou/add_comm_dependency
Browse files Browse the repository at this point in the history
Replace ipykernel dependency by the comm dependency
  • Loading branch information
SylvainCorlay authored Jul 28, 2023
2 parents 39d3c5d + 2ef7e16 commit b330020
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/devinstall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install notebook jupyterlab jupyter_packaging~=0.10
python -m pip install notebook jupyterlab notebook~=6.0 jupyter_packaging~=0.10
- name: Run the dev-install script
run: |
./dev-install.sh
Expand Down
6 changes: 3 additions & 3 deletions dev-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ jlpm build
echo -n "widgetsnbextension"
pip install -v -e ./python/widgetsnbextension
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

echo -n "ipywidgets"
pip install -v -e "./python/ipywidgets[test]"
Expand Down
8 changes: 6 additions & 2 deletions docs/source/examples/Widget Custom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"cell_type": "markdown",
"metadata": {
"tags": ["remove-cell"]
"tags": [
"remove-cell"
]
},
"source": [
"[Index](Index.ipynb) - [Back](Widget%20Styling.ipynb) - [Next](Widget%20Asynchronous.ipynb)"
Expand Down Expand Up @@ -828,7 +830,9 @@
{
"cell_type": "markdown",
"metadata": {
"tags": ["remove-cell"]
"tags": [
"remove-cell"
]
},
"source": [
"[Index](Index.ipynb) - [Back](Widget%20Styling.ipynb) - [Next](Widget%20Asynchronous.ipynb)"
Expand Down
12 changes: 3 additions & 9 deletions python/ipywidgets/ipywidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@
from ._version import __version__, __protocol_version__, __jupyter_widgets_controls_version__, __jupyter_widgets_base_version__

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 .widgets import *

Expand All @@ -44,7 +37,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 python/ipywidgets/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 python/ipywidgets/ipywidgets/tests/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,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 as widget_module
from ..embed import embed_data, embed_snippet, embed_minimal_html, dependency_state

Expand Down
12 changes: 3 additions & 9 deletions python/ipywidgets/ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
in the Jupyter notebook front-end.
"""
import os
import sys
import typing
from contextlib import contextmanager
from collections.abc import Iterable
Expand All @@ -14,6 +15,7 @@
Any, HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
Undefined)
from json import loads as jsonloads, dumps as jsondumps
from .. import comm

from base64 import standard_b64encode

Expand Down Expand Up @@ -524,15 +526,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 python/ipywidgets/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ zip_safe = False
packages = find:

install_requires =
ipykernel>=4.5.1
comm>=0.1.3
ipython>=6.1.0
traitlets>=4.3.1
widgetsnbextension~=4.0.7
Expand Down

0 comments on commit b330020

Please sign in to comment.