16

I am trying to programmatically (with Python) interact with my running jupyter kernel as a prototyping experiment.

I have a jupyter notebook running in my browser and I got the connection info from the notebook via the magic command

%connect_info
{
 "signature_scheme": "hmac-sha256",
 "shell_port": 49545,
 "kernel_name": "",
 "iopub_port": 49546,
 "stdin_port": 49547,
 "hb_port": 49549,
 "control_port": 49548,
 "key": "1a359267-f30d84302c39d352d6ac17c3",
 "transport": "tcp",
 "ip": "127.0.0.1"
}

The jupyter_client docs for KernelClient class leads me to believe I can connect with this info and listen to / display code with this object as well as send code to be executed via the kernel I am connected to but, I am not having any luck with what I have been trying such as:

>>> from jupyter_client import KernelClient
>>> connection = {
  "signature_scheme": "hmac-sha256",
  "shell_port": 49545,
  "kernel_name": "",
  "iopub_port": 49546,
  "stdin_port": 49547,
  "hb_port": 49549,
  "control_port": 49548,
  "key": "1a359267-f30d84302c39d352d6ac17c3",
  "transport": "tcp",
  "ip": "127.0.0.1"
}
>>> client = KernelClient(**connection)
>>> client.history()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/envs/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 347, in history
self.shell_channel.send(msg)
  File "/path/to/envs/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
>>> client.start_channels()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 101, in start_channels
self.shell_channel.start()
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
 TypeError: object() takes no parameters
 >>> client.load_connection_info(connection)
 >>> client.execute('print("Hello World")')
   Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 254, in execute
self.shell_channel.send(msg)
   File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
   TypeError: object() takes no parameters

EDIT: Adding this section for clarity given @Sam H. suggestion I did this but it did not work

>>> from jupyter_client import KernelClient
>>> kc = KernelClient()
>>> kc.load_connection_info(connection)
>>> kc.start_channels()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 101, in start_channels
  self.shell_channel.start()
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
  socket, self.session, self.ioloop
  TypeError: object() takes no parameters

1 Answer 1

4

I have played with the KernelClient in the past, with a very little bit of success, thought admittedly very very little success. The only way I have gotten things to work is via a KernelManager (which you did not have). For example try:

from jupyter_client import KernelManager
km = KernelManager()
km.start_kernel()
kc = km.client()
# now execute something in the client
kc.execute("2+2")
while True:
    try:
        kc_msg = kc.get_iopub_msg(timeout=1)
        if 'content' in kc_msg and 'data' in kc_msg['content']:
            print('the kernel produced data {}'.format(kc_msg['content']['data']))
            break        
    except:
        print('timeout kc.get_iopub_msg')
        pass

That typically (but not always) returns:

the kernel produced data {'text/plain': '4'}

I did a quick google search and the only code or SO post I found that had some indication that I was on the right track is: This post.

Note I recognize this is by no means a complete answer but hopefully it's a step in the right direction. If you have had success I would be glad to learn.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.