Accessing Root in Jupyter Notebook - python

I've started a notebook from the D:/ drive, but navigated a few directories down. My notebook is at D:/dir1/dir2/notebook.ipynb. In my current notebook, I want to execute a script in the root of D:/, where my notebook session was started from.
I want to avoid relative path changes, and was hoping there's a way to access the directory location of where I had started the notebook (the location corresponding to localhost:xxxx/tree. Is that possible?

This is a slightly hacky way, but works:
import jupyter_core
import os,glob,json
jrd = jupyter_core.paths.jupyter_runtime_dir()
with open(glob.glob(jrd+'/nbserver-*.json')[0]) as json_file:
root_dir = json.load(json_file)['notebook_dir']
The reason for the globbing is because the json file you are looking for has a number corresponding to the process id (PID) in its name.
Therefore this method will be guaranteed to work if you have only one notebook instance. If you know the PID you don't need to use glob

Related

How to add to the pythonpath in jupyter lab

I am trying to work with jupyterlab on a remote server that I don't manage, and I want to add my custom libraries to the path so that I can import and use them. Normally, I would go into .bashrc and add to PYTHONPATH there using
export PYTHONPATH="/home/username/path/to/module:$PYTHONPATH"
but this hasn't worked. I have tried this in .bashrc and .bash_profile to no fortune. I have also tried
export JUPYTER_PATH="/home/username/path/to/module:$JUPYTER_PATH"
as I read that somewhere else, and tried it in both the files named above.
What else can I try?
Ideally I'd like to put in some line in jupyterlab that returns the file it is using to add to the path, is that possible?
Or perhaps there is some command I can type directly into a terminal that I can access through jupyterlab that would allow me to add things to my path perminantley. I know that I can use os.path.insert (or similar) at the start of a notebook but as there are certain things I will want to use in every notebook this is a less than ideal solution for me.
Thanks
In a Specific Notebook
Manually append the path to sys.path in the first cell of the notebook
import sys
extra_path = ... # whatever it is
if extra_path not in sys.path:
sys.path.append(extra_path)
As a System Configuration
Modify ~/.ipython/profile_default/ipython_config.py using the shell functionality so that the path gets modified for every notebook.
If that file does not exist, create it by using ipython profile create.
Then insert the modification to sys.path into it by modifying the c.InteractiveShellApp.exec_lines variable, e.g.
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append(<path to append>)'
]
Partially stolen from this answer, which has a different enough context to warrant being a different question.

How do I read a csv file in a seperate parent folder?

I have a directory with the following subdirectories
|---Data
|
|---Notebooks
The Data directory contains csv files, while the Notebooks directory contains my Jupyter notebook files. How do I access the file from the Data directory with a notebook at the Notebooks directory?
My initial idea was this:
df = pd.read_csv('../Data/csvFile')
However the code block renders a file not found error in Pandas.
The path you'll need will be relative to the directory you're running the Python process from normally, but you can use the following trick to make it relative to a file you're running instead:
from pathlib import Path
df = pd.read_csv(Path(__file__).parent/'Data'/'csvFile')
However the __file__ variable is not defined in Jupyter notebooks, so you'll have to use this hack instead to get the directory the notebook is in:
from pathlib import Path
df = pd.read_csv(Path(globals()['_dh'][0])/'Data'/'csvFile')
Note that this hack only works when running from Jupyter. Unfortunately I don't have a one-size-fits-all approach, unless you count detecting if you're in Jupyter, and then selecting which method you'll use based off of that.

how to get the file path for the ipython notebook in use? (the equivalent to __file__)

I want to change dir to the parent dir of my jupyter notebook.
I can not take the notebook path using os.path.basename(os.path.dirname(os.path.realpath(__file__))) as __file__ is not defined.
How can I get the dir of the ipynb file I am using in order to os.chdir() to it?
You can't
https://github.com/ipython/ipython/issues/10123
The reason is because you're always running in the kernel, and in theory multiple notebooks could connect to that kernel.
However - by default if you're starting a notebook, the current working directory is set to the path of the notebook. So the closest, is to call os.getcwd()
I just created the most boring published notebook example to demonstrate this, you can see that, this notebook reflects it's path, and this one that's in a subdirectory also reflects the proper path

Always have Jupyter notebook load with certain options / packages

Is there a way to ensure that jupyter notebook always starts with either:
1) Certain packages imported
and/or
2 Certain options set
I don't want to have to type the same things everytime at the the top of each notebook I run - e.g. always using numpy or pandas.
Additionally, I always want to be able to see multiple output per cell. I use the following code to let this work just fine, but I want this saved as some sort of template, that doesn't require manual effort from me to type each time.
Thanks!
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
First, you find where the startup folder is located.
# on Jupyter notebook
import IPython
IPython.paths.get_ipython_dir()
On Windows, the response is u'C:\\Users\\yourname\\.ipython', while on Linux ~/.ipython.
In that location, there are profile folders. At least, there is a profile_default on your computer. One startup folder exists in each profile folder.
You put a python script file in that folder (my case: C:/Users/myname/.ipython/profile_default/startup).
I name my script file 00-first.py, and put this code in it:
import numpy as np
import pandas as pd
When I start the Jupyter notebook server with the default profile, the startup script will be executed prior to opening a Jupyter notebook.
On a newly open Jupyter notebook, you can use numpy and pandas (as np, pd) without importing them first.
print(np.pi) #3.141592...

Jupyter on Mac: run files from non-default directory

I run Jupyter Notebook on a mac and I am trying to edit a notebook file from a git repo I am contributing to. However I am having trouble accessing the file from the Notebook interface.
Is there a way to access notebooks which are not in the default path on mac (navigation to arbitrary file locations works fine on windows) without changing the default directory to my git repo and without copying the file to my default directory and back every time I edit it?
It looks like direct navigation outside the path is impossible (Based on this post) but it seems that it should be possible to start the notebook from the given directory using the command line.
Things I've tried:
Directly typing the relative and absolute path into the web interface with respect to the tree (i.e. http://localhost:8889/tree/../../[path-to-file]
Renaming the file with the path as a prefix
Starting jupyter from the directory containing the notebook
... with no success as of yet.
Any help on this would be greatly appreciated.
it seems that it should be possible to start the notebook from the given directory using the command line
You can...
jupyter notebook /some/non-default/path
However, you can only reach sub-directories of that path with Jupyter
Starting jupyter from the directory containing the notebook
That should also work
If you python code needs to read files that are in parent directories, relative paths still resolve fine within the notebook kernel.

Categories