How to init content of notebooks / working directory in Jupyterhub/Jupyterlab?

回眸只為那壹抹淺笑 提交于 2020-11-29 03:51:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!