官术网_书友最值得收藏!

Configuration

When building applications, you will need to expose options to run them, like the information to connect to a database or any other variable that is specific to a deployment.

Flask uses a mechanism similar to Django in its configuration approach. The Flask object comes with an object called config, which contains some built-in variables, and which can be updated when you start your Flask app via your configuration objects.

For example, you can define a Config class in a prod_settings.py file as follows:

    class Config: 
DEBUG = False
SQLURI = 'postgres://tarek:xxx@localhost/db'

And then, load it from your app object using app.config.from_object :

>>> from flask import Flask 
>>> app = Flask(__name__)
>>> app.config.from_object('prod_settings.Config')
>>> print(app.config)
<Config {'SESSION_COOKIE_HTTPONLY': True, 'LOGGER_NAME': '__main__',
'APPLICATION_ROOT': None, 'MAX_CONTENT_LENGTH': None,
'PRESERVE_CONTEXT_ON_EXCEPTION': None,
'LOGGER_HANDLER_POLICY': 'always',
'SESSION_COOKIE_DOMAIN': None, 'SECRET_KEY': None,
'EXPLAIN_TEMPLATE_LOADING': False,
'TRAP_BAD_REQUEST_ERRORS': False,
'SESSION_REFRESH_EACH_REQUEST': True,
'TEMPLATES_AUTO_RELOAD': None,
'JSONIFY_PRETTYPRINT_REGULAR': True,
'SESSION_COOKIE_PATH': None,
'SQLURI': 'postgres://tarek:xxx@localhost/db',
'JSON_SORT_KEYS': True, 'PROPAGATE_EXCEPTIONS': None,
'JSON_AS_ASCII': True, 'PREFERRED_URL_SCHEME': 'http',
'TESTING': False, 'TRAP_HTTP_EXCEPTIONS': False,
'SERVER_NAME': None, 'USE_X_SENDFILE': False,
'SESSION_COOKIE_NAME': 'session', 'DEBUG': False,
'JSONIFY_MIMETYPE': 'application/json',
'PERMANENT_SESSION_LIFETIME': datetime.timedelta(31),
'SESSION_COOKIE_SECURE': False,
'SEND_FILE_MAX_AGE_DEFAULT': datetime.timedelta(0, 43200)}>

However, there are two significant drawbacks when using Python modules as configuration files.

First, it can be tempting to add into those configuration modules some code that's more complex than simple flat classes; and by doing so, it means you will have to treat those modules like the rest of the application code. That's usually not what happens when applications are deployed: the configuration files are managed separately from the code.

Secondly, if another team is in charge of managing the configuration file of your application, they will need to edit the Python code to do so. While this is usually fine, it makes it easier to introduce some problems. For instance, it's harder to make Puppet templates out of Python modules rather than flat, static configuration files.

Since Flask exposes its configuration via app.config, it's pretty simple to load additional options from a YAML file, or any other text-based file.

The INI format is the most-used format in the Python community, because there's an INI parser included in the standard library, and because it's pretty universal.

Many Flask extensions exist to load the configuration from an INI file, but using the standard library ConfigParser is trivial. Although, there's one major caveat from using INI files: variables values are all strings, and your application needs to take care of converting them to the right type.

The Konfig project (https://github.com/mozilla-services/konfig) is a small layer on top of ConfigParser, which automates the conversion of simple types like integers and Booleans.

Using it with Flask is straightforward:

$ more settings.ini 
[flask]
DEBUG = 0
SQLURI = postgres://tarek:xxx@localhost/db

$ python
>>> from konfig import Config
>>> from flask import Flask
>>> c = Config('settings.ini')
>>> app = Flask(__name__)
>>> app.config.update(c.get_map('flask'))
>>> app.config['SQLURI']
'postgres://tarek:xxx@localhost/db
主站蜘蛛池模板: 芜湖市| 绵竹市| 道孚县| 永嘉县| 东港市| 化隆| 宜章县| 盖州市| 佛冈县| 隆化县| 绿春县| 修武县| 漳平市| 岐山县| 太仆寺旗| 孝感市| 随州市| 两当县| 巴马| 安远县| 梁河县| 泾川县| 板桥市| 盐山县| 昌黎县| 桐梓县| 两当县| 遵义县| 长沙市| 克什克腾旗| 阿城市| 常州市| 体育| 鹤庆县| 民权县| 东宁县| 大安市| 南投市| 临沂市| 藁城市| 于田县|