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

Jupyter客户端通过Python连接到正在运行的内核,python,ipython,jupyter-notebook,jupyter,Python,Ipython,Jupyter Notebook,Jupyter,我正在尝试以编程方式(使用Python)与正在运行的jupyter内核交互,作为一个原型实验 我有一个jupyter笔记本在我的浏览器中运行,我通过magic命令从笔记本获取连接信息 %connect_info { "signature_scheme": "hmac-sha256", "shell_port": 49545, "kernel_name": "", "iopub_port": 49546, "stdin_port": 49547, "hb_port": 49549,

我正在尝试以编程方式(使用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"
}
for KernelClient类让我相信,我可以连接此信息,监听/显示此对象的代码,并通过连接到的内核发送要执行的代码,但是,我一直在尝试的东西没有任何运气,例如:

>>> 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
>>来自jupyter\u客户端导入内核客户端
>>>连接={
“签名方案”:“hmac-sha256”,
“壳牌港口”:49545,
“内核名称”:“”,
“iopub_端口”:49546,
“标准港”:49547,
“hb_港”:49549,
“控制端口”:49548,
“钥匙”:“1a359267-f30d84302c39d352d6ac17c3”,
“传输”:“tcp”,
“ip”:“127.0.0.1”
}
>>>client=KernelClient(**连接)
>>>client.history()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/path/to/envs/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第347行,在历史记录中
self.shell\u channel.send(msg)
文件“/path/to/envs/jupyter nb/lib/python3.5/site packages/jupyter\u client/client.py”,第143行,在shell\u频道中
套接字、self.session、self.ioloop
>>>client.start_channels()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第101行,在start_通道中
self.shell_channel.start()
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第143行,在shell_频道中
套接字、self.session、self.ioloop
TypeError:对象()不接受任何参数
>>>客户端。加载连接信息(连接)
>>>client.execute('print(“Hello World”)'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,执行中的第254行
self.shell\u channel.send(msg)
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第143行,在shell_频道中
套接字、self.session、self.ioloop
TypeError:对象()不接受任何参数
编辑:添加此部分是为了澄清@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
>>来自jupyter\u客户端导入内核客户端
>>>kc=KernelClient()
>>>kc.负载连接信息(连接)
>>>kc.启动_频道()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第101行,在start_通道中
self.shell_channel.start()
文件“/path/to/jupyter nb/lib/python3.5/site packages/jupyter_client/client.py”,第143行,在shell_频道中
套接字、self.session、self.ioloop
TypeError:对象()不接受任何参数

我以前玩过
KernelClient
,只取得了一点点成功,但不可否认的是,我认为成功很少。我让事情正常运行的唯一方法是通过
内核管理器
(您没有)。例如,请尝试:

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
通常(但不总是)返回:

我在谷歌上快速搜索了一下,我发现的唯一一条表明我走对了方向的代码是:

注意我承认这绝不是一个完整的答案,但希望这是朝着正确方向迈出的一步。如果你成功了,我很高兴知道

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