【问题标题】:How do I connect a jupyter notebook to an existing IPython kernel?如何将 jupyter notebook 连接到现有的 IPython 内核?
【发布时间】:2018-02-22 05:25:55
【问题描述】:

我正在尝试将 jupyter notebook 连接到 IPython 内核,该内核是通过 IPython.kernel_embed() 在 jupyter notebook 服务器之外启动的。

我可以用jupyter console --existing and jupyter qtconsole --existing 很好地附加它,但我不能用jupyter notebook 做它,因为笔记本不支持--existing 标志。正如this issue 中提到的,这不是因为任何技术限制,而是因为从 UI 的角度来看它会造成混淆。

我能够成功地与来自 jupyter notebook 的内核交互

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() 的实例。

任何帮助将不胜感激。

【问题讨论】:

    标签: google-cloud-platform jupyter


    【解决方案1】:

    我最终创建了一个看起来可以完成这项工作的 hacky 内核管理器。

    https://github.com/ebanner/extipy

    唯一需要注意的是,您必须禁用消息身份验证才能使其正常工作,因为 jupyter 抱怨预期的消息签名不匹配。

    更多讨论在这里

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2017-03-15
      • 2019-07-27
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多