Enabling ipywidgets in custom kernel

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:

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:

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:

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

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 for a slightly more ergonomic way of watching the kernel messages, though the modern browser WebSocket stream watchers have gotten quite good. 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 which you might be able to bring in and expand upon.

1 Like

Thanks for the tips.
I saw comm package but unfortunately there is no example of incorporating this code into the kernel.