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.
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.
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:
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?