How to register comm target in Jupyter lab?

153 Views Asked by At

Following Jupyter notebook example on Opening a comm from the kernel, I was able to initiate communication between the kernel and the notebook.

Every time I invoke fetchData -> It initiates a new connection, sends fetchParams and once it's ready it receives the data and activates a callback.

The code:

js

function fetchData(fetchParams, callback) {
    const comm =
    Jupyter.notebook.kernel.comm_manager.new_comm('comm_target', {{}})

    comm.send(fetchParams)

    comm.on_msg(function(msg) {
        const data = JSON.parse(msg.content.data['data']);
        if (callback) {
            callback(data)
        }
    });
}

python

get_ipython().kernel.comm_manager.register_target("comm_target", front_listener_get_data)

It's working fine on Jupyter notebook but when I'm running it on Jupyter lab - I get this error

caught ReferenceError: Jupyter is not defined

I found this solution, but it requires me to use import { Kernel } from "@jupyterlab/services"; and nodejs, which I prefer not to do since it will break the current architecture of the entire project. This is a small feature within a bigger project.

any suggestions?

0

There are 0 best solutions below