Getting directory of current notebook within notebook

Hi, is there a way to get the path to the currently running jupyter python notebook from within that notebook on ARE? Knowing the path to the current notebook would be super handy for saving outputs in the same folder as the notebook file.

From a bit of web searching it seems there’s no general solution, but lots of approaches that work in particular circumstances. I’ve tried a lot of them, but nothing has worked so far for JupyterLab on ARE.

@aekiss I just asked around at ACCESS-NRI and the consensus is that this is very difficult to do and no one has found a general solution to this…

Perhaps someone else in the community has found a solution?

OK ta. I don’t need a general solution, just one that works on ARE.

From what I read, it’s simply not possible to reliably get the path to a python notebook:

What is your use case?

If you have a single notebook and you want to create output for, then my advise would be to write code using always relative paths to the root of the project you are working on and always run code with the project root as the working directory. This ensures that your code is portable; you can move your whole project somewhere else and everything will keep working.

On ARE, you can do this with the `–notebook-dir` argument. For example, if you add this:

The your working directory will be `~/research/access-exp` and any code you run in that session will use that as the root.

If you have code that creates a bunch of output files and want the path to the script to be some sort of automatic “argument”, then you could work with regular python scripts and use


import os
file_path = os.path.realpath(__file__)

To get the path to the script and then run it from the terminal.

I hope that helps.

Try setting up a token for jupyter lab

echo "c.IdentityProvider.token = '$(uuidgen)'" >> ~/.jupyter/jupyter_lab_config.py

then

import ipynbname
ipynbname.path()

#> PosixPath('/home/562/saw562/home/tmp/Untitled41.ipynb')

Make sure the config file is only readable by you

chmod go-rwx ~/.jupyter

You’ll need to substitute $HOME with $HOME/.jupyter_root to get the true path

NCI has developed a Python package called nci-ipynb to help identify the directory where your notebook is located when running it in an ARE JupyterLab session. For more information and usage instructions, see:

https://opus.nci.org.au/spaces/DAE/pages/477823998/NCI-ipynb

4 Likes

Great, thanks all, I was wanting something I could put in the notebook so that anyone could clone it from a repo and have it save files to the cloned dir when run on ARE, and it looks like @rui.yang’s solution would work for that use case.

Would it be possible to include nci-ipynb in conda/analysis3?

That’s a great idea to add nci-ipynb to conda/analysis3! I just started an issue in the conda/analysis3 repo to request the addition.

2 Likes

Great, thanks @paigem :slight_smile: