Setting environment variables dynamically


Is there a way to initialize either a Spawner or a Notebook with environment variables computed “dynamically” (eg per user)? I have a web app which uses the API to interact with JupyterHub (and the single user servers), but each user has unique credentials which they would use to authenticate with the database and I’d like for those credentials to be available in the user environment as opposed to users having to paste them in or having them otherwise exposed in the notebook.

I’m using JupyterHub 1.4.1 with the default DockerSpawner. I’ve seen this GH issue which implies that while it may not be possible with the default maybe a different spawner will let me do what i’m trying to do?

Gonna answer my own question 'cause I figured it out literally right after posting.

In I did:

from dockerspawner import DockerSpawner

class EnvDockerSpawner(DockerSpawner):
    def get_env(self):
        env = super().get_env()
        user_options_env = self.user_options.get("env", {})
        return env

c.JupyterHub.spawner_class = EnvDockerSpawner

Then in my api I do:

rv ="{URL}/users/{username}/server", json={"env": {"DATA": user.json_data}}, headers=_get_notebook_server_header(user))