Creating and accessing environment variables

Version 2.1 by chaney08 on 2021/06/22 19:34

 You can use the URL query parameters to add environment variables to a jupyter notebook.

For example, here is a link to a notebook:

https://lab.ebrains.eu/user-redirect/lab/tree/shared/example_path/notebook.ipynb

To add environment variables that are accessible within that notebook (in this case LAB_MY_VARIABLE) use this URL:

https://lab.ebrains.eu/user-redirect/lab/tree/shared/example_path/notebook.ipynb?LAB_MY_VARIABLE=Hello

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.

Note: For the query parameters to be accessible in the notebook they needs to be prefixed with LAB_ e.g. ?LAB_MY_VARIABLE=4 

To access a environment variable in the notebook you can use this snipped:

import os
os.environ.get('LAB_MY_VARIABLE')