Is there a way to specify a jupyter lab extension in a conda env?

Viewed 1247

Is it possible to specify a jupyter labextension (for example, @jupyter-widgets/jupyterlab-manager) as a dependency for a project managed via conda with an environment.yml file? Or, is there a workaround to ensure everyone on my team can run notebooks which rely on lab extensions out-of-the-box?

3 Answers

... since I just had a similar problem and there seems to be no straight-forward way of doing this directly via the yml-files ... here's how I solved it (using miniconda on Windows)

  1. create an ordinary conda yml-file my_env.yml
  2. create a .bat file that runs the necessary commands create_my_env.bat

the .bat-file looks something like this:

REM activate conda base environment
set root=%USERPROFILE%\AppData\Local\Continuum\miniconda3
call %root%\Scripts\activate.bat %root%
REM create environment
call conda env create -f my_env.yml
REM activate environment
call activate my_env
REM run additional commands
call jupyter labextension install @jupyter-widgets/jupyterlab-manager
call jupyter lab build
    
pause

I did the same thing as raphael, but in a .sh file for Ubuntu. Typically my projects have a setup script that initializes the virtual environment anyways, so I just add "jupyter labextension install" commands after that. I'd love to see these extensions included in yml files, somehow, some day. But I expect that will never be possible, labextensions are quite different from python packages, in my understanding.

conda env create -f environment.yml    # this line installs jupyter lab
conda activate env_name

jupyter labextension install @axlair/jupyterlab_vim
jupyter labextension install @jupyterlab/git
jupyter labextension install @jupyterlab/toc
conda install -c conda-forge jupyter_conda
jupyter labextension install jupyter_conda
jupyter lab build

It looks like this is addressed using prebuilt extensions in JupyterLab 3.0+

From the docs:

Because prebuilt extensions do not require a JupyterLab rebuild, they have a distinct advantage in multiuser systems where JuptyerLab is installed at the system level. On such systems, only the system administrator has permissions to rebuild JupyterLab and install source extensions. Since prebuilt extensions can be installed at the per-user level, the per-environment level, or the system level, each user can have their own separate set of prebuilt extensions that are loaded dynamically in their browser on top of the system-wide JupyterLab.

Example usage with the jupytext and jupyterlab-git extensions:

conda create --name myenv
conda activate myenv
conda install -c conda-forge python jupyterlab jupytext jupyterlab-git
conda deactivate
conda env export -n myenv -c conda-forge --from-history > environment.yml 
conda activate myenv
jupyter lab

In the Jupyter Lab tab that's opened, the jupytext and jupyterlab-git extensions should be active. This should also be the case for environments later built from the environment.yml file.

Related