IPython notebook: How to connect to existing kerne

2020-07-02 22:26发布

I am able to connect two ipython console session to one kernel by:

$ ipython console
In [1]: %connect_info  
{
   ... Content of JSON with info for connecting ...
}

Paste the above JSON into a file, and connect with:
    $> ipython <app> --existing <file>
or, if you are local, you can connect with just:
    $> ipython <app> --existing kernel-43204.json
or even just:
    $> ipython <app> --existing
    if this is the most recent IPython session you have started.

And accordingly I can substitute the <app> by console again

$ ipython console --existing kernel-43204.json

However, I want to share my kernel with ipython notebook so I can visualize my data. I tried and failed with:

$ ipython notebook --existing kernel-43204.json
[C 13:35:01.025 NotebookApp] Unrecognized flag: '--existing'

Any suggestion how I can work and switch between ipython console and ipython notebook?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-02 22:50

There is no UI, nor API to do that with the notebook, there is an assumption for code simplicity that the notebook is the one that own and start the kernel. You will have to write your own KernelManager subclass and configure IPython to use it (+ write a bit of UI code, if you want it easy to use), for you to be able to select an already existing kernel.

查看更多
成全新的幸福
3楼-- · 2020-07-02 22:50

Here’s the example of custom kernel manager that allows Jupyter notebook to kernel created externally.

https://github.com/ebanner/extipy

It’s hacky solution at best.

Jupyter folks can hopefully create such custome kernel class and include it in package and enable it via simple —existing switch. I don’t see any reason why they can’t do that.

查看更多
来,给爷笑一个
4楼-- · 2020-07-02 22:57

I'll give you a solution the other way around. Instead of connecting a notebook to an existing kernel, you can easily connect an ipython session to a kernel that was started by a notebook.

  1. Start your notebook. Now you have a running kernel.
  2. In a code cell, run the magic command %qtconsole

Now you have a console and the notebook connected to the same kernel. You can run the magic command multiple times and have multiple consoles.

BTW, qtconsole is a very smart console. It is even better than the terminal one, especially if you are a Windows user.

查看更多
登录 后发表回答