Wiki source code of Using JupyterHub in HDC

Version 2.1 by Susan Evans on 2023/07/11 14:10

Show last authors
1 {{box cssClass="floatinginfobox" title="Table of Contents"}}
2 {{toc depth="2"/}}
3 {{/box}}
4
5
6 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/>>https://jupyter.org/]].
7
8 = How it Works =
9
10 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.
11
12 = Prerequisites =
13
14 * Project Collaborator role or higher.
15 * JupyterHub has been configured for the Project by the Platform Administrator. See //Getting Access to JupyterHub//.
16
17 = Data Stewardship =
18
19 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.
20
21 = Getting Access to JupyterHub =
22
23 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.
24
25 = Launching JupyterHub =
26
27 [[image:HDC Project Workspace tool navigation Jupyterhub v1.0.0 2023-05-25.png||height="10%" width="30%"]]
28
29 1. Launch your Project and click the **JupyterHub icon** in the left menu bar.
30 1. 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.
31 1. 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.
32 1. 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).
33 1. When finished using JupyterHub, click **Logout** to end your session.
34
35 = Creating a Notebook =
36
37 Users can create a new Jupyter Notebook with Python 3 inside JupyterHub, with dedicated and persistent storage under the users' Home Directory.
38
39 1. In the Launcher, click the **Python 3 Notebook **icon, or click **File > New > Notebook**.
40 1. Create your Notebook.
41
42 [[image:Project Workspace Jupyter Create Python Notebook v2.1.6 2023-02-07.png||height="22%" width="50%"]]
43
44 = Launching the Terminal =
45
46 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.
47
48 1. In the Launcher, click the **Terminal **icon, or click **File > New > Terminal**.
49 1. The terminal window opens.
50
51 [[image:Project Workspace Jupyter Launch Terminal v2.1.6 2023-02-07.png||height="9%" width="50%"]]
52
53 Ubuntu is used to host Jupyter Notebook. Use the command cat /etc/os-release to determine to current version of Ubuntu:
54
55 {{code language="none"}}
56 uname@jupyter-uname:/etc$ cat os-release
57 NAME="Ubuntu"
58 VERSION="20.04.4 LTS (Focal Fossa)"
59 ID=ubuntu
60 ID_LIKE=debian
61 PRETTY_NAME="Ubuntu 20.04.4 LTS"
62 VERSION_ID="20.04"
63 HOME_URL="https://www.ubuntu.com/"
64 SUPPORT_URL="https://help.ubuntu.com/"
65 BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
66 PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
67 VERSION_CODENAME=focal
68 UBUNTU_CODENAME=focal
69 {{/code}}
70
71 = Creating a Python Virtual Environment and Registering a Kernel =
72
73 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).
74
75 == Using conda ==
76
77 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>>url:https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html]], and the corresponding documentation of how to manage virtual environments using conda [[here>>url:https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html]]. 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.
78
79 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 {{code}}source activate{{/code}}. 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”:
80
81 {{code language="none"}}
82 username@jupyter-username:~$ source activate
83 (base) username@jupyter-username:~$
84 {{/code}}
85
86 To create a new environment, run the following commands in the terminal after activating conda:
87
88 {{code language="none"}}
89 (base) username@jupyter-username:~$ conda create --name your_env_name
90 {{/code}}
91
92 Replace {{code}}your_env_name{{/code}} 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:
93
94 {{code language="none"}}
95 (base) username@jupyter-username:~$ conda create --name sample_env
96 Collecting package metadata (current_repodata.json): done
97 Solving environment: done
98
99 ## Package Plan ##
100
101 environment location: /home/username/.conda_envs/sample_env
102
103 Proceed ([y]/n)? y
104
105 Preparing transaction: done
106 Verifying transaction: done
107 Executing transaction: done
108 #
109 # To activate this environment, use
110 #
111 # $ conda activate sample_env
112 #
113 # To deactivate an active environment, use
114 #
115 # $ conda deactivate
116
117 (base) username@jupyter-username:~$
118 {{/code}}
119
120 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 {{code}}conda activate your_env_name{{/code}} and replace “your_env_name” with the corresponding name you chose (“sample_env” in this example):
121
122 {{code language="none"}}
123 (base) username@jupyter-username:~$ conda activate sample_env
124 (/home/username/.conda_envs/sample_env) username@jupyter-username:~$
125 {{/code}}
126
127 You can now install the desired packages in this new conda environment, for instance using the {{code}}conda install{{/code}} command. For example, in order to install the latest version of Python, run:
128
129 {{code language="none"}}
130 (/home/username/.conda_envs/sample_env) username@jupyter-username:~$ conda install python
131 {{/code}}
132
133 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:
134
135 {{code language="none"}}
136 (base) username@jupyter-username:~$ conda list
137 {{/code}}
138
139 To see a list of all existing conda environments, run:
140
141 {{code language="none"}}
142 (base) username@jupyter-username:~$ conda info --envs
143 {{/code}}
144
145 Please find many more examples and the full documentation of how to manage conda environments [[here>>url:https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#]]. 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.
146
147 == Using venv ==
148
149 As an alternative to using conda, you can also use the Python native package venv. Please find the full documentation of venv [[here>>url:https://docs.python.org/3/library/venv.html#]], and a short example of how to create a new virtual environment using venv below:
150
151 {{code language="none"}}
152 username@jupyter-username:~$ python3 -m venv your_env_name
153 username@jupyter-username:~$ source your_env_name/bin/activate
154 {{/code}}
155
156 == Registering the new Virtual Environment as Kernel ==
157
158 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:
159
160 {{code language="none"}}
161 username@jupyter-username:~$ python -m ipykernel install --user --name=your_env_name
162 {{/code}}
163
164 Please replace {{code}}your_env_name{{/code}} 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:
165
166 {{code language="none"}}
167 (your_env_name) username@jupyter-username:~$ conda install -c anaconda ipykernel
168 {{/code}}
169
170 or:
171
172 {{code language="none"}}
173 username@jupyter-username:~$ pip install ipykernel
174 {{/code}}
175
176 Once you have installed ipykernel, re-run the command above to register your novel environment via ipykernel.
177
178 **Example usage:**
179
180 {{code language="none"}}
181 (/home/username/.conda_envs/sample_env) username@jupyter-username:~$ python -m ipykernel install --user --name=sample_env
182 Installed kernelspec sample_env in /home/username/.local/share/jupyter/kernels/sample_env
183 (/home/username/.conda_envs/sample_env) username@jupyter-username:~$
184 {{/code}}
185
186 Afterwards, the environment will be listed when you open the Launcher to open a new Jupyter Notebook:
187
188 [[image:Project Workspace Jupyter view new Kernel 2023-07-11.png||height="25%" width="50%"]]
189
190
191 and also from each opened Notebook, e.g., via **Kernel > Change Kernel…** :
192
193 [[image:Project Workspace Jupyter Kernel change Kernel dropdown 2023-07-11.png||height="64%" width="50%"]]
194
195 = Installing New Python Packages =
196
197 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.
198
199 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.
200
201 For example:
202
203 {{code language="none"}}
204 pip install my_package
205 {{/code}}
206
207 If you are using conda to manage python packages:
208
209 {{code language="none"}}
210 conda install my_package
211 {{/code}}
212
213 The above information is provided as examples only. Please refer to documentation provided by your IT department with respect to proxy configuration.
214
215 = Using the Pilot Command Line Interface in a JupyterHub Terminal =
216
217 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.
218
219 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.
220
221 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//.
222
223 == Launching Pilot Command Line Interface ==
224
225 1. Launch your Project and click the **JupyterHub** icon in in the workspace icon group.
226 1. Click the **Terminal **launcher icon to open the Terminal.
227 1. In the Jupyterhub Terminal, type {{code}}pilotcli{{/code}} to launch the latest version of the Pilot Command Line Interface.
228 1. Use the {{code}}pilotcli --help{{/code}} at any time to show the welcome message again.
229
230 {{code language="none"}}
231 collaborator4@jupyter-collaborator4:~$ pilotcli
232 Usage: pilotcli [OPTIONS] COMMAND [ARGS]...
233
234 What's new (Version 2.2.0):
235
236 1. CLI supports to perform multi-threading upload for file/folders
237
238 2. CLI supports to perform resumable upload for single file
239
240
241
242 Options:
243 --help Show this message and exit.
244
245 Commands:
246 container_registry Container Registry Actions.
247 dataset Dataset Actions.
248 file File Actions.
249 project Project Actions.
250 use_config Config Actions.
251 user User Actions.
252 {{/code}}
253
254 == Logging into the Pilot Command Line Interface ==
255
256 Users are required to login with platform credentials before performing any tasks through Pilot Command Line Interface.
257
258 * Use the command {{code}}pilotcli user login{{/code}} to log into the Pilot Command Line Interface.
259
260 {{code language="none"}}
261 collaborator4@jupyter-collaborator4:~$ pilotcli user login
262 Please, access https://iam.staging.pilot.indocresearch.com/realms/pilot/device?user_code=XXXX-XXXX to proceed
263 ▄▄▄▄▄▄▄ ▄ ▄▄ ▄ ▄▄▄▄ ▄ ▄▄▄▄▄▄▄
264 █ ▄▄▄ █ ▄ ▄███ ▀▀ █▀ ▀██▄ █ ▄▄▄ █
265 █ ▄ ▀ ▄ ▀▄ ▀▀ ▄█▀▄▀ ▀▀▄█▄▄▀ █████▄▄▀▄
266 ▄▄▄▄▄▄▄ ▀ ▀█▄ ▀▄ ██▀█ ▄▀▄▄ █ ▄ █▀▄▄▄
267 █ ▄▄▄ █ █▀█▄▀ █▀ █▀▀█ ▀▄█▄█▄▄▄█▀▄█
268 █ ███ █ █▀██▀▄ █▀▄▄▀▀█▄▀▀█▄▀█ ▀ ▀▄▀██
269 █▄▄▄▄▄█ ▄▀▄▄██▄▄▀▄ ▀▀▄ ▄▄▀▀▀▄ █▄▄▄█
270
271 Waiting validation finish...
272 {{/code}}
273
274 * (((
275 You’ll be asked to validate your HDC user account using one of the provided methods.
276
277 * Copy and paste the provided validation link into a new browser tab or
278 * Scan the QR code with your mobile device.
279 )))
280 * Open the login window and enter your HDC username and password (i.e. your EBRAINS account credentials).
281 * Grant access by clicking **Yes**.
282
283 [[image:Pilotcli Jupyter user login Grant Access window v2.4.0 2023-05-25.png||height="46%" width="50%"]]
284
285 [[image:Pilotcli Jupyter user login Device Login Successful v2.4.0 2023-05-25.png||height="16%" width="50%"]]
286
287 * After successful confirmation, return to the terminal in your JupyterHub browser tab.
288
289 {{code language="none"}}
290 Welcome to the Command Line Tool!
291 {{/code}}
292
293 * You’re now ready to start using the Pilot Command Line Interface to work with your Project data in JupyterHub.
294
295 == Zone Restrictions when using Pilot Command Line Interface in JupyterHub ==
296
297 When using the Pilot Command Line Interface in JupyterHub and the following actions are possible on the derivative files generated in JupyterHub:
298
299 |=(% colspan="1" rowspan="1" %)(((
300 **File Operation**
301 )))|=(% colspan="1" rowspan="1" %)(((
302 **Permitted in the **
303 **Green Room**
304 )))|=(% colspan="1" rowspan="1" %)(((
305 **Permitted in the **
306 **Core**
307 )))
308 |(% colspan="1" rowspan="1" %)File upload 
309 (upload derivative output files from JupyterHub to the Green Room or Core storage)|(% colspan="1" rowspan="1" %)(((
310 Yes
311 )))|(% colspan="1" rowspan="1" %)(((
312 Yes
313 )))
314 |(% colspan="1" rowspan="1" %)File download
315 (download files from Green Room or Core into JupyterHub)|(% colspan="1" rowspan="1" %)(((
316 **No**
317 )))|(% colspan="1" rowspan="1" %)(((
318 Yes
319 )))
320
321 == Downloading Project Data to JupyterHub using the Pilot Command Line Interface ==
322
323 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.
324
325 File related commands are grouped in the {{code}}file{{/code}} category. To view the full list of commands in this category, type {{code}}pilotcli file --help{{/code}}. To download project data, use the file sync command. To view the full list of commands in this category, type {{code}}pilotcli file sync --help{{/code}}.
326
327
328 {{code language="none"}}
329 collaborator4@jupyter-collaborator4:~$ pilotcli file sync --help
330 Usage: pilotcli file sync [OPTIONS] [PATHS]... OUTPUT_PATH
331
332 Download files/folders from a given Project/folder/file in core zone.
333
334 Options:
335 -z, --zone TEXT Target Zone (i.e., core/greenroom)
336 --zip Download files as a zip.
337 -i, --geid Enable downloading by geid.
338 --help Show this message and exit.
339 {{/code}}
340
341 === Example ===
342
343 Downloading a file from the Core to your Home Directory:
344
345 Reminder: Please follow Linux conventions for file management. If your filename contains spaces, wrap it in single or double quotes.
346
347 * //Filename~:// “Chemical Tracking Data.csv”
348 * //Source~:// Project “Indoc Test Project”, “Core” storage zone, folder “collaborator4” {{code}}indoctestproject/collaborator4/Chemical Tracking Data.csv -z core{{/code}}
349 * //Destination: //user's Home directory in the Guacamole or JupyterHub VM {{code}}.{{/code}}
350 * //Command group/option: //{{code}}file sync{{/code}}
351
352 {{code language="none"}}
353 collaborator4@jupyter-collaborator4:~$ pilotcli file sync indoctestproject/collaborator4/'Chemical Tracking Data.csv' . -z core
354 start downloading...
355 Downloading Chemical Tracking Data.csv |██████████████████████████████ 100% 00:00
356 File has been downloaded successfully and saved to: ./Chemical Tracking Data.csv
357 {{/code}}
358
359 To confirm successful download, type {{code}}ls{{/code}} and verify the file "Chemical Tracking Data.csv" is stored in the Home folder.
360
361 {{code language="none"}}
362 collaborator4@jupyter-collaborator4:~$ ls
363 'Chemical Tracking Data.csv' pilotcli
364 {{/code}}
365
366 The file “Chemical Tracking Data.csv” can be viewed in the JupyterHub graphical user interface:
367
368 [[image:Jupyter downloaded file in Home folder v2.4.11 2023-05-25 1850.png||height="15%" width="50%"]]
369
370
371 == Uploading Project Data from JupyterHub using the Pilot Command Line Interface ==
372
373 After analyzing Project data inside the JupyterHub, you can upload the generated outputs back into the Project via the Pilot Command Line Interface.
374
375 === Example ===
376
377 * //Filename//: Chemical Tracking Data rev.csv
378 * //Source~:// user's Home directory in JupyterHub {{code}}.{{/code}}
379 * //Destination//: Project “Indoc Test Project”, folder “collaborator4”, “Core” storage zone,
380 {{code}}indoctestproject/collaborator4{{/code}} {{code}}-z core{{/code}}
381 * //Command group/option~:// {{code}}file upload{{/code}}
382 * //User message// (for upload back to the Core): “my workbench output, no additional sensitive data"
383 * //Command~:// {{code}}pilotcli file upload ./'Chemical Tracking Data rev.csv' -p{{/code}} {{code}}indoctestproject/collaborator4 -z core -m "my workbench output, no additional sensitive data"{{/code}}
384
385 When uploading data to the Core, you are reminded that you are bypassing the usual Green Room upload workflow. To confirm, type {{code}}y{{/code}} at the prompt, or {{code}}N{{/code}} to cancel.
386
387 {{code language="none"}}
388 collaborator4@jupyter-collaborator4:~$ pilotcli file upload ./'Chemical Tracking Data rev.csv' -p indoctestproject/collaborator4 -z core -m "my workbench output, no additional sensitive data"
389 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
390 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
391 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.
392 To cancel this transfer, enter [n/No]
393 To confirm and proceed with the data transfer, enter [y/Yes]
394 [y/N]: y
395 Starting upload of: ./Chemical Tracking Data rev.csv
396 Pre-upload complete.
397 Uploading Chemical Tracking Data rev.csv: |██████████████████████████████ 100% 00:00
398 Upload Time: 2.92s for 1 files
399 All uploading jobs have finished.
400 {{/code}}
401
402 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.
403
404 {{code language="none"}}
405 collaborator4@jupyter-collaborator4:~$ pilotcli file list indoctestproject/collaborator4 -z core
406 Chemical Tracking Data rev.csv Chemical Tracking Data.csv
407 {{/code}}
408
409 [[image:Jupyterhub file upload back to core v2.4.11 2023-05-25 1926.png||height="13%" width="50%"]]
410
411 ----
412
413 Copyright © 2023 [[Indoc Research>>url:https://www.indocresearch.org/]].
414
415 HealthDataCloud is powered by Pilot technology, a product of [[Indoc Research>>url:https://www.indocresearch.org/]].
416