Registering Jupyter event handlers in kernel.js

43 views
Skip to first unread message

Sudarshan Raghunathan

unread,
Aug 15, 2017, 5:57:24 PM8/15/17
to Project Jupyter
Apologies if this is a Javascript noob question. I have a kernel.js for my custom kernel that looks like this:

define(function(){

    var onload = function(){
        console.log("I am being loaded")   

    $([IPython.events]).on('status_started.Kernel', function() {
        console.log("status_started.Kernel")
    });

    $([IPython.events]).on('create.Cell', function() {
        console.log("create.Cell")
    });
    }

    return {onload:onload}
})


I got the list of Javascript events from this page: https://jupyter.readthedocs.io/en/latest/development_guide/js_events.html

When my kernel starts, I can see that onload is being called but none of the other event handlers seem to be getting triggered for example, when I add a new cell to the notebook.

Is kernel.js the correct place to register event handlers and if so, is there anything I am doing obviously wrong like using obsolete names for the events?

Thank you in advance,
Sudarshan

Matthias Bussonnier

unread,
Aug 15, 2017, 6:01:58 PM8/15/17
to jup...@googlegroups.com
Hi,

I think that the reason why yo do not get your event trigerd is that
you haven't told `require` that IPython.events needs to be defined
before you are called. You need to use something along the lines of
teh following


define(['base/js/namespace'], function(IPython){
// your code, will be run only once base/js/namespace is loaded and
with IPython == value return by base/js/namespace executed.
// ...
})


It's far from being easy to understand how these things work, but that
should put you on the right track.
--
M
> --
> 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/44d801c6-02fe-48c9-8175-a02eb3dd669f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sudarshan Raghunathan

unread,
Aug 15, 2017, 6:19:31 PM8/15/17
to Project Jupyter
Thank you for the quick response! I think I understand the problem and will try your suggested approach.

Sudarshan Raghunathan

unread,
Aug 17, 2017, 4:40:52 PM8/17/17
to Project Jupyter
Thanks again for your help, this is what the kernel.js ends up looking like:

define(['base/js/namespace'], function(IPython){
    console.log("Kernel event handler loaded")
    $([IPython.events]).on('kernel_ready.Kernel', function() {
        console.log("kernel_ready.Kernel event handler")
    });

    return {onload:function() {console.log("Setting up kernel event handler: %o",IPython)}}
})
Reply all
Reply to author
Forward
0 new messages