sanic.config.Config#

Configuration object for Sanic.

Inherits from: dict

class Config(defaults: Optional[Dict[str, Union[str, bool, int, float, None]]] = None, env_prefix: Optional[str] = SANIC_, keep_alive: Optional[bool] = None, converters: Optional[Sequence[Callable[[str], Any]]] = None)

You can use this object to both: (1) configure how Sanic will operate, and (2) manage your application's custom configuration values.

load#

Update app.config.

def load(self, config: Union[bytes, str, dict, Any])

Note

Only upper case settings are considered

See Configuration for more details.

Parameters
config
Union[bytes, str, dict, Any]

Path to py file holding settings, dict holding settings, or any object holding settings.

Examples

You can upload app config by providing path to py file holding settings.

# /some/py/file
A = 1
B = 2
config.update_config("${some}/py/file")

Yes you can put environment variable here, but they must be provided in format: ${some_env_var}, and mark that $some_env_var is treated as plain string.

You can upload app config by providing dict holding settings.

d = {"A": 1, "B": 2}
config.update_config(d)

You can upload app config by providing any object holding settings, but in such case config.dict will be used as dict holding settings.

class C:
    A = 1
    B = 2

config.update_config(C)

load_environment_vars#

Load environment variables into the config.

def load_environment_vars(self, prefix = SANIC_)

Looks for prefixed environment variables and applies them to the configuration if present. This is called automatically when Sanic starts up to load environment variables into config. Environment variables should start with the defined prefix and should only contain uppercase letters.

It will automatically hydrate the following types:

  • int
  • float
  • bool

Anything else will be imported as a str. If you would like to add additional types to this list, you can use sanic.config.Config.register_type. Just make sure that they are registered before you instantiate your application.

You likely won't need to call this method directly.

See Configuration for more details.

Parameters
prefix
str

The prefix to use when looking for environment variables. Defaults to SANIC_.

Examples
# Environment variables
# SANIC_SERVER_NAME=example.com
# SANIC_SERVER_PORT=9999
# SANIC_SERVER_AUTORELOAD=true

# Python
app.config.load_environment_vars()

register_type#

Register a custom type converter.

def register_type(self, converter: Callable[[str], Any]): -> None

Allows for adding custom function to cast from a string value to any other type. The function should raise ValueError if it is not the correct type.

Parameters
converter
Callable[[str], Any]

A function that takes a string and returns a value of any type.

Examples
def my_converter(value: str) -> Any:
    # Do something to convert the value
    return value

config.register_type(my_converter)

update#

Update the config with new values.

def update(self, other: Any, kwargs: Any): -> None

This method will update the config with the values from the provided other objects, and then update the config with the provided kwargs. The other objects can be any object that can be converted to a dictionary, such as a dict, Config object, or str path to a Python file. The kwargs must be a dictionary of key-value pairs.

Note

Only upper case settings are considered

Parameters
*other

Any number of objects that can be converted to a dictionary.

**kwargs

Any number of key-value pairs.

Raises
AttributeError

If a key is not in the config.

Examples
config.update(
    {"A": 1, "B": 2},
    {"C": 3, "D": 4},
    E=5,
    F=6,
)

update_config#

Update app.config.

def update_config(self, config: Union[bytes, str, dict, Any])

Note

Only upper case settings are considered

See Configuration for more details.

Parameters
config
Union[bytes, str, dict, Any]

Path to py file holding settings, dict holding settings, or any object holding settings.

Examples

You can upload app config by providing path to py file holding settings.

# /some/py/file
A = 1
B = 2
config.update_config("${some}/py/file")

Yes you can put environment variable here, but they must be provided in format: ${some_env_var}, and mark that $some_env_var is treated as plain string.

You can upload app config by providing dict holding settings.

d = {"A": 1, "B": 2}
config.update_config(d)

You can upload app config by providing any object holding settings, but in such case config.dict will be used as dict holding settings.

class C:
    A = 1
    B = 2

config.update_config(C)

sanic.config.DescriptorMeta#

Metaclass for Config.

Inherits from: ABCMeta

class DescriptorMeta(_)

register#

Register a virtual subclass of an ABC.

def register(cls, subclass)

Returns the subclass, to allow usage as a class decorator.