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

Conversation

martinRenou
Copy link
Member

@martinRenou martinRenou commented Jul 26, 2023

As discussed in #3749

This will:

  • Use the comm implementation from ipykernel if it's available
  • Otherwise import the comm implementation from the new comm package (typically the case of xeus-python, or in a test runtime where there is no kernel)

@github-actions
Copy link

Binder 👈 Launch a binder notebook on branch martinRenou/ipywidgets/add_comm_dependency

@martinRenou martinRenou marked this pull request as draft July 26, 2023 14:09
@martinRenou
Copy link
Member Author

CI failure is due to the Notebook 7 release

@martinRenou martinRenou marked this pull request as ready for review July 26, 2023 14:38
def create_comm(**kwargs):
from ipykernel.comm import Comm

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

Choose a reason for hiding this comment

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

👍

@maartenbreddels
Copy link
Member

I'll test this today!

@maartenbreddels
Copy link
Member

@martinRenou this was more difficult to get right, the comm module could be imported, and ipykernel count not (yet) be imported (as is the case for solara).

Also, by putting the same API in a shim module, I think it cleaned up the code a bit. What do you think?

Comment on lines +29 to +31
from ipykernel.comm import Comm

return Comm(*args, **kwargs)
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 :)

@martinRenou
Copy link
Member Author

Yes putting it all in a separate file makes sense. Thanks for looking into it!

I need to check that this works nicely with xeus-python.

Copy link
Member Author

@martinRenou martinRenou left a comment

Choose a reason for hiding this comment

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

Tested with xeus-python, it works nicely! Thanks @maartenbreddels

@SylvainCorlay SylvainCorlay merged commit b330020 into jupyter-widgets:main Jul 28, 2023
19 checks passed
@SylvainCorlay
Copy link
Member

🎉

@martinRenou martinRenou deleted the add_comm_dependency branch July 28, 2023 08:37
@martinRenou
Copy link
Member Author

meeseeksdev please backport to 7.x

@lumberbot-app
Copy link

lumberbot-app bot commented Jul 28, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 7.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 b3300207f35571e253c355175ef989c6d5be418d
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #3811: Replace ipykernel dependency by the comm dependency'
  1. Push to a named branch:
git push YOURFORK 7.x:auto-backport-of-pr-3811-on-7.x
  1. Create a PR against branch 7.x, I would have named this PR:

"Backport PR #3811 on branch 7.x (Replace ipykernel dependency by the comm dependency)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

martinRenou pushed a commit to martinRenou/ipywidgets that referenced this pull request Jul 31, 2023
martinRenou added a commit that referenced this pull request Jul 31, 2023
Backport PR #3811: Replace ipykernel dependency by the comm dependency
garrison added a commit to garrison/jupyter-sphinx that referenced this pull request Aug 1, 2023
ipywidgets 8.1.0 has been released, which includes the change at
jupyter-widgets/ipywidgets#3811.
With this change, ipykernel is no longer a dependnecy of ipywidgets,
so jupyer-sphinx must now depend on ipykernel directly rather
than assume it will be installed as a transitive dependency of
ipywidgets.  Without this change, jupyter-sphinx tests will fail CI.
This has additionally led to CI failures in some packages that
depend on jupyter-sphinx but not ipykernel.
@vincent-grosbois
Copy link

hello, with the backport of this to 7.X, it means now that ipywidgets 7.X is not working anymore on python 3.6 (as the comm module will require traitlet >= 5, which require python >= 3.7). Is it intended that ipywidgets 7.X won't support python 3.6 ? thanks

@martinRenou
Copy link
Member Author

Thanks for commenting.

We can probably loosen the Traitlets dependency in the comm package in a patch release. Would you like to open a PR for this?

@vincent-grosbois
Copy link

I don't think I'll have the time soon, but here's another proposal: ipython/comm#15

@bollwyvl
Copy link
Contributor

bollwyvl commented Aug 3, 2023

This broke import ipywidgets in jupyterlite-pyodide-kernel as can be seen in e.g.

https://ipywidgets.readthedocs.io/en/stable/try/lab/index.html?path=Widget%20List.ipynb

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 import ipywidgets as widgets

File /lib/python3.10/site-packages/ipywidgets/__init__.py:54
     51         return
     52     register_comm_target()
---> 54 _handle_ipython()

File /lib/python3.10/site-packages/ipywidgets/__init__.py:52, in _handle_ipython()
     50 if ip is None:
     51     return
---> 52 register_comm_target()

File /lib/python3.10/site-packages/ipywidgets/__init__.py:41, in register_comm_target(kernel)
     39 """Register the jupyter.widget comm target"""
     40 from . import comm
---> 41 comm_manager = comm.get_comm_manager()
     42 if comm_manager is None:
     43     return

File /lib/python3.10/site-packages/ipywidgets/comm.py:18, in get_comm_manager()
     17 def get_comm_manager():
---> 18     if requires_ipykernel_shim():
     19         ip = get_ipython()
     21         if ip is not None and getattr(ip, "kernel", None) is not None:

File /lib/python3.10/site-packages/ipywidgets/comm.py:11, in requires_ipykernel_shim()
      8 if "ipykernel" in sys.modules:
      9     import ipykernel
---> 11     version = ipykernel.version_info
     12     return version < (6, 18)
     13 else:

AttributeError: module 'ipykernel' has no attribute 'version_info'

Some ways forward:

  • add version_info to the shim
    • last i checked we couldn't use comm directly for... some reason, and it has its own comm implementation
    • but less duplication is good
  • add some other attribute to check
  • add a special case for 'pyodide' in sys.modules (wouldn't recommend)

As this isn't a part I've monkied with much, would appreciate some insight from those that have, but would happily do the PR.

Also, worrying about python 3.6, and frankly, 3.7, is getting a bit wearisome if it can lead to any problems for folks that are on currently supported pythons.

@martinRenou
Copy link
Member Author

We've been discussing this in #3819

add version_info to the shim

I think that' the way to go for now, longer term we get rid of the shim and use the comm module only.

last i checked we couldn't use comm directly for... some reason

I'm surprised, using the comm module to expose the pyolite's implementation of the comms should be almost straightforward.

Also, worrying about python 3.6, and frankly, 3.7

What do you mean about this? Related, it has been raised that comm could not be installed on Python 3.6 due to a strict traitlets dependency, it has been fixed in comm 0.1.4 by loosening the traitlets dependency.

@martinRenou
Copy link
Member Author

martinRenou commented Aug 3, 2023

I'll work on a PR to update the shims AND make use of the comm module right now.

Well actually jupyterlite/pyodide-kernel#53 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants