Setting Environment Variables

This documentation is for Dash Enterprise,
Plotly’s commercial platform for managing and improving
Dash applications in your organization.
View the docs or
request a trial.

In Plotly Enterprise 2.5.0 and up, you can store secrets as environment variables
instead of hardcoded in your application code.
It’s good practice to keep application
secrets like database passwords outside of your code so that they aren’t
mistakenly exposed or shared. Instead of storing these secrets in code,
you can store them as environment variables and your Dash Application code
can reference them dynamically.


Add Environment Variables

To add environment variables via the Dash Enterprise UI,
navigate to the application settings. Here, use the text boxes to
add the environmental variable name and value. For example, "DATABASE_USER"
and "DATABASE_PASSWORD".

Add Environment Variables

Referencing Environment Variables in Your Code

You can reference these variables with the os.environ module:

database_password = os.environ['DATABASE_PASSWORD']

 

Alternatively, if the variable isn’t in your environment and you want
to fall back to some other value, use:

database_password = os.environ.get('DATABASE_PASSWORD', 'my-default-database-password')

Defining Environment Variables In Your Local Environment

By referencing these environment variables in our code, we’ll need to add
these variables to our local environment as well. One easy way to do
this is to define the variables on-the-fly when you run python app.py.
That is, instead of running python app.py, run:

$ DATABASE_USER=chris DATABASE_PASSWORD=my-password python app.py

 

Alternatively, you can define them for your session by “exporting” them:

$ export DATABASE_USER=chris
$ export DATABASE_PASSWORD=my-password
$ python app.py

Delete Environment Variables

To remove an environment variable via the Dash Enterprise UI,
navigate to the application settings. Here, simply click the red
cross situated to the right-hand side of the environment variable.

Delete Environment Variables