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

标签 python ipython jupyter-notebook jupyter

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

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

%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"
}

jupyter_client docs对于 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

编辑:根据@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

最佳答案

我过去曾使用过 KernelClient,取得了一点点成功,诚然认为成功非常非常少。我让事情正常工作的唯一方法是通过 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

通常(但不总是)返回:

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

我做了一个快速的谷歌搜索,我发现唯一的代码或 SO 帖子有一些迹象表明我在正确的轨道上是:This post .

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

关于python - Jupyter 客户端通过 Python 连接到正在运行的内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48122157/

相关文章:

ipython - 如何获取MathJax以在ipython Notebook中启用mhchem扩展

eclipse - IPython 在 PyDev 控制台中不可用

python - KernelRestarter : restart failed in jupyter , 内核已死

python - 如何将列表分配给 torch.tensor?

python - Scrapy 内存错误(请求太多)Python 2.7

python - 设置 selenium 以与 Internet Explorer 一起使用

python - 在哪里可以找到 Google App Engine 中 webapp2 session.get 的详细语法?

ipython - Jupyter笔记本: command for hide the output of a cell?

python - 如何在 Jupyter Notebook 中永久地将 'Completer.use_jedi' 配置为 'False'

python - leetcode 爆破气球超时