Skip to content
Myr

improve env vars config example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by jordan bonser · committed by David Lord · parent b765e0d · Browse files · 1 file+9 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+9 added−11 removed

  • other:+9 added−11 removed

1 file+9 added−11 removed

modified:docs/config.rst+9 added-11 removed
435 unmodified lines
436methods on the config object as well to load from individual files. For a437complete reference, read the :class:`~flask.Config` object's438documentation.439440441Configuring from Environment Variables442--------------------------------------443444In addition to pointing to configuration files using environment variables, you
3 unmodified lines
448Environment variables can be set on Linux or OS X with the export command in449the shell before starting the server::450451 $ export SECRET_KEY='5f352379324c22463451387a0aec5d2f'451 $ export DEBUG=False452 $ export MAIL_ENABLED=false453 $ python run-app.py454 * Running on http://127.0.0.1:5000/454 * Restarting with reloader...455456On Windows systems use the `set` builtin instead::456On Windows systems use the ``set`` builtin instead::457458 >set SECRET_KEY='5f352379324c22463451387a0aec5d2f'459 >set DEBUG=False459460While this approach is straightforward to use, it is important to remember that461environment variables are strings -- they are not automatically deserialized462into Python types.463464Here is an example of a configuration file that uses environment variables::465467 # Example configuration466 import os467470 ENVIRONMENT_DEBUG = os.environ.get("DEBUG", default=False)471 if ENVIRONMENT_DEBUG.lower() in ("f", "false"):472 ENVIRONMENT_DEBUG = False468 _mail_enabled = os.environ.get("MAIL_ENABLED", default="true")469 MAIL_ENABLED = _mail_enabled.lower() in {"1", "t", "true"}470474 DEBUG = ENVIRONMENT_DEBUG475 SECRET_KEY = os.environ.get("SECRET_KEY", default=None)471 SECRET_KEY = os.environ.get("SECRET_KEY")472473 if not SECRET_KEY:477 raise ValueError("No secret key set for Flask application")474 raise ValueError("No SECRET_KEY set for Flask application")475476477Notice that any value besides an empty string will be interpreted as a boolean478``True`` value in Python, which requires care if an environment explicitly sets
3 unmodified lines
482ability to access the configuration when starting up. There are other methods483on the config object as well to load from individual files. For a complete484reference, read the :class:`~flask.Config` class documentation.485486487Configuration Best Practices488----------------------------489490The downside with the approach mentioned earlier is that it makes testing
204 unmodified lines