flask.Config

class flask.Config(root_path, defaults=None)[source]

Works exactly like a dict but provides ways to fill it from files or special dictionaries. There are two common patterns to populate the config.

Either you can fill the config from a config file:

app.config.from_pyfile('yourconfig.cfg')

Or alternatively you can define the configuration options in the module that calls from_object() or provide an import path to a module that should be loaded. It is also possible to tell it to use the same module and with that provide the configuration values just before the call:

DEBUG = True
SECRET_KEY = 'development key'
app.config.from_object(__name__)

In both cases (loading from any Python file or loading from modules), only uppercase keys are added to the config. This makes it possible to use lowercase values in the config file for temporary values that are not added to the config or to define the config keys in the same file that implements the application.

Probably the most interesting way to load configurations is from an environment variable pointing to a file:

app.config.from_envvar('YOURAPPLICATION_SETTINGS')

In this case before launching the application you have to set this environment variable to the file you want to use. On Linux and OS X use the export statement:

export YOURAPPLICATION_SETTINGS='/path/to/config/file'

On windows use set instead.

Parameters:
  • root_path – path to which files are read relative from. When the config object is created by the application, this is the application’s root_path.
  • defaults – an optional dictionary of default values

Methods

__init__(root_path[, defaults])
clear(() -> None.  Remove all items from D.)
copy(() -> a shallow copy of D)
from_envvar(variable_name[, silent]) Loads a configuration from an environment variable pointing to a configuration file.
from_json(filename[, silent]) Updates the values in the config from a JSON file.
from_mapping(*mapping, **kwargs) Updates the config like update() ignoring items with non-upper keys.
from_object(obj) Updates the values from the given object.
from_pyfile(filename[, silent]) Updates the values in the config from a Python file.
fromkeys(...) v defaults to None.
get((k[,d]) -> D[k] if k in D, ...)
get_namespace(namespace[, lowercase, ...]) Returns a dictionary containing a subset of configuration options that match the specified namespace/prefix.
has_key((k) -> True if D has a key k, else False)
items(() -> list of D’s (key, value) pairs, ...)
iteritems(() -> an iterator over the (key, ...)
iterkeys(() -> an iterator over the keys of D)
itervalues(...)
keys(() -> list of D’s keys)
pop((k[,d]) -> v, ...) If key is not found, d is returned if given, otherwise KeyError is raised
popitem(() -> (k, v), ...) 2-tuple; but raise KeyError if D is empty.
setdefault((k[,d]) -> D.get(k,d), ...)
update(([E, ...) If E present and has a .keys() method, does: for k in E: D[k] = E[k]
values(() -> list of D’s values)
viewitems(...)
viewkeys(...)
viewvalues(...)