Attached Read-only notebooks like volume to a docker container

Hi folks ,
So my usecase is that I am a teacher And I have hosted a jupyterhub on an ubuntu server so that my students can work on datascience projects. I am attaching volumes so that each user/student has different workspace. Now I want to pre install/download/attach some notebooks in my students workspace or attached a common read only volume for all containers (so that students can duplicate it and work on it) How do I do this here ?

I tried shared_volumes but I was not able to see those notebooks.
I would love to see something like this

host/path/to/notebooks/<all_notebooks.ipynb> binds-to /home/jovyn/work/sample-notebooks

My JupyterHub config file:

import dockerspawner
import os
#from IPython.utils.localinterfaces import public_ips
from jupyter_client.localinterfaces import public_ips
import subprocess

c = get_config()  # noqa

c.Authenticator.admin_users = {'ross'}

c.DockerSpawner.allowed_images = {
    'Python Base Notebook': 'quay.io/jupyter/base-notebook:latest',
    'Python Scipy Notebook': 'quay.io/jupyter/scipy-notebook:latest'
}



c.JupyterHub.spawner_class = dockerspawner.DockerSpawner


c.JupyterHub.hub_ip = public_ips()[0]




def create_dir_hook(spawner):
    username = spawner.user.name
    volume_path = os.path.join('/home', username) 
    volume_path = os.path.join(volume_path, 'jupyterhub_volume_data')
    print(volume_path)
    if not os.path.exists(volume_path):
        print("Path doesnt exist")
        os.makedirs(volume_path)
        
    
       

c.Spawner.pre_spawn_hook = create_dir_hook


notebook_dir = '/home/jovyan/work'
host_dir = '/home/{username}/jupyterhub_volume_data/'
print(notebook_dir)
print(host_dir)
c.DockerSpawner.notebook_dir = notebook_dir




# Mount the real user's Docker volume on the host to the notebook user's
# notebook directory in the container
c.DockerSpawner.volumes = { 
    f'{host_dir}': notebook_dir
        
    }



c.DockerSpawner.remove = True



c.DockerSpawner.extra_create_kwargs = {'user': 'root'}
# c.DockerSpawner.extra_host_config = {'runtime': 'nvidia'}  
#https://hackmd.io/@DanielChen/Sy81P-Aw4?type=viewe
c.DockerSpawner.environment = {
    'GRANT_SUDO': 'yes',
    'CHOWN_HOME': 'yes',
    'CHOWN_EXTRA': '/home/jovyan',
    'CHOWN_HOME_OPTS': '-R',
    'NB_UID': 1000,
    'NB_GID': 1000,
    }


@manics Any idea about how to go about this ?

If you have those notebooks in a git repo, you can use nbgitpuller which will pull the repo with notebooks when users launch JupyterLab servers.

2 Likes

@mahendrapaipuri Thank you for the suggestion. This looks really good.
What would happen if student clicks that link twice ? For the first time , it will clone but for the second time will it cause any issue ?

I havent used it myself. The link is a classic HTTP redirect and if the repo is already cloned, git will refuse to clone it again. So, it should be a no op when they click it second time

When you visit an nbgitpuller link, it ensures the directory is up to date. So first time it does git clone, second time does git pull with some default conflict resolution so the pull should always succeed without data loss.

2 Likes