I am trying to obtain the current NoteBook name when running the IPython notebook. I know I can see it at the top of the notebook. What I am after something like
在运行IPython笔记本时,我正在尝试获取当前的笔记本名称。我知道我可以在笔记本的顶部看到它。我追求的是什么
currentNotebook = IPython.foo.bar.notebookname()
I need to get the name in a variable.
我需要在变量中输入名字。
4 个解决方案
#1
20
As already mentioned you probably aren't really supposed to be able to do this, but I did find a way. It's a flaming hack though so don't rely on this at all:
就像前面提到的,你可能真的不能这么做,但是我找到了一个方法。这是一个燃烧的黑客,所以不要依赖这个:
import json
import os
import urllib2
import IPython
from IPython.lib import kernel
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.x
if IPython.version_info[0] < 2:
## Not sure if it's even possible to get the port for the
## notebook app; so just using the default...
notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))
for nb in notebooks:
if nb['kernel_id'] == kernel_id:
print nb['name']
break
else:
sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
print sess['notebook']['name']
break
I updated my answer to include a solution that "works" in IPython 2.0 at least with a simple test. It probably isn't guaranteed to give the correct answer if there are multiple notebooks connected to the same kernel, etc.
我更新了我的答案,包括了一个在IPython 2.0中“工作”的解决方案,至少通过一个简单的测试。如果有多个笔记本连接到同一个内核,它可能不能保证给出正确的答案,等等。
#2
21
I have the following which works with IPython 2.0. I observed that the name of the notebook is stored as the value of the attribute 'data-notebook-name'
in the <body>
tag of the page. Thus the idea is first to ask Javascript to retrieve the attribute --javascripts can be invoked from a codecell thanks to the %%javascript
magic. Then it is possible to access to the Javascript variable through a call to the Python Kernel, with a command which sets a Python variable. Since this last variable is known from the kernel, it can be accessed in other cells as well.
下面是IPython 2.0。我注意到,这个笔记本的名称作为属性“data-notebook-name”的值存储在页面的标记中。因此,首先要让Javascript检索这个属性——Javascript可以从codecell中调用Javascript,这要归功于Javascript的%%的魔力。然后,可以通过调用Python内核来访问Javascript变量,并使用一个命令来设置Python变量。由于最后一个变量是内核中已知的,所以它也可以在其他单元中访问。
%%javascript
var kernel = IPython.notebook.kernel;
var body = document.body,
attribs = body.attributes;
var command = "theNotebook = " + "'"+attribs['data-notebook-name'].value+"'";
kernel.execute(command);
From a Python code cell
来自Python代码单元格
print(theNotebook)
Out[ ]: HowToGetTheNameOfTheNoteBook.ipynb
[]:HowToGetTheNameOfTheNoteBook.ipynb
A defect in this solution is that when one changes the title (name) of a notebook, then this name seems to not be updated immediately (there is probably some kind of cache) and it is necessary to reload the notebook to get access to the new name.
这个解决方案的一个缺陷是,当一个人更改一个笔记本的标题(名称)时,这个名称似乎不会立即更新(可能有某种缓存),并且必须重新加载这个笔记本以访问新的名称。
[Edit] On reflection, a more efficient solution is to look for the input field for notebook's name instead of the <body>
tag. Looking into the source, it appears that this field has id "notebook_name". It is then possible to catch this value by a document.getElementById()
and then follow the same approach as above. The code becomes, still using the javascript magic
[编辑]在反射时,一个更有效的解决方案是查找笔记本名称的输入字段,而不是标记。查看源代码,这个字段似乎有id“notebook_name”。然后可以通过document.getElementById()捕获这个值,然后按照上面的方法进行捕获。代码变得,仍然使用javascript魔法
%%javascript
var kernel = IPython.notebook.kernel;
var thename = window.document.getElementById("notebook_name").innerHTML;
var command = "theNotebook = " + "'"+thename+"'";
kernel.execute(command);
Then, from a ipython cell,
然后,从一个ipython细胞,
In [11]: print(theNotebook)
Out [11]: HowToGetTheNameOfTheNoteBookSolBis
Contrary to the first solution, modifications of notebook's name are updated immediately and there is no need to refresh the notebook.
与第一个解决方案相反,笔记本名称的修改会立即更新,无需刷新。
#3
19
On Jupyter 3.0 the following works. Here I'm showing the entire path on the Jupyter server, not just the notebook name:
在Jupyter 3.0中有以下作品。这里我展示了Jupyter服务器上的整个路径,而不仅仅是笔记本名:
To store the NOTEBOOK_FULL_PATH
on the current notebook front end:
要将NOTEBOOK_FULL_PATH存储在当前的笔记本前端:
%%javascript
var nb = IPython.notebook;
var kernel = IPython.notebook.kernel;
var command = "NOTEBOOK_FULL_PATH = '" + nb.base_url + nb.notebook_path + "'";
kernel.execute(command);
To then display it:
然后显示:
print("NOTEBOOK_FULL_PATH:\n", NOTEBOOK_FULL_PATH)
Running the first Javascript cell produces no output. Running the second Python cell produces something like:
运行第一个Javascript单元不会产生任何输出。运行第二个Python单元格会产生如下内容:
NOTEBOOK_FULL_PATH:
/user/zeph/GetNotebookName.ipynb
#4
10
adding to previous answers,
增加以前的答案,
to get the notebook name run the following in a cell:
要让笔记本名称在单元格中运行以下内容:
%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
this gets you the file name in nb_name
这将获取nb_name中的文件名
then to get the full path you may use the following in a separate cell:
然后,为了获得完整的路径,您可以在单独的单元格中使用以下内容:
import os
nb_full_path = os.path.join(os.getcwd(), nb_name)
#1
20
As already mentioned you probably aren't really supposed to be able to do this, but I did find a way. It's a flaming hack though so don't rely on this at all:
就像前面提到的,你可能真的不能这么做,但是我找到了一个方法。这是一个燃烧的黑客,所以不要依赖这个:
import json
import os
import urllib2
import IPython
from IPython.lib import kernel
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.x
if IPython.version_info[0] < 2:
## Not sure if it's even possible to get the port for the
## notebook app; so just using the default...
notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))
for nb in notebooks:
if nb['kernel_id'] == kernel_id:
print nb['name']
break
else:
sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
print sess['notebook']['name']
break
I updated my answer to include a solution that "works" in IPython 2.0 at least with a simple test. It probably isn't guaranteed to give the correct answer if there are multiple notebooks connected to the same kernel, etc.
我更新了我的答案,包括了一个在IPython 2.0中“工作”的解决方案,至少通过一个简单的测试。如果有多个笔记本连接到同一个内核,它可能不能保证给出正确的答案,等等。
#2
21
I have the following which works with IPython 2.0. I observed that the name of the notebook is stored as the value of the attribute 'data-notebook-name'
in the <body>
tag of the page. Thus the idea is first to ask Javascript to retrieve the attribute --javascripts can be invoked from a codecell thanks to the %%javascript
magic. Then it is possible to access to the Javascript variable through a call to the Python Kernel, with a command which sets a Python variable. Since this last variable is known from the kernel, it can be accessed in other cells as well.
下面是IPython 2.0。我注意到,这个笔记本的名称作为属性“data-notebook-name”的值存储在页面的标记中。因此,首先要让Javascript检索这个属性——Javascript可以从codecell中调用Javascript,这要归功于Javascript的%%的魔力。然后,可以通过调用Python内核来访问Javascript变量,并使用一个命令来设置Python变量。由于最后一个变量是内核中已知的,所以它也可以在其他单元中访问。
%%javascript
var kernel = IPython.notebook.kernel;
var body = document.body,
attribs = body.attributes;
var command = "theNotebook = " + "'"+attribs['data-notebook-name'].value+"'";
kernel.execute(command);
From a Python code cell
来自Python代码单元格
print(theNotebook)
Out[ ]: HowToGetTheNameOfTheNoteBook.ipynb
[]:HowToGetTheNameOfTheNoteBook.ipynb
A defect in this solution is that when one changes the title (name) of a notebook, then this name seems to not be updated immediately (there is probably some kind of cache) and it is necessary to reload the notebook to get access to the new name.
这个解决方案的一个缺陷是,当一个人更改一个笔记本的标题(名称)时,这个名称似乎不会立即更新(可能有某种缓存),并且必须重新加载这个笔记本以访问新的名称。
[Edit] On reflection, a more efficient solution is to look for the input field for notebook's name instead of the <body>
tag. Looking into the source, it appears that this field has id "notebook_name". It is then possible to catch this value by a document.getElementById()
and then follow the same approach as above. The code becomes, still using the javascript magic
[编辑]在反射时,一个更有效的解决方案是查找笔记本名称的输入字段,而不是标记。查看源代码,这个字段似乎有id“notebook_name”。然后可以通过document.getElementById()捕获这个值,然后按照上面的方法进行捕获。代码变得,仍然使用javascript魔法
%%javascript
var kernel = IPython.notebook.kernel;
var thename = window.document.getElementById("notebook_name").innerHTML;
var command = "theNotebook = " + "'"+thename+"'";
kernel.execute(command);
Then, from a ipython cell,
然后,从一个ipython细胞,
In [11]: print(theNotebook)
Out [11]: HowToGetTheNameOfTheNoteBookSolBis
Contrary to the first solution, modifications of notebook's name are updated immediately and there is no need to refresh the notebook.
与第一个解决方案相反,笔记本名称的修改会立即更新,无需刷新。
#3
19
On Jupyter 3.0 the following works. Here I'm showing the entire path on the Jupyter server, not just the notebook name:
在Jupyter 3.0中有以下作品。这里我展示了Jupyter服务器上的整个路径,而不仅仅是笔记本名:
To store the NOTEBOOK_FULL_PATH
on the current notebook front end:
要将NOTEBOOK_FULL_PATH存储在当前的笔记本前端:
%%javascript
var nb = IPython.notebook;
var kernel = IPython.notebook.kernel;
var command = "NOTEBOOK_FULL_PATH = '" + nb.base_url + nb.notebook_path + "'";
kernel.execute(command);
To then display it:
然后显示:
print("NOTEBOOK_FULL_PATH:\n", NOTEBOOK_FULL_PATH)
Running the first Javascript cell produces no output. Running the second Python cell produces something like:
运行第一个Javascript单元不会产生任何输出。运行第二个Python单元格会产生如下内容:
NOTEBOOK_FULL_PATH:
/user/zeph/GetNotebookName.ipynb
#4
10
adding to previous answers,
增加以前的答案,
to get the notebook name run the following in a cell:
要让笔记本名称在单元格中运行以下内容:
%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
this gets you the file name in nb_name
这将获取nb_name中的文件名
then to get the full path you may use the following in a separate cell:
然后,为了获得完整的路径,您可以在单独的单元格中使用以下内容:
import os
nb_full_path = os.path.join(os.getcwd(), nb_name)