JupyterHub (TLJH) admin how to add notebooks to ALL new users? Alternative to nbgitpuller

75 views
Skip to first unread message

Valdis S Coding

unread,
Apr 28, 2020, 6:15:03 AM4/28/20
to Project Jupyter
How would one add a common set(folder) of notebooks to be used and  by all new users upon first login?

The idea is that there would be some shared folder containing notebooks for the admin.  


Contents of this folder would be copied(not linked) upon creation of a new user account.


Ideally there would two folders for all new users:

One shared (with possibly some notebooks,datasets being write protected)

This is possible:

My question is about 
second folder which  would be notebooks which would be individual to the user and they could modify at will.

Currently the only way to get new users up to speed is to provide a nbgitpuller link.

This is not ideal when people are starting their new accounts asynchronously.


As a last resort, I am looking into writing a little script to just copy something to individual users.

That is automating something like this: sudo cp /home/jupyter-hubadmin/MyNotebook.ipynb /home/jupyter-newuseraccount_foo
The above works but seems very hacky..

Any ideas on what to try ?

B reddy

unread,
Jun 30, 2020, 2:48:24 AM6/30/20
to Project Jupyter
You can alternatively add the below piece of code in you jupyterhub_config.py file so it can add the new directory for each logged in user asynchronously .

def create_dir_hook(spawner):
 
     spawned_user = spawner.user.name # get the spawned user
     volume_path = os.path.join('/home/jupyter-hubadmin', spawned_user)
    uid = pwd.getpwnam('jupyter').pw_uid
    spawner.port = random_port()
    if not os.path.exists(volume_path):
        # create a directory with umask 0755 
        # hub and container user must have the same UID to be writeable
        # still readable by other users on the system
        os.makedirs(volume_path, 0o755)
        #subprocess.Popen ("git init" , shell=True , cwd=volume_path).communicate()
        pass
    # the user folder should be owner by the user configured in the docker container
    # if not, the end user will be not able to create any notebook
    os.chown(volume_path, uid, uid)


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

Jonathan Gutow

unread,
Jun 30, 2020, 7:59:11 AM6/30/20
to Project Jupyter
I found I wanted something more flexible, where I could have published (not editable documents) and shared documents as well as the ability to define multiple groups on my hub. I created a couple of simple scripts to do this. I think they could be called from a web page in the Jupyter Hub administration pages. However, I am not in a position to spend the time writing the necessary code/templates. Anyway, in case this is useful my scripts are available on github(https://github.com/gutow/tljh_grp_utils).

Regards,
Jonathan
Reply all
Reply to author
Forward
0 new messages