Using Jupyter Widgets on another kernels

Hello, I created a widget following this: Jupyter Widgets — Jupyter Widgets 8.0.2 documentation and it works very well with Python kernel. But what I want to do is using a custom kernel to render the widget, how can I do that?

The kernel needs to support Jupyter comms, which are indeed not as widely implemented. Some kernels didn’t like that part of the spec enough that they implemented their own version. Bummer.

At any rate, that functionality of ipykernel has been extracted out into the comm package, so may contain enough information to implement comm on your custom kernel. You’d still need the host python packages around, and installed next to the client application, but you’d be able to initiate them by following the messaging spec, plus how the reference implementation works. jupyterlab-kernelspy is very handy for watching what ipykernel does.

Once comms work, then your language probably needs some kind of observable pattern. The jupyter stack, for the most part, uses traitlets, but most languages have something akin to that, so you shouldn’t have to write that part yourself.

Finally, there’s the actual widget data model. This is kind of a doozy, as it’s actually a nested graph. For that, seeing the ipywidgets source is going to be critical.

Another approach altogether is to use some out-of-band method between the two kernels: e.g. a database, REST API, websockets, shared memory, or some other way to do message passing. The UX wouldn’t be great, of course.

2 Likes

Hmm, that is going to help a lot for sure, thanks a lot for that. May you just also show me the way for the last step? How can I override the view, I didn’t understand it totally.

Welp, you’ve given us zero info about what’s on the other side of the pipe. But presumably it can write to a file.

In the python side:

  • fire up something like watchdog, watchgod or watchfiles
  • watch a, e.g. to-python.json
    • when the file changes, have it parse the file and update the widget
    • when the widget changes, update to-mystery-language.json
  • on the mystery language, have a similar thing that writes and reads a file, at worst with polling

I think it is my fault. Firstly, thanks for helping.
Basically, in my kernel, I want to render a Widget no matter what is the response. So, lets say I type something, and when I execute, it shows me that widget with the response as a prop. Thats all I want to do, if I can also override notebook output, that also fits in my case.

I’m kinda lost by your description.

If your kernel is not python, and based on ipykernel or xeus-python, then ipywidgets probably won’t work.

The solution i outlined would be, in e.g. jupyterlab, to have two .ipynb files open, one using one of those kernels (with ipywidgets installed), and one using your custom kernel. Your custom kernel’s notebook would not have buttons. The two kernels would talk to each other out of band, such as by the JSON file mechanism described.

Otherwise, you’d need to implement an ipywidgets-compatible backend for whichever client widgets you wanted to drive, nominally widgetsnbextension or jupyterlab_widgets.

Oh, thank you. You helped a lot for my problem, have a great day!