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 1 commit
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
14 changes: 2 additions & 12 deletions python/ipywidgets/ipywidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@
from traitlets import link, dlink
from IPython import get_ipython

# ipykernel <6.18 when the comm package did not exist
if "ipykernel" in sys.modules and "comm" not in sys.modules:
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
# Using the comm package
else:
from comm import get_comm_manager

from .widgets import *


Expand All @@ -48,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)
12 changes: 2 additions & 10 deletions python/ipywidgets/ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,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 @@ -525,16 +526,7 @@ def open(self):
if self._model_id is not None:
args['comm_id'] = self._model_id

# ipykernel <6.18 when the comm package did not exist
if "ipykernel" in sys.modules and "comm" not in sys.modules:
def create_comm(**kwargs):
from ipykernel.comm import Comm

return Comm(**kwargs)
else:
from comm import create_comm

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

@observe('comm')
def _comm_changed(self, change):
Expand Down