获取当前的jupyter-lab笔记本名称(适用于Jupyter-lab版本2.1和3.0.1以及笔记本版本> 6.0.3) [英] Get current jupyter-lab notebook name [for Jupyter-lab version 2.1 and 3.0.1 and notebook version >6.0.3)
问题描述
大家好,
正如我的标题所建议的那样,我想访问笔记本名称(在 jupyter-lab 中)作为变量.因此,我可以在笔记本本身中重用它(例如,命名笔记本中生成的一些图形文件).
As my title suggested it, I would like to get access to the notebook name (in jupyter-lab) as a variable. So I could reuse it in the notebook itself (for example to name some figure files generated in the notebook).
我看到几年前也出现过类似的问题 [请参阅此处] .但是我没有找到满意的答案.
I saw that a similar issue was opened years ago [see here]. However I didnt find a satisfactory answer.
我喜欢 @bill建议的答案的简单性:
import ipyparams
currentNotebook = ipyparams.notebook_name
但是,它对我不起作用.我在第一次执行第一个单元格时收到此警告:
However, it doesn't work for me. I got this warning the first time I execute the first cell:
import ipyparams
Javascript Error: Jupyter is not defined
currentNotebook = ipyparams.notebook_name
currentNotebook
''
然后,如果我再次重新运行该单元格,则不再有警告消息,但是变量currentNotebook
仍然为空. (我按顺序运行单元,但没有执行运行所有单元".)
Then if I rerun the cell again, I don't have the warning message anymore but the variable currentNotebook
is still empty. (I run the cell sequentially, I didn't do a 'Run All Cells').
我的Jupyter版本是
My Jupyter version is
jupyter notebook --version
6.0.3]
jupyter-lab --version
2.1.1
我主要将笔记本用于 python 代码.
I am using my notebook mostly for python code.
@juan解决方案[此处],使用
@juan solution [here], using ipynbname is working for
jupyter-notebook : 6.1.6
jupyter lab : 2.2.6
,但此解决方案仍不适用于 jupyter lab : 3.0.1
ipynbname 现在适用于jupyter 3
ipynbname is now working for jupyter 3
有关它的更多详细信息[此处]
More details about it [here]
推荐答案
As an alternative you can use the following library: ipynbname
#! pip install ipynbname
import ipynbname
nb_fname = ipynbname.name()
nb_path = ipynbname.path()
这对我有用,解决方案非常简单.
This worked for me and the solution is quite straightforward.
这篇关于获取当前的jupyter-lab笔记本名称(适用于Jupyter-lab版本2.1和3.0.1以及笔记本版本> 6.0.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!