如何在Jupyter笔记本中获取活动内核名称 [英] How to get active kernel name in Jupyter notebook

查看:305
本文介绍了如何在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

这篇关于如何在Jupyter笔记本中获取活动内核名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆