Using JupyterHub in HDC
JupyterHub is an open-source, multi-user version of Jupyter Notebook for performing analysis of Project files in the Core. More information can be found in the application documentation https://jupyter.org/.
How it Works
JupyterHub allows Project members to create or import Jupyter Notebooks into the Project Workspace environment, retrieve Project files from the Core, perform computational workflows on the data, and write the outputs back to the Core where they can be accessed by other Project members. JupyterHub spins up a new JupyterLab instance for each Project member.
Prerequisites
- Project Collaborator role or higher.
- JupyterHub has been configured for the Project by the Platform Administrator. See Getting Access to JupyterHub.
Data Stewardship
Users are reminded to abide by the Platform Terms of Use and any Project-specific restrictions when using Workspace tools to access data and code.
Getting Access to JupyterHub
JupyterHub is configured at the time of Project Setup. If you launch JupyterHub and receive a notice that it hasn’t been deployed for your project, please contact your Platform Administrator.
Launching JupyterHub
- Launch your Project and click the JupyterHub icon in the left menu bar.
- Click Sign in with Keycloak to initiate your session. JupyterHub automatically authenticates with your existing username and password and launches your session - no additional sign-in is required.
- You can chose to either start a Minimal environment, which comes with Python, or a Datascience environment, which also includes R and Julia in addition to Python.
- From the JupyterHub home page (a JupyterLab interface) you can now perform various actions such as creating and working on Jupyter Notebooks, importing existing ones, and using the Pilot Command Line Interface in the terminal to retrieve, analyze, and re-upload Project Core data, and create. Moreover, you can also use the pre-deployed and configured package management software conda to download, install, and manage for instance Python packages as per individual demand (see the sections Installing New Python Packages and Creating a Virtual Python Environment and Registering a Kernel below for more details).
- When finished using JupyterHub, click Logout to end your session.
Creating a Notebook
Users can create a new Jupyter Notebook with Python 3 inside JupyterHub, with dedicated and persistent storage under the users' Home Directory.
- In the Launcher, click the Python 3 Notebook icon, or click File > New > Notebook.
- Create your Notebook.
Launching the Terminal
JupyterHub provides browser-based terminal access for advanced users to run commands directly in the system shell. Importantly, this allows users to sync data between for instance the Projects Core and their JupyterHub home directory using pilotcli, or to download and manage Python packages.
- In the Launcher, click the Terminal icon, or click File > New > Terminal.
- The terminal window opens.
Ubuntu is used to host Jupyter Notebook. Use the command cat /etc/os-release to determine to current version of Ubuntu:
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
Creating a Python Virtual Environment and Registering a Kernel
The user has full flexibility to use different virtual environment and/or package management systems. Please find the examples of using conda or Pythons in-built venv options described below. Importantly, in either case, the user has to register the new environment as a kernel using ipykernel, to make is accessible via the Jupyter Notebooks (see Registering the new Virtual Environment as Kernel for more details).
Using conda
The package management software conda by Anaconda has become one of the most popular package management systems, especially for Data and Life Sciences. Therefore, conda is already pre-deployed and configured in each user’s JupyterHub. Please find the full documentation of conda here, and the corresponding documentation of how to manage virtual environments using conda here. The following steps provide a short example of how you can use conda to create a new virtual environment using the JupyterHub terminal within the Platform.
At first, you need to activate conda. Since it is already pre-deployed and configured for you, all you need to do is launch a terminal within JupyterHub (see Launching the Terminal above) and execute the command source activate. This will activate conda and you can see the success of this by the indication of the currently activated conda environment at the beginning of the line, displayed in parentheses - usually “base”:
(base) username@jupyter-username:~$
To create a new environment, run the following commands in the terminal after activating conda:
Replace your_env_name with your preferred name for the environment. When being prompted by conda to confirm the creation of the environment at the specified location (per default in the users home directory - please do not change this location, to ensure persistency of your created environment), proceed with the creation by typing “y”, or abort the process by typing “N”. Once confirmed, conda will complete the environment creation process and remind you to activate the environment:
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/username/.conda_envs/sample_env
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate sample_env
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) username@jupyter-username:~$
Please note, at the end of the environment creation process, you will still remain in the previously activate environment (“base”, in this example). Therefore, please remember to activate the novel environment before installing any packages by running the command conda activate your_env_name and replace “your_env_name” with the corresponding name you chose (“sample_env” in this example):
(/home/username/.conda_envs/sample_env) username@jupyter-username:~$
You can now install the desired packages in this new conda environment, for instance using the conda install command. For example, in order to install the latest version of Python, run:
To see a list of all installed packages in the currently activated environment (indicated in parentheses at the beginning of the line, “base” in this case), run:
To see a list of all existing conda environments, run:
Please find many more examples and the full documentation of how to manage conda environments here. Importantly, please remember to follow the instructions in the Registering the new Virtual Environment as Kernel section below, to make the virtual environment accessible via the Jupyter Notebooks.
Using venv
As an alternative to using conda, you can also use the Python native package venv. Please find the full documentation of venv here, and a short example of how to create a new virtual environment using venv below:
username@jupyter-username:~$ source your_env_name/bin/activate
Registering the new Virtual Environment as Kernel
In order to make the newly created virtual environment accessible for the Jupyter Notebooks, you have to register it using ipykernel. Importantly, please make sure that the corresponding environment is currently active before running the following commands:
Please replace your_env_name with the name of your newly created environment. Depending on which package and/or virtual environment management system you chose to use, you may have to install ipykernel in the newly created environment first. Remember to activate the newly created environment and then run one of the following commands to install ipykernel, depending on your package management system of choice:
or:
Once you have installed ipykernel, re-run the command above to register your novel environment via ipykernel.
Example usage:
Installed kernelspec sample_env in /home/username/.local/share/jupyter/kernels/sample_env
(/home/username/.conda_envs/sample_env) username@jupyter-username:~$
Afterwards, the environment will be listed when you open the Launcher to open a new Jupyter Notebook: