SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
Назадflask.Config(root_path, defaults=None)Works exactly like a dict but provides ways to fill it from files or special dictionaries. There are two common patterns to populate the config.
Either you can fill the config from a config file:
app.config.from_pyfile('yourconfig.cfg')
Or alternatively you can define the configuration options in the module that calls from_object() or provide an import path to a module that should be loaded. It is also possible to tell it to use the same module and with that provide the configuration values just before the call:
DEBUG = True SECRET_KEY = 'development key' app.config.from_object(__name__)
In both cases (loading from any Python file or loading from modules), only uppercase keys are added to the config. This makes it possible to use lowercase values in the config file for temporary values that are not added to the config or to define the config keys in the same file that implements the application.
Probably the most interesting way to load configurations is from an environment variable pointing to a file:
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
In this case before launching the application you have to set this environment variable to the file you want to use. On Linux and OS X use the export statement:
export YOURAPPLICATION_SETTINGS='/path/to/config/file'
On windows use set instead.
| Parameters: |
|
|---|
from_envvar(variable_name, silent=False)Loads a configuration from an environment variable pointing to a configuration file. This is basically just a shortcut with nicer error messages for this line of code:
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
| Parameters: |
|
|---|---|
| Returns: |
bool. |
from_json(filename, silent=False)Updates the values in the config from a JSON file. This function behaves as if the JSON object was a dictionary and passed to the from_mapping() function.
| Parameters: |
|
|---|
New in version 0.11.
from_mapping(*mapping, **kwargs)Updates the config like update() ignoring items with non-upper keys.
New in version 0.11.
from_object(obj)Updates the values from the given object. An object can be of one of the following two types:
Objects are usually either modules or classes. from_object() loads only the uppercase attributes of the module/class. A dict object will not work with from_object() because the keys of a dict are not attributes of the dict class.
Example of module-based configuration:
app.config.from_object('yourapplication.default_config')
from yourapplication import default_config
app.config.from_object(default_config)
You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with from_pyfile() and ideally from a location not within the package because the package might be installed system wide.
See Development / Production for an example of class-based configuration using from_object().
| Parameters: | obj – an import name or object |
|---|
from_pyfile(filename, silent=False)Updates the values in the config from a Python file. This function behaves as if the file was imported as module with the from_object() function.
| Parameters: |
|
|---|
New in version 0.7: silent parameter.
get_namespace(namespace, lowercase=True, trim_namespace=True)Returns a dictionary containing a subset of configuration options that match the specified namespace/prefix. Example usage:
app.config['IMAGE_STORE_TYPE'] = 'fs'
app.config['IMAGE_STORE_PATH'] = '/var/app/images'
app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'
image_store_config = app.config.get_namespace('IMAGE_STORE_')
The resulting dictionary image_store_config would look like:
{
'type': 'fs',
'path': '/var/app/images',
'base_url': 'http://img.website.com'
}
This is often useful when configuration options map directly to keyword arguments in functions or class constructors.
| Parameters: |
|
|---|
New in version 0.11.