Version 8.1 by deuerlei on 2021/08/24 12:54

Show last authors
1 Notebooks can be opened from a link in a collab or other web page.
2
3 Some notebooks may need to be passed parameters, e.g. to indicate the context they are run in. For this, you can use the URL query parameters to add environment variables to a Jupyter Notebook.
4
5 For example, here is a link to a notebook:
6
7 (% style="--darkreader-inline-color:#a561c1; color:#8e44ad" %)[[https:~~/~~/lab.ebrains.eu/user-redirect/lab/tree/shared/example_path/notebook.ipynb>>https://lab.ebrains.eu/user-redirect/lab/tree/shared/example_path/notebook.ipynb]]
8
9 ==== (% style="--darkreader-inline-color:#a561c1; color:#8e44ad" %)Custom environment variables(%%) ====
10
11 To add environment variables that are accessible within that notebook (in this case LAB_MY_VARIABLE) use this URL:
12
13 (% style="--darkreader-inline-color:#a561c1; color:#8e44ad" %)https:~/~/lab.ebrains.eu/user-redirect/lab/tree/shared/example_path/notebook.ipynb(% style="--darkreader-inline-color:#aec2d3; color:#2c3e50" %)**?LAB_MY_VARIABLE=Hello**
14
15 You can add as many variables as you want but the query string needs to be properly formatted. You can find more info about the query string format [[here>>https://en.wikipedia.org/wiki/Query_string]].
16
17 //Note**:**//
18
19 //- For the query parameters to be accessible in the notebook they need to be prefixed with LAB_ e.g. LAB_MY_VARIABLE=4 //
20
21 //- For the environment variables to be accessible within the notebook the notebook needs to be accessed via a "shareable link" . You can find a tutorial on how to create a shareable link to a notebook //[[here>>https://wiki.ebrains.eu/bin/view/Collabs/the-collaboratory/Documentation%20Lab/Tutorial#HSharingalinktoyournotebook]]
22
23 From within a notebook you can then access the environment variable from the notebook as in this code snippet:
24
25 {{code}}
26 import os
27 os.environ.get('LAB_MY_VARIABLE')
28 {{/code}}
29
30 ==== (% style="--darkreader-inline-color:#a561c1; color:#8e44ad" %)Default environment variables(%%) ====
31
32 The following default variables are made available within the notebook:
33
34 * The notebook filename (LAB_NB_NAME)
35 * The name of the collab containing the notebook file in its Drive (LAB_COLLAB_NAME)
36 * The Fenix site where the notebook is executed (LAB_EXECUTION_SITE)
37 * The Docker image in which the notebook is executed(LAB_IMAGE_NAME)
38
39 You can find an example notebook showing how to access the default variables [[here>>https://lab.ebrains.eu/user-redirect/lab/tree/shared/The%20Collaboratory/lab_env.ipynb?LAB_TEST=1]]
40
41 //Note~://
42
43 //- The default environment variables are also only available in a notebook that was accessed via a shareable link. //
44
45 - For execution site specific shareable links (e.g. lab.ch.ebrains.eu/..., lab.de.ebrains.eu/...) the default environment variables are also not available. We will update the "copy shareable link" functionality to be site independent in a future release. For now you can create a shareable link that contains the default environment variables by removing the region code from the URL. For example: https:~/~/lab.ebrains.eu/...(% style="--darkreader-inline-color:#b1aaa0; color:#b0a99f" %) instead of(% style="--darkreader-inline-color:#a561c1; color:#8e44ad" %) (%%)https:~/~/lab.ch.ebrains.eu/...