Jupyterhub: How should notebooks be shared/used in Jupyterhub

Created on 28 May 2018  路  4Comments  路  Source: jupyterhub/jupyterhub

I have created Jupyterhub using LocalAzureAdOAuthenticator authenticator. Now i have do the following:
1) Keep a set of Jupyter notebooks in common location in the jupyterhub server
2) All the users logging in the server should have their own copy of notebooks. Since these notebooks generate image files as output, if all the users run the notebooks from same location, the image files get overwritten.
Could you please suggest the approach that can be followed to resolve this issues?

Most helpful comment

@sarath145p I am using DockerSpawner and using pre_spawn_hook to

  1. Create a directory for any new user(in case it odesn't exist) in `/home/user1/jupyterhub_volumes

  2. Copy jupyter notebook tutorials from /home/user1/jupyterhub_volumes/jupyter_notebook_tutorials to the newly created user directory.

import os, shutil
def copyDirectory(src, dest):
        try:
            shutil.copytree(src, dest)
        # Directories are the same
        except shutil.Error as e:
            print('Directory not copied. Error: %s' % e)
        # Any error saying that the directory doesn't exist
        except OSError as e:
            print('Directory not copied. Error: %s' % e)


def create_dir_hook(spawner):
    username = spawner.user.name # get the username
    volume_path = os.path.join('/home/user1/jupyterhub_volumes', username)
    if not os.path.exists(volume_path):
        # create a directory with umask 0777 
        # hub and container user must have the same UID to be writeable
        # still readable by other users on the system
        os.mkdir(volume_path, 0o777)
        # Copy tutorials to user volume mount
        tutorials_src = os.path.join('/home/user1/jupyterhub_volumes', 'jupyter_notebook_tutorials')
        tutorials_dest=os.path.join(volume_path, 'jupyter_notebook_tutorials')
        copyDirectory(tutorials_src, tutorials_dest)

# attach the hook function to the spawner
c.Spawner.pre_spawn_hook = create_dir_hook

All 4 comments

How in-sync do you want them to be? One option is to pre-load the users' home directories from a central location (e.g. from /etc/skel or via a git repo). This can be done via the pre_spawn_start hook.

Actually this is how i want it to work:
My notebooks are currently in a directory say: /home/user1/notebooks or /etc/skel
Whenever any other user logins to Jupyterhub, the notebooks in above directory should be pre-loaded to the current user's home directories and he should be able to run the notebooks. We would not be updating the main notebooks very often, so it doesn't have to be in-sync always. But whenever we update the main notebooks, it should reflect in the user's home directories with the changes.

Will 'pre_spawn_start' work for this case? Can you also suggest how to implement the same in config file?

@sarath145p I am using DockerSpawner and using pre_spawn_hook to

  1. Create a directory for any new user(in case it odesn't exist) in `/home/user1/jupyterhub_volumes

  2. Copy jupyter notebook tutorials from /home/user1/jupyterhub_volumes/jupyter_notebook_tutorials to the newly created user directory.

import os, shutil
def copyDirectory(src, dest):
        try:
            shutil.copytree(src, dest)
        # Directories are the same
        except shutil.Error as e:
            print('Directory not copied. Error: %s' % e)
        # Any error saying that the directory doesn't exist
        except OSError as e:
            print('Directory not copied. Error: %s' % e)


def create_dir_hook(spawner):
    username = spawner.user.name # get the username
    volume_path = os.path.join('/home/user1/jupyterhub_volumes', username)
    if not os.path.exists(volume_path):
        # create a directory with umask 0777 
        # hub and container user must have the same UID to be writeable
        # still readable by other users on the system
        os.mkdir(volume_path, 0o777)
        # Copy tutorials to user volume mount
        tutorials_src = os.path.join('/home/user1/jupyterhub_volumes', 'jupyter_notebook_tutorials')
        tutorials_dest=os.path.join(volume_path, 'jupyter_notebook_tutorials')
        copyDirectory(tutorials_src, tutorials_dest)

# attach the hook function to the spawner
c.Spawner.pre_spawn_hook = create_dir_hook

Thanks so much for your response @minrk and @sangramga , now Jupyterhub works fine!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuvipanda picture yuvipanda  路  3Comments

jan-janssen picture jan-janssen  路  4Comments

consideRatio picture consideRatio  路  3Comments

Spritekin picture Spritekin  路  3Comments

barrachri picture barrachri  路  3Comments