Can I register a Comm target in a JupyterLab notebook extension?

627 views
Skip to first unread message

John T

unread,
May 27, 2018, 2:43:45 PM5/27/18
to Project Jupyter
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:
          // Register comm object per http://jupyter-notebook.readthedocs.io/en/stable/comms.html
          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.

sp...@draves.org

unread,
May 27, 2018, 3:33:04 PM5/27/18
to jup...@googlegroups.com
Yes a lab extension can create a comm channel to talk with the kernel.
Here's where BeakerX does it:
which calls another function that actually calls registerCommTarget():
It waits until the context is ready using Promise.all on context.ready.
Best, -Scott

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/daaa0d16-8a82-4f90-b866-bc6e37b14862%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

John T

unread,
May 27, 2018, 9:38:45 PM5/27/18
to Project Jupyter
(Google seems to only let me post a reply to my own message, but this is actually in response to Scott’s post below.)

Thanks very much. It is good to know that it can be done. It looks like JupyterLab comms are much more complicated and much less documented than plain Jupyter Notebook comm, so I hope I can figure out the logic.

John

unread,
May 27, 2018, 10:45:15 PM5/27/18
to Project Jupyter
I was about to ask the same question and I saw yours at the top of the list.

How can I get "Opening Comm from the kernel" documented here


to work in Jupyterlab.  I would like to modify the activate() routine in


to also open a Comm channel but the javascript code doesn't compile when added to typescript file.

How can I get this code to work in jupyterlab typescript.

Jupyter.notebook.kernel.comm_manager.register_target('my_comm_target',
    function(comm, msg) {
        // comm is the frontend comm instance
        // msg is the comm_open message, which can carry data

        // Register handlers for later messages:
        comm.on_msg(function(msg) {});
        comm.on_close(function(msg) {});
        comm.send({'foo': 0});
    });

when I run

npm run build

command I get a compile error. 

error  TS2304: cannot find name 'Jupyter'


John
Reply all
Reply to author
Forward
0 new messages