Flask from_envvar setup

Hi, What value should be passed here if I do away with config.py entirely?
FLASKR_SETTINGS=
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

Simply define the environment variable FLASKR_SETTINGS that points to a config file to be loaded. The silent switch just tells Flask to not complain if no such environment key is set.

Hi @funza :wave: Welcome and thanks for reaching out!

Great question! The app.config.from_envvar function will look for a file path inside of the environment variable that you provide and try to read your variables from that file. We recommend against storing secrets on your filesystem so we have a different approach.

This example takes in your configuration as environment variables and assigns them to Flask’s configuration via app.config.

from flask import Flask
from os import environ as env

app = Flask(__name__)
# String
app.config["SERVER_NAME"] = env["SERVER_NAME"]
# String (Mapped Name)
app.config["SECRET_KEY"] = env["FLASK_SECRET_KEY"]
# Boolean
app.config["TESTING"] = env["TESTING"] == "true"
# Int
app.config["NUM_WORKERS"] = int(env["NUM_WORKERS"])

This approach allows you to handle type conversion as well as mapping names in Doppler to known configuration variables in Flask.

To pass environment variables to your application, you can use doppler run:

# Using `flask run`
doppler run -- flask run
# Using gunicorn
doppler run -- gunicorn app:app

Keep in mind that the ENV Flask configuration variable is automatically set by the FLASK_ENV environment variable.

Here are some additional resources on using environment variables in Python and on Flask’s configuration object.

Please let me know if you have any questions!

1 Like

Thank you. I’ve tried this and it works fine. Would it leave a copy in the filesystem or is it safe?

app = Flask(__name__)
doppler_vars = json.loads(os.popen('doppler secrets download --no-file --format json').read())
print(doppler_vars.items())
for key, value in doppler_vars.items():
    app.config[key] = value

Interesting solution! That will not leave a copy in the filesystem, however, it will only work inside environments that have the Doppler CLI installed and configured. I’d still recommend using os.environ to decouple your app from the CLI binary.

If you want to simply pass all of the environment variables to Flask, you can do:

from flask import Flask
import os

app = Flask(__name__)
app.config.from_mapping(os.environ)

In this solution, you would still use doppler run to inject your secrets as environment variables.

The only downside here is that integers and booleans will come through as strings. For example, Flask expects app.config["TESTING"] to be a boolean type. If you aren’t passing in any built-in Flask variables, you probably don’t need to worry about this part.

Hi @funza,

In addition to the excellent advice provided by @Nic_Manoogian, you can take a look at our sample Mandaliron GIFs Python app which uses a Config class to provide type casting and checking.

For a much more full-featured solution to type-safe config, check out pydantic

1 Like

The type hints helped a lot compared to individual type conversions. :+1: .