7

Problem

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').

Configuration details

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.


Edit 27/01/2021

@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


Edit 28/01/2021

ipynbname is now working for jupyter 3

More details about it [here]

lhoupert
  • 584
  • 8
  • 25
  • Caveat: New issue opened at https://github.com/msm1089/ipynbname/issues/10 because for me it works with ipykernel but not with Xeus. – Julian Moore Aug 08 '21 at 12:14

3 Answers3

17

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.

Juan Pablo
  • 317
  • 2
  • 8
  • 1
    Thank you @Juan! Have you tried with `jupter-lab` too? In my case it only works with `jupyter-notebook` – lhoupert Jan 27 '21 at 08:28
  • 1
    When running it in `jupyter-lab`, I got the error `FileNotFoundError: Can't identify the notebook name` – lhoupert Jan 27 '21 at 08:29
  • Yes, I only tried it in jupyter lab. @Bill's solution doesn't work for me either, but this one does. `jupyter lab --version 2.2.6` `conda --version 4.9.2` – Juan Pablo Jan 27 '21 at 13:11
  • Please take a look at this: [link](https://stackoverflow.com/questions/50687077/cant-run-any-ipynb-files-using-jupyter-lab-or-jupyter-notebook-filenotfounderr/52558580) – Juan Pablo Jan 27 '21 at 13:22
  • Thanks, I had a look I already have `nb_conda` install. By any chance do you have jupyterlab 3.0 install? maybe it works only for jupyter-lab <3.0 – lhoupert Jan 27 '21 at 13:27
  • 2
    So I just tried with `jupyter lab --version 2.2.6` and it is working. The problem appears on jupyter-lab >3.0 – lhoupert Jan 27 '21 at 13:35
  • 2
    Sure enough, I tried in jupyter lab 3.0.5 and it doesn't work. It seems to be then there is a problem with versions higher than 3 – Juan Pablo Jan 27 '21 at 14:30
  • 3
    After opening an issue on the `ipynbname` repository , they added the compatibility for `jupyter >3` . See issue [[here](https://github.com/msm1089/ipynbname/issues/2#issuecomment-768879726)] – lhoupert Jan 28 '21 at 08:24
  • It worked fine for me with jupyter lab 3.1.2 provided I was using ipykernel; it did not work with Xeus kernel. – Julian Moore Aug 09 '21 at 14:20
  • 1
    Confirming that works perfectly in `jupyter lab --version 3.3.2` – P. Navarro Mar 23 '22 at 15:59
  • 1
    This work for me but extremely slow 20+ seconds, is it expected ? – Juan Osorio Dec 20 '22 at 17:39
-2

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.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
  • Thank you @mistertham. I tried but it doesn't solve the problem. I have edited my initial post in which I separate the import and the function call. – lhoupert Jul 15 '20 at 12:53
-2

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'
Brian
  • 43
  • 1