quart.config module

class quart.config.Config(root_path: Union[bytes, str, os.PathLike], defaults: Optional[dict] = None)

Bases: dict

Extends a standard Python dictionary with additional load (from) methods.

Note that the convention (as enforced when loading) is that configuration keys are upper case. Whilst you can set lower case keys it is not recommended.

from_envvar(variable_name: str, silent: bool = False) → None

Load the configuration from a location specified in the environment.

This will load a cfg file using from_pyfile() from the location specified in the environment, for example the two blocks below are equivalent.

app.config.from_envvar('CONFIG')
filename = os.environ['CONFIG']
app.config.from_pyfile(filename)
from_json(filename: str, silent: bool = False) → None

Load the configuration values from a JSON formatted file.

This allows configuration to be loaded as so

app.config.from_json('config.json')
Parameters
  • filename – The filename which when appended to root_path gives the path to the file.

  • silent – If True any errors will fail silently.

from_mapping(mapping: Optional[Mapping[str, Any]] = None, **kwargs: Any) → None

Load the configuration values from a mapping.

This allows either a mapping to be directly passed or as keyword arguments, for example,

config = {'FOO': 'bar'}
app.config.from_mapping(config)
app.config.form_mapping(FOO='bar')
Parameters
  • mapping – Optionally a mapping object.

  • kwargs – Optionally a collection of keyword arguments to form a mapping.

from_object(instance: Union[object, str]) → None

Load the configuration from a Python object.

This can be used to reference modules or objects within modules for example,

app.config.from_object('module')
app.config.from_object('module.instance')
from module import instance
app.config.from_object(instance)

are valid.

Parameters

instance – Either a str referencing a python object or the object itself.

from_pyfile(filename: str, silent: bool = False) → None

Load the configuration from a Python cfg or py file.

See Python’s ConfigParser docs for details on the cfg format. It is a common practice to load the defaults from the source using the from_object() and then override with a cfg or py file, for example

app.config.from_object('config_module')
app.config.from_pyfile('production.cfg')
Parameters

filename – The filename which when appended to root_path gives the path to the file

from_toml(filename: str, silent: bool = False) → None

Load the configuration values from a TOML formatted file.

This allows configuration to be loaded as so

app.config.from_toml('config.toml')
Parameters
  • filename – The filename which when appended to root_path gives the path to the file.

  • silent – If True any errors will fail silently.

get_namespace(namespace: str, lowercase: bool = True, trim_namespace: bool = True) → Dict[str, Any]

Return a dictionary of keys within a namespace.

A namespace is considered to be a key prefix, for example the keys FOO_A, FOO_BAR, FOO_B are all within the FOO namespace. This method would return a dictionary with these keys and values present.

config = {'FOO_A': 'a', 'FOO_BAR': 'bar', 'BAR': False}
app.config.from_mapping(config)
assert app.config.get_namespace('FOO_') == {'a': 'a', 'bar': 'bar'}
Parameters
  • namespace – The namespace itself (should be uppercase).

  • lowercase – Lowercase the keys in the returned dictionary.

  • trim_namespace – Remove the namespace from the returned keys.

class quart.config.ConfigAttribute(key: str, converter: Optional[Callable] = None)

Bases: object

Implements a property descriptor for objects with a config attribute.

When used as a class instance it will look up the key on the class config object, for example:

class Object:
    config = {}
    foo = ConfigAttribute('foo')

obj = Object()
obj.foo = 'bob'
assert obj.foo == obj.config['foo']