Accessing environment variables inside jupyterhub jinja templates


I am trying to access environment variables inside JupyterHub templates to set the image URL for images hosted in an S3 bucket. I’ve set the necessary environment variable with the S3 bucket URL, but I’m unsure how to use it within the JupyterHub HTML templates.

How can I access environment variables inside JupyterHub templates using the Jinja2 templating syntax? Could you please provide a solution or guidance for this?

In which template are you trying to set image URL? You will have to pass variables to the template when rendering the HTML page. For instance, look here where we pass variables for_user, spawner, spawner_options_form, etc., to the template. If you look into spawn.html template, those variables are referenced inside the HTML template.

I am accessing JupyterHub templates from this GitHub repository: The s3_bucket_name remains constant for all images. I aim to define the bucket name globally and then append it to the image object names across all HTML pages. Could you please provide a solution for configuring this inside the values.yaml file?

You should use JupyterHub.template_vars which will be set on all templates. In your case, it will be c.JupyterHub.template_vars = {'s3_bucket_name': '<name_of_the_bucket>'}. Then you can refer this s3_bucket_name in all templates to add URLs to your images. Look into the documentation that explains with an example.

1 Like

Thank you so much, @mahendrapaipuri. Now I am able to access inside hub temples.

1 Like