# Frontent to kernel callback

**URL:** <https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666>\
**Category:** JupyterLab\
**Created:** [July 15, 2019, 2:44pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666 "2019-07-15T14:44:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@josh](https://discourse.jupyter.org/u/josh)\
**Post date:** [July 15, 2019, 2:44pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/1 "2019-07-15T14:44:06Z")

</div>

Hi,  
this is Josh, webdeveloper, but new to Jupyterlab development, asking for help getting started exchanging information between (python) kernels and frontend extensions.

This page explains how frontend and kernel can communicate in Jupyter:  
[https://jupyter-notebook.readthedocs.io/en/stable/comms.html#opening-a-comm-from-the-kernel](https://jupyter-notebook.readthedocs.io/en/stable/comms.html#opening-a-comm-from-the-kernel)  
Jupyter **Lab** requires few changes to the frontend index.ts extension instead of the Jupyter…register\_target() call from the Comms document:

```
...
import { INotebookTracker, NotebookPanel }
from '@jupyterlab/notebook';
...
const extension: JupyterFrontEndPlugin<void> = {
  id: 'myid',
  autoStart: true,
  requires: [ICommandPalette, INotebookTracker],
  activate: (app: JupyterFrontEnd, palette: ICommandPalette,
    notebooks: INotebookTracker) => {
    ....
    notebooks.widgetAdded.connect(( sender :any, nbPanel: NotebookPanel ) => {                
      nbPanel.session.ready.then(()=>{
        nbPanel.session.kernel.registerCommTarget('my_comm_target',
            (comm: any) => {
                comm.onMsg = (msg: any) => {
                    console.log("target my_comm_target...",msg)
                };
                let count:number=0;
                setInterval(()=>{
                  console.log("send: ",count);
                  comm.send({'count':count++});
                },2000)
            }
        );
      });
    });
  }  
};

```

Together with the following kernel code from the link above, the frontend receives the “{‘foo’: 2}” data from the kernel:

```
from ipykernel.comm import Comm
my_comm = Comm(target_name='my_comm_target', data={'foo': 1})
my_comm.send({'foo': 2})
@my_comm.on_msg
def _recv(msg):
    print(msg)

```

The problem is that the on\_msg kernel side callback is never reached, although the frontend sends “{‘count’:count++}” every two seconds. What’s wrong with this code?

Josh

---

<div class="post-metadata">

**Author:** ![josh](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@josh](https://discourse.jupyter.org/u/josh)\
**Post date:** [July 17, 2019, 9:52am UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/2 "2019-07-17T09:52:47Z")

</div>

The problem is somewhat different than I thought, and still your professional help is needed to make the callback work as expected.

I have changed the frontend callback code from my previous post to write to a file instead of only to stdout:

```
@my_comm.on_msg
def _recv(msg):
    print(msg)
    with open("/tmp/foo.log","a") as f:
        f.write(str(msg)+"\n")

```

Now the messages are written to the file, but still no output in the browser. It seems like ipythons stdout is redirected to “nowhere” as soon as the Comm object is instantiated. I need to fix this but have no clue how.

Josh

---

<div class="post-metadata">

**Author:** ![josh](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@josh](https://discourse.jupyter.org/u/josh)\
**Post date:** [July 19, 2019, 8:16am UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/3 "2019-07-19T08:16:09Z")

</div>

Hi again,  
is anybody able to reproduce my problem that the kernel stops printing messages when connected to a CommTarget on the frontend?

Any clue why this happens?

Anybody willing to discuss this maybe by email?

It is hard to develop for Jupyter if we don’t get any sort of reply with such a problem.

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![saulshanabrook](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/saulshanabrook/32/159_2.png) [@saulshanabrook](https://discourse.jupyter.org/u/saulshanabrook)\
**Post date:** [July 19, 2019, 6:50pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/4 "2019-07-19T18:50:33Z")

</div>

> [@josh](#):
>
> is anybody able to reproduce my problem that the kernel stops printing messages when connected to a CommTarget on the frontend?

Yes I am. I believe a solution for this would be to have a place in JupyterLab to show stderr/stdout messages that are received when we are not running a cell. There is probably an open issue for this already somewhere.

---

<div class="post-metadata">

**Author:** ![josh](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@josh](https://discourse.jupyter.org/u/josh)\
**Post date:** [July 22, 2019, 2:17pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/5 "2019-07-22T14:17:01Z")

</div>

@saulshanabrook thanks a lot for giving a reply. May I ask you for some more details, where you think an open issue for this problem might exist? I search in all directions and didn’t find any.

What do you think about this cell:

```
import threading
import time

def loop():
    for k in range(60):
        print(k)
        time.sleep(1)
        
        
threading.Thread(target=loop).start()

```

It stops “running” immediately when the thread is started, but the thread continues to write to stdout. This makes me think my initial problem with stdout from kernel Comms is different, more involved?

---

<div class="post-metadata">

**Author:** ![saulshanabrook](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/saulshanabrook/32/159_2.png) [@saulshanabrook](https://discourse.jupyter.org/u/saulshanabrook)\
**Post date:** [July 25, 2019, 4:01pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/6 "2019-07-25T16:01:55Z")

</div>

> [@josh](#):
>
> May I ask you for some more details, where you think an open issue for this problem might exist? I search in all directions and didn’t find any.

I am not sure, unfortunately. I would open a new issue then describing your use case.

---

<div class="post-metadata">

**Author:** ![jasongrout](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jasongrout/32/371_2.png) [@jasongrout](https://discourse.jupyter.org/u/jasongrout)\
**Post date:** [July 26, 2019, 11:17pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/7 "2019-07-26T23:17:17Z")

</div>

Mehmet Bektas recently submitted a PR for a place for output messages to go when they aren’t handled by a cell: [https://github.com/jupyterlab/jupyterlab/pull/6833](https://github.com/jupyterlab/jupyterlab/pull/6833)

---

<div class="post-metadata">

**Author:** ![jasongrout](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jasongrout/32/371_2.png) [@jasongrout](https://discourse.jupyter.org/u/jasongrout)\
**Post date:** [July 26, 2019, 11:19pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/8 "2019-07-26T23:19:06Z")

</div>

One solution is to use an output widget to capture the output: [https://ipywidgets.readthedocs.io/en/stable/examples/Output%20Widget.html#Debugging-errors-in-callbacks-with-the-output-widget](https://ipywidgets.readthedocs.io/en/stable/examples/Output%20Widget.html#Debugging-errors-in-callbacks-with-the-output-widget)

---

<div class="post-metadata">

**Author:** ![mlz](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mlz](https://discourse.jupyter.org/u/mlz)\
**Post date:** [April 6, 2020, 9:08pm UTC](https://discourse.jupyter.org/t/frontent-to-kernel-callback/1666/9 "2020-04-06T21:08:12Z")

</div>

Hi, first, thanks for the code example, very useful. The message is indeed received, here is an example with setting the value to a global variable.

```
last_message=0

@my_comm.on_msg
def _recv(msg):
    global last_message
    last_message = msg
```
