Skip to content
Myr

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

1 file+67 added−0 removed

modified:docs/config.rst+67 added
130 unmodified lines
1311322. 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.135136137Development / Production138------------------------139140Most applications need more than one configuration. There will at least141be a separate configuration for a production server and one used during142development. The easiest way to handle this is to use a default143configuration that is always loaded and part of version control, and a144separate configuration that overrides the values as necessary as mentioned145in the example above::146147 app = Flask(__name__)148 app.config.from_object('yourapplication.default_settings')149 app.config.from_envvar('YOURAPPLICATION_SETTINGS')150151Then you just have to add a separate `config.py` file and export152``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However153there are alternative ways as well. For example you could use imports or154subclassing.155156What is very popular in the Django world is to make the import explicit in157the config file by adding an ``from yourapplication.default_settings158import *`` to the top of the file and then overriding the changes by hand.159You could also inspect an environment variable like160``YOURAPPLICATION_MODE`` and set that to `production`, `development` etc161and import different hardcoded files based on that.162163An interesting pattern is also to use classes and inheritance for164configuration::165166 class Config(object):167 DEBUG = False168 TESTING = False169 DATABASE_URI = 'sqlite://:memory:'170171 class ProductionConfig(Config):172 DATABASE_URI = 'mysql://user@localhost/foo'173 174 class DevelopmentConfig(Config):175 DEBUG = True176177 class TestinConfig(Config):178 TESTING = True179180To enable such a config you just have to call into181:meth:`~flask.Config.from_object`::182183 app.config.from_object('configmodule.ProductionConfig')184185There are many different ways and it's up to you how you want to manage186your configuration files. However here a list of good recommendations::187188- 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).200201.. _fabric: http://fabfile.org/