Using JupyterHub in HDC

Last modified by Dennis Segebarth on 2024/10/02 18:14

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/.

1723798240534-407.png

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.

If you access JupyterHub of the HealthDataCloud Test Project, please be aware that the resources are limited for each user to: 2 GB of persistent storage volume, 4 GB memory, and a single CPU. These limitations can easily be adjusted for new Projects.

Launching JupyterHub

1723798257792-201.png

  1. Launch your Project and click the JupyterHub icon in the left menu bar.
  2. 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.
  3. 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.
  4. 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).
  5. 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.

  1. In the Launcher, click the Python 3 Notebook icon, or click File > New > Notebook.
  2. Create your Notebook.

1723798278604-114.png

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.

  1. In the Launcher, click the Terminal icon, or click File > New > Terminal.
  2. The terminal window opens.

1723798293872-992.png

Ubuntu is used to host Jupyter Notebook. Use the command cat /etc/os-release to determine to current version of Ubuntu:

uname@jupyter-uname:/etc$ cat os-release
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”:

username@jupyter-username:~$ source activate
(base) username@jupyter-username:~$ 

To create a new environment, run the following commands in the terminal after activating conda:

(base) username@jupyter-username:~$ conda create --name your_env_name

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:

(base) username@jupyter-username:~$ conda create --name sample_env
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):

(base) username@jupyter-username:~$ conda activate sample_env
(/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:

(/home/username/.conda_envs/sample_env) username@jupyter-username:~$ conda install python

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:

(base) username@jupyter-username:~$ conda list

To see a list of all existing conda environments, run:

(base) username@jupyter-username:~$ conda info --envs

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:~$ python3 -m venv your_env_name
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:

username@jupyter-username:~$ python -m ipykernel install --user --name=your_env_name

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:

(your_env_name) username@jupyter-username:~$ conda install -c anaconda ipykernel

or:

username@jupyter-username:~$ pip install ipykernel

Once you have installed ipykernel, re-run the command above to register your novel environment via ipykernel.

Example usage:

(/home/username/.conda_envs/sample_env) username@jupyter-username:~$ python -m ipykernel install --user --name=sample_env
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:

1723798325144-485.png

and also from each opened Notebook, e.g., via Kernel > Change Kernel… :

1723798338447-469.png

Installing New Python Packages

We highly recommend the use of virtual environments when installing new packages (see Creating a Python Virtual Environment and Registering a Kernel above for more details). Consequently, we recommend installing new packages via commands in the JupyterHub terminal in the corresponding virtual environments, instead of installing packages from within Jupyter Notebooks.

Depending on the IT policies, outbound traffic may need to go through a proxy. If so, users will be required to provide the proxy command line argument such as pip, curl, wget, etc.

For example:

pip install my_package

If you are using conda to manage python packages:

conda install my_package

The above information is provided as examples only. Please refer to documentation provided by your IT department with respect to proxy configuration.

Using the Pilot Command Line Interface in a JupyterHub Terminal

The Pilot Command Line Interface (CLI) is deployed within JupyterHub as extension resource. Project members can use the Pilot Command Line Interface in a JupyterHub terminal to download Project data from the Core for further analysis, and upload the derivative outputs back to the Green Room or Core.

The Home Directory is your default directory. When you download a copy of your Core files to JupyterHub, the files persists in the JupyterHub environment until deleted by you, so you can return to the session and continue your work at a later time without the need to retrieve the data from the Core again.

The following sections focus on getting started with basic pilotcli commands in JupyterHub. For additional pilotcli commands and usage, see the article Working with HDC Project Files in the Command Line Interface.

Launching Pilot Command Line Interface

  1. Launch your Project and click the JupyterHub icon in in the workspace icon group.
  2. Click the Terminal launcher icon to open the Terminal.
  3. In the Jupyterhub Terminal, type pilotcli to launch the latest version of the Pilot Command Line Interface.
  4. Use the pilotcli --help
    at any time to show the welcome message again.
collaborator4@jupyter-collaborator4:~$ pilotcli
Usage: pilotcli [OPTIONS] COMMAND [ARGS]...

  What's new (Version 2.2.0):

   1. CLI supports to perform multi-threading upload for file/folders

   2. CLI supports to perform resumable upload for single file

 

Options:
  --help  Show this message and exit.

Commands:
  container_registry  Container Registry Actions.
  dataset             Dataset Actions.
  file                File Actions.
  project             Project Actions.
  use_config          Config Actions.
  user                User Actions.

Logging into the Pilot Command Line Interface

Users are required to login with platform credentials before performing any tasks through Pilot Command Line Interface.

  • Use the command pilotcli user login to log into the Pilot Command Line Interface.
collaborator4@jupyter-collaborator4:~$ pilotcli user login
Please, access https://iam.staging.pilot.indocresearch.com/realms/pilot/device?user_code=XXXX-XXXX to proceed
 ▄▄▄▄▄▄▄  ▄ ▄▄     ▄ ▄▄▄▄    ▄ ▄▄▄▄▄▄▄
 █ ▄▄▄ █ ▄ ▄███   ▀▀  █▀  ▀██▄ █ ▄▄▄ █
 █ ▄ ▀ ▄ ▀▄ ▀▀ ▄█▀▄▀ ▀▀▄█▄▄▀ █████▄▄▀▄
 ▄▄▄▄▄▄▄ ▀ ▀█▄ ▀▄ ██▀█ ▄▀▄▄  █ ▄ █▀▄▄▄
 █ ▄▄▄ █ █▀█▄▀ █▀   █▀▀█ ▀▄█▄█▄▄▄█▀▄█  
 █ ███ █ █▀██▀▄ █▀▄▄▀▀█▄▀▀█▄▀█ ▀ ▀▄▀██
 █▄▄▄▄▄█ ▄▀▄▄██▄▄▀▄  ▀▀▄  ▄▄▀▀▀▄ █▄▄▄█

 Waiting validation finish...                                      
  • You’ll be asked to validate your HDC user account using one of the provided methods.

    • Copy and paste the provided validation link into a new browser tab or
    • Scan the QR code with your mobile device.
  • Open the login window and enter your HDC username and password (i.e. your EBRAINS account credentials).
  • Grant access by clicking Yes.

1723798355215-434.png

1723798365454-527.png

  • After successful confirmation, return to the terminal in your JupyterHub browser tab.
Welcome to the Command Line Tool!
  • You’re now ready to start using the Pilot Command Line Interface to work with your Project data in JupyterHub.

Zone Restrictions when using Pilot Command Line Interface in JupyterHub

When using the Pilot Command Line Interface in JupyterHub and the following actions are possible on the derivative files generated in JupyterHub:

File Operation

Permitted in the 
Green Room 

Permitted in the 
Core 

File upload 
(upload derivative output files from JupyterHub to the Green Room or Core storage)

Yes

Yes

File download
(download files from Green Room or Core into JupyterHub)

No

Yes

Downloading Project Data to JupyterHub using the Pilot Command Line Interface

After logging into the Pilot Command Line Interface, you can download data from the Project Core into the JupyterHub environment to start your data analyses.

File related commands are grouped in the file category. To view the full list of commands in this category, type pilotcli file --help
. To download project data, use the file sync command. To view the full list of commands in this category, type pilotcli file sync --help
.

collaborator4@jupyter-collaborator4:~$ pilotcli file sync --help
Usage: pilotcli file sync [OPTIONS] [PATHS]... OUTPUT_PATH

  Download files/folders from a given Project/folder/file in core zone.

Options:
  -z, --zone TEXT  Target Zone (i.e., core/greenroom)
  --zip            Download files as a zip.
  -i, --geid       Enable downloading by geid.
  --help           Show this message and exit.

Example

Downloading a file from the Core to your Home Directory:

Reminder: Please follow Linux conventions for file management. If your filename contains spaces, wrap it in single or double quotes.

  • Filename: “Chemical Tracking Data.csv”
  • Source: Project “Indoc Test Project”, “Core” storage zone, folder “collaborator4” indoctestproject/collaborator4/Chemical Tracking Data.csv -z core
  • Destination: user's Home directory in the Guacamole or JupyterHub VM .
  • Command group/option: file sync
collaborator4@jupyter-collaborator4:~$ pilotcli file sync indoctestproject/collaborator4/'Chemical Tracking Data.csv' . -z core
start downloading...
Downloading Chemical Tracking Data.csv |██████████████████████████████ 100% 00:00
File has been downloaded successfully and saved to: ./Chemical Tracking Data.csv

To confirm successful download, type ls and verify the file "Chemical Tracking Data.csv" is stored in the Home folder.

collaborator4@jupyter-collaborator4:~$ ls
'Chemical Tracking Data.csv'   pilotcli

The file “Chemical Tracking Data.csv” can be viewed in the JupyterHub graphical user interface:

1723798383409-873.png

Uploading Project Data from JupyterHub using the Pilot Command Line Interface

After analyzing Project data inside the JupyterHub, you can upload the generated outputs back into the Project via the Pilot Command Line Interface. 

Example

  • Filename: Chemical Tracking Data rev.csv
  • Source: user's Home directory in JupyterHub .
  • Destination: Project “Indoc Test Project”, folder “collaborator4”, “Core” storage zone,
    indoctestproject/collaborator4 -z core
  • Command group/option: file upload
  • User message (for upload back to the Core): “my workbench output, no additional sensitive data"
  • Command: pilotcli file upload ./'Chemical Tracking Data rev.csv' -p indoctestproject/collaborator4 -z core -m "my workbench output, no additional sensitive data" 

When uploading data to the Core, you are reminded that you are bypassing the usual Green Room upload workflow. To confirm, type y at the prompt, or N to cancel.

collaborator4@jupyter-collaborator4:~$ pilotcli file upload ./'Chemical Tracking Data rev.csv' -p indoctestproject/collaborator4 -z core -m "my workbench output, no additional sensitive data"
You are about to transfer data directly to the PILOT Core! In accordance with the PILOT Terms of Use, please confirm that you have made your best efforts to
pseudonymize or anonymize the data and that you have the legal authority to transfer and make this data available for dissemination and use within the PILOT .If you
need to process the data to remove sensitive identifiers, please cancel this transfer and upload the data to the Green Room to perform these actions.
To cancel this transfer, enter [n/No]
To confirm and proceed with the data transfer, enter [y/Yes]
 [y/N]: y
Starting upload of: ./Chemical Tracking Data rev.csv
Pre-upload complete.
Uploading Chemical Tracking Data rev.csv:  |██████████████████████████████ 100% 00:00
Upload Time: 2.92s for 1 files
All uploading jobs have finished. 

After completing the upload, you can confirm the new file “Chemical Tracking Data rev.csv" exists in the correct directory using the pilotcli file list command and/or in the Portal File Explorer.

collaborator4@jupyter-collaborator4:~$ pilotcli file list indoctestproject/collaborator4 -z core
Chemical Tracking Data rev.csv  Chemical Tracking Data.csv    

1723798397694-530.png


Copyright © 2023-2024 Indoc Systems.

HealthDataCloud is powered by Pilot technology, a product of Indoc Systems.