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

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
主站蜘蛛池模板: 平昌县| 专栏| 丰县| 台北县| 安新县| 龙里县| 威信县| 孟州市| 尉氏县| 蛟河市| 屯门区| 襄城县| 盐津县| 盐源县| 江都市| 正安县| 萨嘎县| 凤城市| 九龙坡区| 义乌市| 彰武县| 芦溪县| 中牟县| 柞水县| 贵南县| 绥棱县| 金阳县| 始兴县| 太仆寺旗| 清水河县| 景宁| 定州市| 黔江区| 安新县| 西华县| 溧阳市| 荔浦县| 元阳县| 襄垣县| 通辽市| 阳新县|