[PYTHON] Define custom actions in JupyterHub (hook function)

You can use JupyterHub's hook function to automatically create a directory for each user when you log in.

Define it as a function in the configuration file (jupyterhub_config.py). There are the following types of hooks.

Sample scripts are available in the official repositories.

jupyterhub/examples/bootstrap-script at master ยท jupyterhub/jupyterhub

For example, the hook action that automatically creates a user folder is as follows.

# in jupyterhub_config.py  
import os
def create_dir_hook(spawner):
    username = spawner.user.name # get the username
    volume_path = os.path.join('/volumes/jupyterhub', username)
    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.mkdir(volume_path, 0o755)
        # now do whatever you think your user needs
        # ...
        pass

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

Recommended Posts

Define custom actions in JupyterHub (hook function)
Duality in function
[Road to Python Intermediate] Define __getattr__ function in class
Generator function in JavaScript
Custom sort in Python3