Configuration / app.config (Flask)

app.config is the dictionary that holds a Flask application's settings, such as SECRET_KEY, DEBUG, and database URIs. You populate it from objects, files, or environment variables so the same code can run in different environments.

Flask

Centralizing settings in app.config keeps configuration out of your logic. Flask reads some keys itself (SECRET_KEY, DEBUG, TESTING, SQLALCHEMY_DATABASE_URI when using the extension), and you can store your own custom keys there too.

There are several ways to load config: app.config.from_object('config.Production') reads uppercase attributes from a class, app.config.from_envvar() points to a file via an env var, and app.config.from_prefixed_env() pulls FLASK_-prefixed variables. The class-per-environment pattern (Dev/Test/Prod) is common.

Sensitive values (secret keys, database passwords, API tokens) should come from environment variables, never be committed to source control. A typical setup loads a base Config class and overrides per environment.

Syntax

app.config.from_object("config.Production")
app.config["DEBUG"] = True

Examples

Python

import os

class Config:
    SECRET_KEY = os.environ.get("SECRET_KEY")
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL", "sqlite:///app.db")

class Dev(Config):
    DEBUG = True

app.config.from_object("config.Dev")

Learn it in a lesson

See also

Back to the Dictionary