# Enabling ipywidgets in custom kernel

**URL:** <https://discourse.jupyter.org/t/enabling-ipywidgets-in-custom-kernel/19537>\
**Category:** Kernels\
**Created:** [May 23, 2023, 10:48am UTC](https://discourse.jupyter.org/t/enabling-ipywidgets-in-custom-kernel/19537 "2023-05-23T10:48:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan7123](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/stan7123/32/9966_2.png) [@stan7123](https://discourse.jupyter.org/u/stan7123)\
**Post date:** [May 23, 2023, 10:48am UTC](https://discourse.jupyter.org/t/enabling-ipywidgets-in-custom-kernel/19537/1 "2023-05-23T10:48:49Z")

</div>

Hello,  
I have a custom Python kernel which is (in short) executing python on remote machine.  
I’m trying to make ipywidgets work but I get no control rendered in notebook (for instance FileUpload), and also there are errors in the browser console:

```auto
Failed to fetch ipywidgets through the "jupyter.widget.control" comm channel, fallback to fetching individual model state. Reason: Control comm was closed too early

Error: widget model not found

```

I feel that there’s some problem with comms.  
Does my kernel have all the code to make comms work?

Kernel code:

```auto
from ipykernel.kernelbase import Kernel

class CustomKernel(Kernel):
    implementation = 'CustomKernel'
    implementation_version = '0.001'
    language = 'python'
    language_version = '3'
    language_info = {
        'name': 'python',
        'mimetype': 'text/x-python',
        'file_extension': '.py',
    }

    async def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
        ...

    async def do_shutdown(self, restart):
        ...

```

Packages:

```auto
jupyterlab==3.6.3
jupyterlab-widgets==3.0.7
ipywidgets==8.0.6

```

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [May 23, 2023, 11:43am UTC](https://discourse.jupyter.org/t/enabling-ipywidgets-in-custom-kernel/19537/2 "2023-05-23T11:43:54Z")

</div>

If possible, basing a custom kernel off a higher-level kernel (e.g. `IPyKernel` rather than `KernelBase`), and replacing/monkeypatching the parts needed. Comm in specific is rather messy.

Otherwise, if set on doing everything from scratch, it comes down to watching a (or preferrably multiple) working kernel implementations at the network level.

I can recommend [kernelspy](https://github.com/jupyterlab-contrib/jupyterlab-kernelspy) for a slightly more ergonomic way of watching the kernel messages, though the modern browser WebSocket stream watchers have gotten quite good. [Wireshark](https://github.com/wireshark/wireshark) is also quite excellent, and has fairly good support for JSON payloads.

Much of the `ipykernel` code around the system underlying widgets, is now available as a separate package, [comm](https://pypi.org/project/comm/) which you might be able to bring in and expand upon.

---

<div class="post-metadata">

**Author:** ![stan7123](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/stan7123/32/9966_2.png) [@stan7123](https://discourse.jupyter.org/u/stan7123)\
**Post date:** [May 23, 2023, 12:11pm UTC](https://discourse.jupyter.org/t/enabling-ipywidgets-in-custom-kernel/19537/3 "2023-05-23T12:11:17Z")

</div>

Thanks for the tips.  
I saw [comm](https://github.com/ipython/comm) package but unfortunately there is no example of incorporating this code into the kernel.
