(image_jupyter)= # Integrating JupyterLab The section describes how JupyterLab has been integrated in to the virtual machine image at the ILL. There are many different ways of installing JupyterLab, however a certain amount of configuration has to conform to VISA requirements to ensure that the link to VISA Jupyter Proxy behaves as expected. ## Installation using a python virtual environment We use pip to install a specific version of JupyterLab and *labextensions*. We install JuypyterLab under the root directory of `/opt/visa/jupyter`. The following script describes the process: ```bash #!/bin/bash echo "Installing JupyterLab..." JUPYTER_DIR=/opt/visa/jupyter mkdir -p $JUPYTER_DIR cd $JUPYTER_DIR || exit python3 -m venv jupyterlab source jupyterlab/bin/activate pip install --upgrade pip pip install jupyterlab==2.2.9 pip install ipywidgets==7.5.1 # enable interactive matplotlib jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter labextension install jupyter-matplotlib@0.7.4 jupyter nbextension enable --py widgetsnbextension echo "Finished installing JupyterLab" ``` ## Starting the server at boot There are certain requirements for JupyterLab to be correctly integrated into VISA: - The Jupyter server needs to start automatically during the boot process This ensures that JupyterLab is available for connection automatically via VISA without starting it manually - Jupyter must run as the same user as the owner of the instance The VISA user will therefore have access to their home directory and notebook files as they expect - The *base_url* of the server must be `/jupyter/{INSTANCE_ID}` The VISA Jupyter Proxy forwards requests on a specific URL which must match that of the Juptyer server - The port of the Jupyter Server must be identical to the [configuration of VISA Jupyter Proxy](deployment_environment_variables_jupyter_proxy_port) VISA Jupyter Proxy forwards HTTP requests to the Jupyter Server When creating the instance, VISA API Server uses [cloud-init](https://cloudinit.readthedocs.io/en/latest/index.html) to pass meta data to the instance including `id` (the ID of the instance) and `owner` (the username of the owner). The script below uses these elements to start JupyterLab in the required way (feel free to modify this script accordingly, but keep in mind that changing the *root_url* will break the link to VISA and changing the user will modify the behaviour of Jupyter). ```bash #!/bin/bash JUPYTER_ENV=/opt/visa/jupyter/jupyterlab # Get owner and instance_id metadata OWNER=`cloud-init query ds.meta_data.meta.owner` INSTANCE_ID=`cloud-init query ds.meta_data.meta.id` # The jupyter configuration file (you may want a different conf depending on dev or prod environments) JUPYTER_CONF=/path/to/jupyter-conf.py # Verify that we get the data from cloud-init if [ -z "$OWNER" ]; then echo "Failed to get OWNER from OpenStack instance metadata" exit 1 else echo "Got owner \"$OWNER\" from OpenStack instance metadata" fi if [ -z "$INSTANCE_ID" ]; then echo "Failed to get INSTANCE_ID from OpenStack instance metadata" exit 1 else echo "Got instance ID $INSTANCE_ID from OpenStack instance metadata" fi # Defined the base url of Jupyter (as required by VISA Jupyter Proxy) BASE_URL=/jupyter/$INSTANCE_ID # Check that the owner/login exists if ! id "$OWNER" &>/dev/null; then echo "Failed to run JupyerLab: User $OWNER not found" exit 1 fi # Run as the instance owner su - $OWNER < conda activate my_conda_env (my_conda_env) > conda install ipykernel (my_conda_env) > python -m ipykernel install --user --name=my_conda_env ``` More details of this can be found on the [VISA User documentation](https://visa.ill.fr/help/data-analysis/conda) at the ILL.