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
Provenance
- Commit
800c744- Tree
84750ed- Author
jordan bonser <jordan_bonser@live.co.uk>- Committer
David Lord <davidism@gmail.com>- Parent
b765e0d
1 file changed+9 added−11 removed
modified:docs/config.rst+9 added-11 removed
435 unmodified lines436 methods on the config object as well to load from individual files. For a437 complete reference, read the :class:`~flask.Config` object's438 documentation.439 440+441 Configuring from Environment Variables442 --------------------------------------443 444 In addition to pointing to configuration files using environment variables, you3 unmodified lines448 Environment variables can be set on Linux or OS X with the export command in449 the shell before starting the server::450 451 $ 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...455 456-On Windows systems use the `set` builtin instead::456+On Windows systems use the ``set`` builtin instead::457 458 >set SECRET_KEY='5f352379324c22463451387a0aec5d2f'459- >set DEBUG=False459 460 While this approach is straightforward to use, it is important to remember that461 environment variables are strings -- they are not automatically deserialized462 into Python types.463 464 Here is an example of a configuration file that uses environment variables::465 467- # Example configuration466 import os467 470- 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"}470 474- DEBUG = ENVIRONMENT_DEBUG475- SECRET_KEY = os.environ.get("SECRET_KEY", default=None)471+ SECRET_KEY = os.environ.get("SECRET_KEY")472+473 if not SECRET_KEY:477- raise ValueError("No secret key set for Flask application")474+ raise ValueError("No SECRET_KEY set for Flask application")475 476 477 Notice that any value besides an empty string will be interpreted as a boolean478 ``True`` value in Python, which requires care if an environment explicitly sets3 unmodified lines482 ability to access the configuration when starting up. There are other methods483 on the config object as well to load from individual files. For a complete484 reference, read the :class:`~flask.Config` class documentation.485 486+487 Configuration Best Practices488 ----------------------------489 490 The downside with the approach mentioned earlier is that it makes testing204 unmodified lines