Creating and accessing environment variables

Version 5.2 by chaney08 on 2021/07/20 19:20

Notebooks can be opened from a link in a collab or other web page.

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.

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 need to be prefixed with LAB_ e.g. LAB_MY_VARIABLE=4 

You can then access the environment variable from the notebook as in this code snippet:

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