问题
If I create a new user in JupyterHub I want that the working directory of the corresponding JupyterLab instance is initialized with some getting started examples:
I already installed the git extension for Jupyterlab. Is there a way to automatically clone a git repository for new users?
Here is the doc on Spawners: https://jupyterhub.readthedocs.io/en/stable/reference/spawners.html
I could find a hint on workspace initialization.
回答1:
The Spawner provides some hook functions in the configuration file jupyterhub_config.py
. And its possible to get the current user name from within the hook function.
import subprocess
def git(*args):
return subprocess.check_call(['git'] + list(args))
def init_working_directory(spawner):
username = spawner.user.name
git_source = 'https://$user:$password@gitlab.server.de/my/project'
target_folder = '/home/' + username + '/GettingStarted'
git('clone', git_source, target_folder)
c.Spawner.pre_spawn_hook = init_working_directory
There are a few issues left:
a) The git clone command only works the first time, when the folder /home/username/GettingStarted does not yet exist.
b) There is no progress bar shown during the delayed log and the git clone command takes a while.
c) Git password might be shown in error messages/console.
Therefore, I will initially do the git clone when creating my Docker container and only perform a local copy in pre_spawn_hook if the GettingStarted folder does not yet exist.
来源:https://stackoverflow.com/questions/64485967/how-to-init-content-of-notebooks-working-directory-in-jupyterhub-jupyterlab