Jupyter客户端通过Python连接到运行内核

Sci*_*McQ 13 python ipython jupyter jupyter-notebook

我正在尝试以编程方式(使用Python)与我正在运行的jupyter内核进行交互,作为原型实验.

我在浏览器中运行了一个jupyter笔记本,我通过magic命令从笔记本中获取了连接信息

%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"
}
Run Code Online (Sandbox Code Playgroud)

KernelClient类的jupyter_client文档让我相信我可以连接这个信息并用这个对象监听/显示代码,并发送代码通过我连接到的内核执行但是,我没有运气好我一直在尝试如下:

>>> 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
Run Code Online (Sandbox Code Playgroud)

编辑:为了清晰起见添加此部分@Sam H.建议我这样做但它没有用

>>> 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
Run Code Online (Sandbox Code Playgroud)

mus*_*rat 4

我过去玩过KernelClient,取得了一点点成功,诚然,我认为成功非常非常小。我让事情正常工作的唯一方法是通过 a KernelManager(你没有)。例如尝试:

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
Run Code Online (Sandbox Code Playgroud)

通常(但并非总是)返回:

the kernel produced data {'text/plain': '4'}
Run Code Online (Sandbox Code Playgroud)

我做了一个快速的谷歌搜索,我发现唯一的代码或SO帖子表明我走在正确的轨道上是:这篇文章

请注意,我认识到这绝不是一个完整的答案,但希望这是朝着正确方向迈出的一步。如果您取得了成功,我很乐意学习。