I created a lab extension using the
mimerender-cookiecutter, but cannot figure out how to create a Comm object to send custom messages between the frontend and a notebook kernel. When I try to open a comm instance in the kernel-side code, the web console displays an error message that my comm object is "not found in registry".
1. Based on the
Comms Doc Page, I am calling a "register_target" method in the load_ipython_extension() function in nb_extension.js:
export function load_ipython_extension() {
console.log('load_ipython_extension');
define(
['nbextensions/jupyterlab_foo/index', 'base/js/namespace'],
(Extension, Jupyter) => {
const { notebook } = Jupyter;
Extension.register_renderer(notebook);
Extension.render_cells(notebook);
// New code:
Jupyter.notebook.kernel.comm_manager.register_target('my_comm_target', function(comm, msg) {
console.log('registering comm target');
comm.on_msg(function(msg){console.log('on_msg()')});
comm.on_close(function(msg){console.log('on_close()')});
//comm.send({'foo': 0});
});
}
);
}
Is this the right place to register comm objects? It's the only js code that has access to the Jupyter object, as far as I can tell.
Side note: the rest of the extension works, even though I don't see either of the console.log messages I added to my load_ipython_extension() call. Is this code being executed outside of the browser somehow?
2. In my kernel (python) code, I am initializing a Comm object per that same Comms doc:
from ipykernel.comm import Comm
my_comm = Comm(target_name='my_comm_target', data={'foo': 1})
And when the Comm object is initialized, an error message "Object not found in registry" is written to the javascript console.
Can anyone comment on whether this code should or should not work? Is there any way to get visibility into the load_ipython_extension function? I would sure appreciate whatever help anyone can provide, as I am completely stumped.