如何在Jupyter笔记本中获取活动内核名称
我想制作一个打印活动内核名称的笔记本。
我使用
I would like to make a notebook that prints the active kernel name. I registered multiple venvs as kernels using the
python -m ipykernel install --user --name <kernel_name>
在笔记本中我想打印一些关于活动内核的元数据。
In the notebook I would like to print some metadata about the active kernel.
如何获得活动内核的名称?
How can one get the name of the active kernel?
编辑:
以下是我的堆栈中的特定版本
Here are the particular versions in my stack
ipykernel==4.5.2
ipython==5.3.0
jupyter==1.0.0
尽管如此,这个问题一般应该回答。
nevertheless, the question should be answered in general.
一种方法是在笔记本中使用Javascript并执行一些使代码在Python中可用的代码:
One way to do this is using Javascript within the notebook, and executing some code to make the string available in Python:
%%javascript
var kernel = Jupyter.notebook.kernel
kernel.execute('kernel_name = ' + '"' + kernel.name + '"')
然后你有Python的内核名称:
Then you have the kernel name in Python:
print(kernel_name)
# my-kernel
作为额外奖励,您可以使用 jupyter_client
模块,一旦你有了它的名字,就可以找到有关内核的更多信息:
As an added bonus, you can use the jupyter_client
module to find more information about the kernel once you have its name:
from jupyter_client import kernelspec
spec = kernelspec.get_kernel_spec(kernel_name)
print(spec.resource_dir)
# /path/to/my/kernel