quart.config module#

class quart.config.Config(root_path: Union[bytes, str, 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) bool#

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.

filename = os.environ['CONFIG']
from_file(filename: str, load: Callable[[IO[Any]], Mapping], silent: bool = False) bool#

Load the configuration from a data file.

This allows configuration to be loaded as so

app.config.from_file('config.toml', toml.load)
app.config.from_file('config.json', json.load)
  • filename – The filename which when appended to root_path gives the path to the file.

  • load – Callable that takes a file descriptor and returns a mapping loaded from the file.

  • silent – If True any errors will fail silently.

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

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'}
  • 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,

from module import instance

are valid.


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

from_prefixed_env(prefix: str = 'QUART', *, loads: ~typing.Callable[[str], ~typing.Any] = <function loads>) bool#

Load any environment variables that start with the prefix.

The prefix (default QUART_) is dropped from the env key for the config key. Values are passed through a loading function to attempt to convert them to more specific types than strings.

Keys are loaded in sorted() order.

The default loading function attempts to parse values as any valid JSON type, including dicts and lists. Specific items in nested dicts can be set by separating the keys with double underscores (__). If an intermediate key doesn’t exist, it will be initialized to an empty dict.

  • prefix – Load env vars that start with this prefix, separated with an underscore (_).

  • loads – Pass each string value to this function and use the returned value as the config value. If any error is raised it is ignored and the value remains a string. The default is json.loads().

from_pyfile(filename: str, silent: bool = False) bool#

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


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

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}
assert app.config.get_namespace('FOO_') == {'a': 'a', 'bar': 'bar'}
  • 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']