2

I need the location of jupyter's local directory to use in another script. For example, when starting jupyter, the console prints:

> Serving notebooks from local directory: C:\Users\PC\Documents

Somehow, jupyter has set this path as the default local location from which my notebooks are served. It is the "root" of my jupyter environment. How do I find this default setting? Is there a pre-existing file I can parse, or a command I can type?


Among other attempts, I've tried the following:

> jupyter notebook --help-all
--NotebookApp.notebook_dir=<Unicode>
    Default: ''
    The directory to use for notebooks and kernels.

Note: I do not have a config file, nor do I not wish to generate a config file to get this information. For clarity, I am not concerned with changing the directory.

pylang
  • 40,867
  • 14
  • 129
  • 121
  • How are you starting Jupyter? Is the path `C:\Users\PC\Documents` constant or does it differ? – Jacob Budin Oct 16 '17 at 18:19
  • @JacobBudin I start jupyter using Anaconda's JupyterNotebook link, thus yes it is always a reproducible start. However, it appears to run `jupyter-notebook-script.py` which calls `notebook.notebookapp`. There may be code there for parsing the local directory. – pylang Oct 16 '17 at 21:19

1 Answers1

4

From https://stackoverflow.com/a/37690452, it seems that the default location is set/derived in the cwp.py file in the Anaconda folder. For default installs on Windows, it seems that this script would be at (for username PC, as in your question) C:\Users\PC\AppData\Local\Continuum\Anaconda3\cwp.py.

Finding the source of cwp.py on github.com/ContinuumIO/menuinst, it seems that it's basically using the current Windows User's Documents folder.

edit:

I reproduce below the gist of my post on the github issue at notebook/issues/2941#issuecomment-337076059 as requested in comments:


Unless specified otherwise in config or environment variables, the notebook server will use the current working directory at start time as the notebook directory. Yours seems to be set to the documents folder because the Anaconda shortcut which you're using to start the notebook is doing a little magic to set the working directory before starting the notebook server. For you purposes, you could:

  1. Explicitly set the notebook directory in a config file or environment variable
  2. Assume the notebook root is at

    os.path.expanduser('~/Documents')
    

    (note this may not work require adjustment if OS language is not English)

  3. Use the pip/conda package menuinst to find the folder, replicating the same logic as the anaconda shortcut exactly - something along the lines of:

    from menuinst.windows.knownfolders import FOLDERID, get_folder_path
    
    documents_folder, exception = get_folder_path(FOLDERID.Documents)
    if exception:
        documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
    if exception:
        print('bang! exception! do some error handling')
    else:
        print(documents_folder)
    

    (note this will probably fail on non-windows systems)

jcb91
  • 639
  • 4
  • 8
  • Many thanks. I appreciate the third suggestion from this post: https://github.com/jupyter/notebook/issues/2941#issuecomment-337076059. Would you include this link. I'd be happy to accept this answer. – pylang Oct 28 '17 at 04:21
  • @pylang No problem. I've edited the answer to include both a link the github issue, and a transcription of the content, just in case SO outlives GitHub (!) – jcb91 Oct 28 '17 at 11:59
  • FYI: I don't recommend menuinst anymore because it lagged in maintenance and updates on pypi, but at the time, this was a good answer. – pylang Aug 13 '19 at 19:02