Bidirectional Comm Communication

69 views
Skip to first unread message

Dominic Kuang

unread,
May 5, 2017, 12:48:49 PM5/5/17
to Project Jupyter
I've been trying to figure out how to use comms to communicate from Python <-> JS. Using the following simple example:

# Cell 1
from IPython.kernel.comm import Comm

def handle_comm_message(message):
    print(message)

comm = Comm('comm-target-name')
comm.on_msg(handle_comm_message)

// Cell 2
%%javascript
 
var IPython = window.IPython IPython.notebook.kernel.comm_manager.register_target('comm-target-name', function(comm, data) {
    comm
.on_msg(function(msg) { console.log(msg) });
    comm
.send({'data': 'sent from the client'})
});


# Cell 3
comm
.send(data={'data': 'sent from python'})

I can reliably get Python -> JS to work. In this case, after executing Cell 3, I receive the message in the console containing "Sent from Python" as expected. However, I am unable to trigger the sending from client. I am not sure if I have something set up incorrectly or I am simply misunderstanding how comms work. Any help would be appreciated! Thanks!
Message has been deleted

Dominic Kuang

unread,
May 5, 2017, 12:54:29 PM5/5/17
to Project Jupyter
I've uploaded the notebook here for convenience. http://nbviewer.jupyter.org/gist/finaiized/ad8836b64a874518d32c40e7a4a03966

Jason Grout

unread,
May 5, 2017, 6:03:25 PM5/5/17
to Project Jupyter

Try switching your cell one and two.

(Sorry, on phone so can't test)

Thanks,

Jason


On Fri, May 5, 2017, 09:54 Dominic Kuang <fina...@gmail.com> wrote:
I've uploaded the notebook here for convenience. http://nbviewer.jupyter.org/gist/finaiized/ad8836b64a874518d32c40e7a4a03966

--
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/f2c16cc5-b8bf-4ae4-9084-adab48d1dbce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dominic Kuang

unread,
May 8, 2017, 12:16:32 PM5/8/17
to Project Jupyter, ja...@jasongrout.org
Thanks for the reply Jason. I tried switching the cells and randomly trying different execution orders but to no avail. If I'm not wrong, I should expect to see something printed in the output cell from the Python callback being triggered.

Jason Grout

unread,
May 8, 2017, 11:34:22 PM5/8/17
to Dominic Kuang, Project Jupyter
Ah, right. In order to see output, you'll need to set the right javascript callbacks to handle the resulting display messages. I think if you make your python handler write to a file, you'll see the output in the file. Alternatively, you can use an Output ipywidget to capture the output:

from ipywidgets import Output
out = Output()
def handle_comm_message(message):
    with out:
        print(message)

and then display the output widget in another cell somewhere

Thanks,

Jason

Dominic Kuang

unread,
May 9, 2017, 12:34:01 PM5/9/17
to Project Jupyter, fina...@gmail.com, ja...@jasongrout.org
That works perfectly Jason, thanks! I also goofed up by not putting the comm.send inside the on_msg callback for some reason, which is what I had original wanted. Appreciate the help!
Reply all
Reply to author
Forward
0 new messages