Each application needs a configuration. Flask allows developers to implement their application configuration using several different approaches:
Regardless of what approach developer chooses for her/ his Flask application configuration, all configuration values are loaded on the config attribute(configuration dictionary) of the Flask application object:
There is a list of configuration values, used internally by Flask.
Note that Flask expects configuration to be available when the application starts up.
Flask allows developers to store their configuration in separate files and load it from them.
The configuration file uses INI file syntax – name/value pairs in a plain text file, separated by an equal sign (“=”).
Good pattern to follow is to use classes for your Flask application configuration:
and to use this configuration, the from_object method should be used:
The application can be configured based on configuration file, which is specified by a environment variable:
here the FLASK_CONFIG_FILE environment variable points to the configuration file.
Since version 0.8 Flask provides one more configuration option – instance folders. The instance folder is designed not to be under source control and could store sensitive information. This instance folder should be directly deployed on the production server. To be able to use it, the instance_path of the Flask object should be specified:
or use it as relative path, where we have instance folder under our application folder.
Now you are ready to configure your application based on the instance folders mechanism, where the flask.cfg is stored either under the instance_path (1) or under the instance folder (2)
Good practice is to have a default configuration, which is under source control and to override it with sensitive and specific information kept in instance folders. For the default configuration you could use object-based configuration hierarchy(described in Object-based configuration section) and to manage which configuration object to load via environment variables:
The complete demo application, described in this blog post, can be found here.
In the next blog post I will describe how to use templates in your Flask project.