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?
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
Create a directory for any new user(in case it odesn't exist) in `/home/user1/jupyterhub_volumes
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!
Most helpful comment
@sarath145p I am using DockerSpawner and using
pre_spawn_hooktoCreate a directory for any new user(in case it odesn't exist) in `/home/user1/jupyterhub_volumes
Copy jupyter notebook tutorials from
/home/user1/jupyterhub_volumes/jupyter_notebook_tutorialsto the newly created user directory.