jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.79k stars 5k forks source link

Set notebook working directory on server level #7525

Open ylieder opened 1 day ago

ylieder commented 1 day ago

Problem

Usually in my Python projects I follow the following project structure:

- project root dir
   - app (Python module/package)
   - notebooks 
      - subdir
        - notebook1.ipynb
      - notbook2.ipynb 
   - data

So my notebooks are in several hierarchy levels in a notebooks folder. When I now start a Jupyter notebook server from the root folder, I would like to be able to specify that the working directory of the notebook is the root folder, such that I have direct access to e.g. data and app, independently of the running notebook and its location in the file hierarchy.

There is the option jupyter notebook --root-dir, but this does not change the actual working directories of the notebooks, just the default entry path of the server app.

Proposed Solution

Adding command line arguments for jupyter notebook to set the working directory for each notebook, wherever it is located. E.g.:

jupyter notebook --exec_dir / --working_dir .
# or
jupyter notebook --exec_dir / --working_dir <absolute path>

Additional context

VSCode Jupyter extension offers an option archiving exactly the behaviour:

{
  "jupyter.notebookFileRoot": "${workspaceFolder}"
}

The feature is searched for at several Stackoverflow threads, e.g.: https://stackoverflow.com/questions/67287424/how-to-change-jupyter-notebooks-working-directory-configuration https://stackoverflow.com/questions/15680463/change-ipython-jupyter-notebook-working-directory#comment103251523_15682400

krassowski commented 1 day ago

Also requested in https://github.com/jupyterlab/jupyterlab/issues/11619 (see some workarounds in the comments).

Really, what most users are after is modifying PYTHONPATH variable for given project. One can set it in kernelspec .json file using an environment variable but this is just not convenient.

More generally, the PYTHONPATH comes from the (working) directory in which kernels are starting up. I think that this option should be named --kernel-startup-dir or similar. Thoughts?

ylieder commented 1 day ago

Oh, didn't see that there is already an issue for it and also didn't found the StackOverflow thread. Thank you for reference! I use similar workarounds in my projects, but IMHO much less convenient than the suggested feature.