如何将jupyter笔记本电脑连接到现有的ipython内核



我正在尝试将jupyter笔记本电脑连接到ipython内核,该笔记本是通过IPython.kernel_embed()启动在Jupyter Notebook Server之外的Ipython内核。

我可以用jupyter console --existingjupyter qtconsole --existing固定它,但是我不能使用jupyter notebook进行操作,因为笔记本不支持--existing标志。如本期所述,这并不是因为任何技术限制,而是因为从UI的角度来看会混淆。

我能够成功地与

的jupyter笔记本中的内核进行交互
from jupyter_client import BlockingKernelClient
client = BlockingKernelClient()
client.load_connection_file('/Users/ebanner/Library/Jupyter/runtime/kernel-10962.json')
client.start_channels()

和问题client.execute_interactive()。但是,我真的很想避免在每个单元格中运行client.execute_interactive()

我尝试了几件事。首先,我尝试更改jupyter_notebook_config.py中的c.ConnectionFileMixin.*_-port变量,并编写自己的自定义内核管理器,并通过c.NotebookApp.kernel_manager_class设置为

from tornado import gen, web
from jupyter_client import KernelManager
from notebook.services.kernels.kernelmanager import MappingKernelManager
class ExistingMappingKernelManager(MappingKernelManager):
    """A KernelManager that just connects to an existing kernel."""
    @gen.coroutine
    def start_kernel(self, kernel_id=None, path=None, **kwargs):
        kernel_id = 1
        km = KernelManager(kernel_name='python3')
        kc = km.client()
        kc.load_connection_file('/Users/ebanner/Library/Jupyter/runtime/kernel-10962.json')
        kc.start_channels()
        try:
            kc.wait_for_ready()
        except RuntimeError:
            kc.stop_channels()
            raise
        raise gen.Return(kernel_id)

但是到目前为止,这些方法都失败了。

最有前途的路线似乎是覆盖KernelManager._launch_kernel(),尽管我不确定该如何覆盖它,因为它当前返回由ipykernel启动的内核过程中的subprocess.Popen()实例。

任何帮助将不胜感激。

我最终创建了一个似乎可以完成工作的黑客内核经理。

https://github.com/ebanner/extipy

唯一的警告是,您必须禁用消息身份验证才能使其工作,因为Jupyter抱怨预期的消息签名不匹配。

在这里进行更多讨论

https://groups.google.com/forum/# !! topic/jupyter/qamkem52xn0

最新更新