Hi,
this is Josh, webdeveloper, but new to Jupyterlab development, asking for help getting started exchanging information between (python) kernels and frontend extensions.
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?
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.
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.
@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?