jupyter-notebookjupyterjupyterhub

Access "tab" path of a Jupyter Notebook that is stored on S3


I have a running instance of JupyterHub that spawns containers for each user.

These containers have s3contents (https://github.com/danielfrg/s3contents) installed, which means notebooks are not actually stored in the file system they have, so they report the following when talking about its path:

enter image description here

The problem is that I need to retrieve the current path they actually have in the "virtual" path they show on the tab:

enter image description here

How could I get this information from within the notebook itself?

This is my jupyter_notebook_config.py content:

from s3contents import S3ContentsManager
from hybridcontents import HybridContentsManager
from jupyter_server.services.contents.largefilemanager import LargeFileManager
c = get_config()

c.NotebookApp.contents_manager_class = HybridContentsManager
c.NotebookApp.notebook_dir = "/home/jovyan/work"

c.HybridContentsManager.manager_classes = {
    # Associate the root directory with an S3ContentsManager.
    # This manager will receive all requests that don"t fall under any of the
    # other managers.
    "s3-shared": S3ContentsManager,
    # Associate /local_directory with a LargeFileManager.
    "": LargeFileManager,
}

c.HybridContentsManager.manager_kwargs = {
    # Args for root S3ContentsManager.
    "s3-shared": {
        "access_key_id": "{{ ceph.tokens.data.access_key }}",
        "secret_access_key": "{{ ceph.tokens.data.secret_key }}",
        "bucket": "{{ ceph.buckets.jupyter }}",
        "endpoint_url": "{{ ceph_ca_host_complete }}",
        "skip_tls_verify": True,
        "prefix": "",
    },
    # Args for the LargeFileManager mapped to anything else but s3 shared content
    "": {
        "root_dir": "/home/jovyan/work",
    },
}

Solution

  • Ref. Get current jupyter-lab notebook name [for Jupyter-lab version 2.1 and 3.0.1 and notebook version >6.0.3)

    #! pip install ipynbname
    
    import ipynbname
    nb_fname = ipynbname.name()
    nb_path = ipynbname.path()