jupyterhub 和 conda 环境

标签 jupyter ipywidgets jupyterhub

我对 conda 环境和 jupyterhub 之间的关系有点困惑。

正如 jupyterhub 文档所说,它可以从 conda 安装。因此可以使用一些 conda 环境(例如环境“root”)并从中执行“conda install jupyterhub”。

在相同的环境中将“居住”jupyter。在此环境中安装 nb_conda 可以在笔记本中选择内核和其他 conda 环境。

我的问题是关于 nbextensions 和 ipywidgets 等软件。他们应该在哪里?在与jupyterhub相同的环境中还是在与新笔记本相对应的环境中?

最佳答案

conda 和 jupyter 之间的关系可能会令人困惑。将 conda 视为您的环境,将 jupyter 视为任何其他包。您可以用来启动流程然后提供服务的包。

要回答您的问题,它们应该安装在您的 conda 环境中。不幸的是,情况比这要复杂一些。这些扩展将可供所有用户使用。我还没有亲自测试过在不同环境中拥有更多扩展的单个用户(如果可能的话,但如果我这样做的话会更新答案)。

If it helps, this is what the docs have to say for the matter:

To install the jupyter_contrib_nbextensions notebook extensions, three steps are required. First, the Python pip package needs to be installed. Then, the notebook extensions themselves need to be copied to the Jupyter data directory. Finally, the installed notebook extensions can be enabled, either by using built-in Jupyter commands, or more conveniently by using the jupyter_nbextensions_configurator server extension, which is installed as a dependency of this repo.

Assuming you installed the extensions via conda :

conda install -c conda-forge jupyter_contrib_nbextensions

然后使用了--sys-prefix,这很好。来自文档:

--sys-prefix to install into python's sys.prefix, useful for instance in virtual environments, such as with conda.

因此,要添加扩展,过程应如下所示:

$ sudo su -
$ pip install fileupload
$ jupyter nbextension install --sys-prefix --py fileupload
$ jupyter nbextension enable fileupload --py --sys-prefix

由于标题询问的是 conda 环境,因此我也会对此进行一些讨论。我已经在 Ubuntu 18.04LTS 上测试了这些方法。

很多时候,您会希望允许用户共享用户创建的环境,而永远无法访问 root 权限。我见过你有两个很好的选择(如果你知道其他方法,请评论):1)共享环境2)从需求文件复制环境。不要忘记您还必须将环境添加为内核。

方法 1 - 共享环境

在共享位置创建一个环境,然后让两个用户将其添加为内核。

conda create -p /home/envs/test --clone root

可以clone root来复制root环境,或者base来复制基础环境。/home/envs/test 将在“envs”目录中创建一个“测试”环境。确保 envs 拥有将使用这些文件的用户的所有必要权限。

从那里作为另一个用户,只需将环境添加为内核即可。

$ sudo su - <user-to-install-kernel-to>
$ conda activate <test>
$ python -m ipykernel install --user --name test \
         --display-name "Python (test)"

注意..我相信我必须手动更新内核规范才能使其指向正确的 python 环境🤦‍♂️

方法2

或者,只需创建环境的副本

$ conda env export --name test > environment.yml
$ sudo su - customer
$ conda env create --name cust-env-copy --file environment.yml
$ python -m ipykernel install --user --name cust-env-copy \
         --display-name "Python (test)"

关于jupyterhub 和 conda 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301865/

相关文章:

jupyter-notebook - ipywidgets:更改小部件值后自动更新变量并运行代码

ipython - 如何使 jupyter notebook 中的小部件可滚动?

java - dockerized java应用程序访问已通过docker部署在jupyterhub上的monetdb数据库

macos - 现有 jupyter 安装中的 Julia

pip - 更新 conda 后 conda 环境损坏

python - 自动调整 IPython 小部件布局

docker - 使用多个具有相同镜像的容器进行Docker崩溃测试

ruby - iRuby 笔记本无法在 ubuntu 上加载 rbczmq

jupyter-notebook - Jupyter notebook 显示问题(隐藏标题和 maintoolbar 后)

python - Jupyterhub-错误 :asyncio:Task exception was never retrieved