Yes a lab extension can create a comm channel to talk with the kernel. Here's where BeakerX does it: https://github.com/twosigma/beakerx/blob/a19c30748d5e6d95db9f7be094bb73db2a8ec386/js/lab/src/plugin/index.ts#L58 which calls another function that actually calls registerCommTarget(): https://github.com/twosigma/beakerx/blob/c629239ddb7219decd5a9df662c432ad6df5ef62/js/lab/src/plugin/comm.ts#L77 It waits until the context is ready using Promise.all on context.ready. Best, -Scott
On Sun, May 27, 2018 at 2:43 PM John T <[email protected]> wrote: > I created a lab extension using the mimerender-cookiecutter > <https://github.com/jupyterlab/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 > <http://jupyter-notebook.readthedocs.io/en/stable/comms.html>, 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. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jupyter/daaa0d16-8a82-4f90-b866-bc6e37b14862%40googlegroups.com > <https://groups.google.com/d/msgid/jupyter/daaa0d16-8a82-4f90-b866-bc6e37b14862%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- BeakerX.com ScottDraves.com <http://www.ScottDraves.com> @Scott_Draves <http://twitter.com/scott_draves> -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/CAMaD%2Bf7Bo7%3DPXF-u4yYzRq%3DfPgGo%3DXURTU%2BVNV3S%2BVnGMND20w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
