Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to get jupyter server root directory

Consider following:

$ cd /home/mydir
$ jupyter notebook --port=8888

In plain English, I am running jupyter server from /home/mydir directory.

Is there a simple way to get this directory from within a notebook regardless if it's a R notebook or a Python notebook or whatever? Maybe there is some magic command or variable?

NOTE: getwd() is not an answer as it returns directory of a current notebook but not the jupyter server root.

like image 605
Matrix Norm Avatar asked Sep 15 '17 21:09

Matrix Norm


Video Answer


2 Answers

I have a similar problem and found your post, although I don't see a viable solution yet. Eventually I did found a solution, although it works only because I only care about Linux and I only care about Python. The solution is this magic line:

J_ROOT = os.readlink('/proc/%s/cwd' % os.environ['JPY_PARENT_PID'])

(I put it in a module in my PYTHONPATH so that I can easily use it in any Python notebook.) See if it is good for you.

like image 53
Isaac To Avatar answered Sep 25 '22 00:09

Isaac To


Remember that your iPtyhon is just a Python module, so you can execute any valid Python code in a cell. So, if you started your notebook and haven't executed any directory changes in your code, you should be able to retrieve your cwd with the following in a cell:

import os
os.getcwd()

But furthermore, you can execute shell commands in cells, so you can retrieve other information in the cell. For example:

!which jupyter

should give you the path to your jupyter executable.

Which then leads you to running something like:

!jupyter --paths

which should give you something similar to: ​

config:
    /Users/yourdir/.jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /Users/yourdir/Library/Jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /Users/yourdir/Library/Jupyter/runtime
like image 31
Shawn Mehan Avatar answered Sep 24 '22 00:09

Shawn Mehan