Skip to content
Myr

Improve configuration docs a bit

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

authored by Priit Laes · committed by Priit Laes · parent 45d963d · Browse files · 1 file+20 added−18 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+20 added−18 removed

  • other:+20 added−18 removed

1 file+20 added−18 removed

modified:docs/config.rst+20 added-18 removed
3 unmodified lines
4======================56.. versionadded:: 0.378Applications need some kind of configuration. There are different things9you might want to change like toggling debug mode, the secret key, and a10lot of very similar things.8Applications need some kind of configuration. There are different settings9you might want to change depending on the application environment like10toggling the debug mode, setting the secret key, and other such11environment-specific things.1213The way Flask is designed usually requires the configuration to be14available when the application starts up. You can hardcode the15configuration in the code, which for many small applications is not
15 unmodified lines
31 app = Flask(__name__)32 app.config['DEBUG'] = True3334Certain configuration values are also forwarded to the34:attr:`~flask.Flask` object so that you can read and write them from35there::35:attr:`~flask.Flask` object so you can read and write them from there::3637 app.debug = True3839To update multiple keys at once you can use the :meth:`dict.update`
98 unmodified lines
138139Configuring from Files140----------------------141142Configuration becomes more useful if you can configure from a file, and143ideally that file would be outside of the actual application package so that144you can install the package with distribute (:ref:`distribute-deployment`)145and still modify that file afterwards.142Configuration becomes more useful if you can store it in a separate file,143ideally located outside the actual application package. This makes144packaging and distributing your application possible via various package145handling tools (:ref:`distribute-deployment`) and finally modifying the146configuration file afterwards.147148So a common pattern is this::149150 app = Flask(__name__)
19 unmodified lines
170The configuration files themselves are actual Python files. Only values171in uppercase are actually stored in the config object later on. So make172sure to use uppercase letters for your config keys.173173Here is an example configuration file::174Here is an example of a configuration file::175176 # Example configuration177 DEBUG = False178 SECRET_KEY = '?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83'179178Make sure to load the configuration very early on so that extensions have180Make sure to load the configuration very early on, so that extensions have181the ability to access the configuration when starting up. There are other182methods on the config object as well to load from individual files. For a183complete reference, read the :class:`~flask.Config` object's184documentation.
2 unmodified lines
187Configuration Best Practices188----------------------------189190The downside with the approach mentioned earlier is that it makes testing189a little harder. There is no one 100% solution for this problem in190general, but there are a couple of things you can do to improve that191experience:191a little harder. There is no single 100% solution for this problem in192general, but there are a couple of things you can keep in mind to improve193that experience:1941951. create your application in a function and register blueprints on it.196 That way you can create multiple instances of your application with197 different configurations attached which makes unittesting a lot
6 unmodified lines
204205Development / Production206------------------------207206Most applications need more than one configuration. There will at least207be a separate configuration for a production server and one used during208development. The easiest way to handle this is to use a default209configuration that is always loaded and part of version control, and a208Most applications need more than one configuration. There should be at209least separate configurations for the production server and the one used210during development. The easiest way to handle this is to use a default211configuration that is always loaded and part of the version control, and a212separate configuration that overrides the values as necessary as mentioned213in the example above::214215 app = Flask(__name__)
128 unmodified lines