Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ipykernel dependency by the comm dependency #3811

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +29 to +31
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really like to get rid of this some day.

That way we can remove the nasty patches of ipykernel.comm in xeus-python.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you want to get rid of this? in xeus-python you don't need to fix this right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say a library imports ipykernel and we import it before ipywidgets, then we end up in this block, breaking xeus-python. So xeus-python needs to patch this import.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only for old ipykernels right? Do you think that will still happen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this piece of code to basically stay around forever, so we better get it right! :)
We can also skip this if we can detect xeus-python, or some other condition to make this more robust? Do you make use of a 'fake' ipykernel module in xeus-python?

Copy link
Member Author

@martinRenou martinRenou Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for old ipykernel yes.

I'd like this piece of code to basically stay around forever

Let's talk again about it in ten years? 😄

Do you make use of a 'fake' ipykernel module in xeus-python?

Yeah we mock the ipykernel module https://github.com/jupyter-xeus/xeus-python/blob/main/src/xinterpreter.cpp#L77 . We should keep this mock in xeus-python for supporting old ipywidgets anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to discuss this in ten years from now :)

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
Loading