【问题标题】:How to init content of notebooks / working directory in Jupyterhub/Jupyterlab?如何在 Jupyterhub/Jupyterlab 中初始化笔记本/工作目录的内容?
【发布时间】:2020-10-23 12:36:11
【问题描述】:

如果我在 JupyterHub 中创建一个新用户,我希望使用一些入门示例初始化相应 JupyterLab 实例的工作目录:

我已经为 Jupyterlab 安装了 git 扩展。有没有办法为新用户自动克隆一个 git 存储库?

这里是 Spawners 的文档: https://jupyterhub.readthedocs.io/en/stable/reference/spawners.html

我可以找到有关工作区初始化的提示。

【问题讨论】:

    标签: jupyter-notebook jupyter-lab jupyterhub


    【解决方案1】:

    Spawner 在配置文件jupyterhub_config.py 中提供了一些钩子函数。并且可以从钩子函数中获取当前用户名。

    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
    

    还有几个问题:

    a) git clone 命令只在第一次工作,当文件夹 /home/用户名/GettingStarted 还不存在。

    b) 延迟日志期间没有显示进度条,git clone 命令需要一段时间。

    c) Git 密码可能会显示在错误消息/控制台中。

    因此,我将在创建 Docker 容器时首先执行 git clone,并且如果 GettingStarted 文件夹尚不存在,则仅在 pre_spawn_hook 中执行本地复制。

    【讨论】:

      猜你喜欢
      • 2014-02-04
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2013-03-18
      相关资源
      最近更新 更多