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

http://jupyter-notebook.readthedocs.io/en/stable/comms.html

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

http://jupyterlab.readthedocs.io/en/stable/developer/xkcd_extension_tutorial.html

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

On Sunday, May 27, 2018 at 6:38:45 PM UTC-7, John T wrote:
>
> (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.
>  
>
> On Sunday, May 27, 2018 at 2:43:45 PM UTC-4, John T 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/4d4e225b-7479-4e3a-bcd5-3538134488d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to