Flask Extension¶
Dynaconf provides a drop in replacement for .
As Flask encourages the composition by overriding the attribute this extension follows the patterns of Flask and turns your Flask’s in to a instance.
Initialize the extension¶
Initialize the FlaskDynaconf extension in your
from flask import Flask
from dynaconf import FlaskDynaconf
app = Flask(__name__)
FlaskDynaconf(app)
You can optionally use as well.
Use environment variables¶
Then the will work as a instance and will be the global prefix for exporting environment variables.
Example:
export FLASK_DEBUG=true # app.config.DEBUG
export FLASK_INTVALUE=1 # app.config['INTVALUE']
export FLASK_MAIL_SERVER='host.com' # app.config.get('MAIL_SERVER')
Settings files¶
You can also have settings files for your Flask app, in the root directory (the same where you execute ) put your and files and then define your environments , and .
To switch the working environment the variable can be used, so to work in development mode or to switch to production.
IMPORTANT: To use CLI the must be defined.
IF you don’t want to manually create your config files take a look at the CLI
Customizations¶
It is possible to customize how your Flask project will load settings, example: You want your users to customize a settings file defined in and you want environment variables to be loaded from
your flask (or wherever you setup dynaconf)
GLOBAL_ENV_FOR_DYNACONF = "PROJECTNAME"
"""This defines which environment variable global prefix dynaconf will load
That means that `export PROJECTNAME_FOO=1` will be loaded to `app.config.FOO
On command line it is possible to check it with `dynaconf list -k foo`"""
ENVVAR_FOR_DYNACONF = "PROJECTNAME_SETTINGS"
"""This defines which path dynaconf will look to load config files
example: export PROJECTNAME_SETTINGS=/path/to/settings.toml and the format can be
.ini, .json, .yaml or .toml
e.g::
export PROJECTNAME_SETTINGS=settings.toml
[default]
FOO = 1
[development]
FOO = 2
[production]
FOO = 3
OR::
export PROJECTNAME_SETTINGS=settings.yaml
default:
foo: 1
development:
foo: 2
production:
foo: 3
It is also possible to pass a list of files::
export PROJECTNAME_SETTINGS=settings.toml,other_settings.yaml,another.json
The variables will be cascaded in the defined order (last wins the precedence)
The environment variables wins precedence over all!
"""
# load dynaconf
app = Flask(__name__)
FlaskDynaconf(
app,
GLOBAL_ENV_FOR_DYNACONF=GLOBAL_ENV_FOR_DYNACONF,
ENVVAR_FOR_DYNACONF=ENVVAR_FOR_DYNACONF
)
Then the working environment can now be switched using