Reputation: 704
Hi all,
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.
I like the simplicity of the answer suggested by @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
''
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').
My Jupyter version is
jupyter notebook --version
6.0.3]
jupyter-lab --version
2.1.1
I am using my notebook mostly for python code.
@juan solution [here], using ipynbname is working for
jupyter-notebook : 6.1.6
jupyter lab : 2.2.6
but this solution is still not working for jupyter lab : 3.0.1
ipynbname is now working for jupyter 3
More details about it [here]
Upvotes: 8
Views: 9101
Reputation: 63
For those lacking ipynbname
, this JavaScript hack will work to grab the Jupyter notebook filename and path and save them to variables accessible by Python. However, you cannot access those variables in the same cell as you run the JavaScript code. So if you want to do anything with the variables, it'll take a second cell.
Proof-of-concept: A simple Jupyter notebook, world.ipynb
, in directory hello
.
Cell #1:
def get_notebook_name():
"""Execute JS code to save Jupyter notebook name to variable `notebook_name`"""
from IPython.core.display import Javascript, display_javascript
js = Javascript("""IPython.notebook.kernel.execute('notebook_name = "' + IPython.notebook.notebook_name + '"');""")
return display_javascript(js)
def get_notebook_path():
"""Execute JS code to save Jupyter notebook path to variable `notebook_path`"""
from IPython.core.display import Javascript, display_javascript
js = Javascript("""IPython.notebook.kernel.execute('notebook_path = "' + IPython.notebook.notebook_path + '"');""")
return display_javascript(js)
# execute javascript
get_notebook_name()
get_notebook_path()
Cell #2
# print results
print(f"{notebook_name=}")
print(f"{notebook_path=}")
Results:
notebook_name='world.ipynb'
notebook_path='hello/world.ipynb'
Upvotes: -2
Reputation: 357
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.
Upvotes: 21
Reputation: 1
You need to put the import code in a separate cell above the function (ipyparams.notebook_name). Otherwise it will try to run the code before the library is fully loaded, so the currentNotebook variable is still blank. Putting the import in its own cell forces it to load before moving to cell #2.
Upvotes: -2