From 6ef081c3323b2dd4b50882625e086e84c2d99693 Mon Sep 17 00:00:00 2001 From: tristan fisher Date: Fri, 9 Jun 2017 10:19:08 -0400 Subject: [PATCH 1/3] Add basic how-to on using envvars for config --- docs/config.rst | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/config.rst b/docs/config.rst index 1da7d2ee..096d527f 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -375,6 +375,54 @@ methods on the config object as well to load from individual files. For a complete reference, read the :class:`~flask.Config` object's documentation. +Configuring from Environmental Variables +---------------------------------------- + +In addition to pointing to configuration files using environment variables, you +may find it useful (or necessary) to control your configuration values +directly from the environment. + +Environment variables can be set on Linux or OS X with the export command in the +shell before starting the server:: + + $ export SECRET_KEY='?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83' + $ export DEBUG=False + $ python run-app.py + * Running on http://127.0.0.1:5000/ + * Restarting with reloader... + +On Windows systems use the `set` builtin instead:: + + >set SECRET_KEY='?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83' + >set DEBUG=False + +While this approach is straightforward to use, it is important to remember that +environment variables are strings -- they are not automatically deserialized into Python +types. + +Here is an example of a configuration file that uses environmental variables:: + + # Example configuration + import os + + ENVIRONMENT_DEBUG = os.environ.get("DEBUG", default=False) + if ENVIRONMENT_DEBUG.lower() in ("f", "false"): + ENVIRONMENT_DEBUG = False + + DEBUG = ENVIRONMENT_DEBUG + SECRET_KEY = os.environ.get("SECRET_KEY", default=None) + if not SECRET_KEY: + raise ValueError("No secret key set for Flask application") + + +Notice that any value besides a empty string will be interpreted as a boolean True value in Python, which +requires care if an environment may explicit setting values intended to be False. + +Make sure to load the configuration very early on, so that extensions have +the ability to access the configuration when starting up. There are other +methods on the config object as well to load from individual files. For a +complete reference, read the :class:`~flask.Config` object's +documentation. Configuration Best Practices ---------------------------- From 8c8e523ca488cf339541943f1e527fe7d6d18ad1 Mon Sep 17 00:00:00 2001 From: tristan fisher Date: Fri, 9 Jun 2017 10:54:47 -0400 Subject: [PATCH 2/3] Fixes grammar and phrasing on envvar how-to --- docs/config.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 096d527f..e0541511 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -415,8 +415,8 @@ Here is an example of a configuration file that uses environmental variables:: raise ValueError("No secret key set for Flask application") -Notice that any value besides a empty string will be interpreted as a boolean True value in Python, which -requires care if an environment may explicit setting values intended to be False. +Notice that any value besides an empty string will be interpreted as a boolean True value in Python, which +requires care if an environment explicitly sets values intended to be False. Make sure to load the configuration very early on, so that extensions have the ability to access the configuration when starting up. There are other From 56e53f99d15e0ec0f3b4443f683b9aa4d34a26e8 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 9 Jun 2017 10:28:54 -0700 Subject: [PATCH 3/3] formatting [ci skip] --- docs/config.rst | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index e0541511..186a3695 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -375,15 +375,15 @@ methods on the config object as well to load from individual files. For a complete reference, read the :class:`~flask.Config` object's documentation. -Configuring from Environmental Variables ----------------------------------------- +Configuring from Environment Variables +-------------------------------------- In addition to pointing to configuration files using environment variables, you -may find it useful (or necessary) to control your configuration values -directly from the environment. +may find it useful (or necessary) to control your configuration values directly +from the environment. -Environment variables can be set on Linux or OS X with the export command in the -shell before starting the server:: +Environment variables can be set on Linux or OS X with the export command in +the shell before starting the server:: $ export SECRET_KEY='?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83' $ export DEBUG=False @@ -397,10 +397,10 @@ On Windows systems use the `set` builtin instead:: >set DEBUG=False While this approach is straightforward to use, it is important to remember that -environment variables are strings -- they are not automatically deserialized into Python -types. +environment variables are strings -- they are not automatically deserialized +into Python types. -Here is an example of a configuration file that uses environmental variables:: +Here is an example of a configuration file that uses environment variables:: # Example configuration import os @@ -415,14 +415,14 @@ Here is an example of a configuration file that uses environmental variables:: raise ValueError("No secret key set for Flask application") -Notice that any value besides an empty string will be interpreted as a boolean True value in Python, which -requires care if an environment explicitly sets values intended to be False. +Notice that any value besides an empty string will be interpreted as a boolean +``True`` value in Python, which requires care if an environment explicitly sets +values intended to be ``False``. -Make sure to load the configuration very early on, so that extensions have -the ability to access the configuration when starting up. There are other -methods on the config object as well to load from individual files. For a -complete reference, read the :class:`~flask.Config` object's -documentation. +Make sure to load the configuration very early on, so that extensions have the +ability to access the configuration when starting up. There are other methods +on the config object as well to load from individual files. For a complete +reference, read the :class:`~flask.Config` class documentation. Configuration Best Practices ----------------------------