【发布时间】:2018-01-05 22:27:08
【问题描述】:
我正在尝试以编程方式(使用 Python)与我正在运行的 jupyter 内核进行交互,作为原型设计实验。
我的浏览器中运行着一个 jupyter notebook,我通过魔法命令从 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"
}
KernelClient 类的jupyter_client docs 让我相信我可以连接到此信息并使用此对象收听/显示代码以及发送要通过我连接到的内核执行的代码,但是,我没有我一直在尝试的任何运气,例如:
>>> 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
【问题讨论】:
标签: python ipython jupyter-notebook jupyter