Added a chapter on configuration options
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 77743a5 · Browse files · 1 file·+67 added−0 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+67 added−0 removed
- other:+67 added−0 removed
Provenance
- Commit
b7c0e56- Tree
b4dd268- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
77743a5
1 file changed+67 added−0 removed
modified:docs/config.rst+67 added
130 unmodified lines131 132 2. Do not write code that needs the configuration at import time. If you133 limit yourself to request-only accesses to the configuration you can134 reconfigure the object later on as needed.135+136+137+Development / Production138+------------------------139+140+Most applications need more than one configuration. There will at least141+be a separate configuration for a production server and one used during142+development. The easiest way to handle this is to use a default143+configuration that is always loaded and part of version control, and a144+separate configuration that overrides the values as necessary as mentioned145+in the example above::146+147+ app = Flask(__name__)148+ app.config.from_object('yourapplication.default_settings')149+ app.config.from_envvar('YOURAPPLICATION_SETTINGS')150+151+Then you just have to add a separate `config.py` file and export152+``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However153+there are alternative ways as well. For example you could use imports or154+subclassing.155+156+What is very popular in the Django world is to make the import explicit in157+the config file by adding an ``from yourapplication.default_settings158+import *`` to the top of the file and then overriding the changes by hand.159+You could also inspect an environment variable like160+``YOURAPPLICATION_MODE`` and set that to `production`, `development` etc161+and import different hardcoded files based on that.162+163+An interesting pattern is also to use classes and inheritance for164+configuration::165+166+ class Config(object):167+ DEBUG = False168+ TESTING = False169+ DATABASE_URI = 'sqlite://:memory:'170+171+ class ProductionConfig(Config):172+ DATABASE_URI = 'mysql://user@localhost/foo'173+ 174+ class DevelopmentConfig(Config):175+ DEBUG = True176+177+ class TestinConfig(Config):178+ TESTING = True179+180+To enable such a config you just have to call into181+:meth:`~flask.Config.from_object`::182+183+ app.config.from_object('configmodule.ProductionConfig')184+185+There are many different ways and it's up to you how you want to manage186+your configuration files. However here a list of good recommendations::187+188+- keep a default configuration in version control. Either populate the189+ config with this default configuration or import it in your own190+ configuration files before overriding values.191+- use an environment variable to switch between the configurations.192+ This can be done from outside the Python interpreter and makes193+ development and deployment much easier because you can quickly and194+ easily switch between different configs without having to touch the195+ code at all. If you are working often on different projects you can196+ even create your own script for sourcing that activates a virtualenv197+ and exports the development configuration for you.198+- Use a tool like `fabric`_ in production to push code and199+ configurations sepearately to the production server(s).200+201+.. _fabric: http://fabfile.org/