Skip to main content

Config

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.

Attributes

AttributeTypeDescription
root_path`stros.PathLike[str]`

Constructor

Signature

def Config(
root_path: str | os.PathLike[str],
defaults: dict[str, t.Any]| None = None
) - > None

Parameters

NameTypeDescription
root_path`stros.PathLike[str]`
defaults`dict[str, t.Any]None` = None

Methods


from_envvar()

@classmethod
def from_envvar(
variable_name: str,
silent: bool = False
) - > bool

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

NameTypeDescription
variable_namestrThe name of the environment variable that contains the path to the configuration file
silentbool = FalseWhether to suppress errors and return False if the environment variable is not set or the file is missing

Returns

TypeDescription
boolTrue if the file was loaded successfully.

from_prefixed_env()

@classmethod
def from_prefixed_env(
prefix: str = FLASK,
loads: t.Callable[[str], t.Any] = json.loads
) - > bool

Load any environment variables that start with FLASK_, dropping the prefix 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.

Parameters

NameTypeDescription
prefixstr = FLASKThe prefix used to filter environment variables, which will be stripped from the resulting config keys
loadst.Callable[[str], t.Any] = json.loadsA function used to deserialize the environment variable string value into a Python object

Returns

TypeDescription
boolAlways returns True after processing matching environment variables

from_pyfile()

@classmethod
def from_pyfile(
filename: str | os.PathLike[str],
silent: bool = False
) - > bool

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

NameTypeDescription
filename`stros.PathLike[str]`
silentbool = FalseWhether to suppress OSError exceptions if the file does not exist

Returns

TypeDescription
boolTrue if the file was loaded successfully.

from_object()

@classmethod
def from_object(
obj: object | str
)

Updates the values from the given object. An object can be of one of the following two types: a string (imported as a module) or an actual object reference.

Parameters

NameTypeDescription
obj`objectstr`

from_file()

@classmethod
def from_file(
filename: str | os.PathLike[str],
load: t.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]],
silent: bool = False,
text: bool = True
) - > bool

Update the values in the config from a file that is loaded using the load parameter. The loaded data is passed to the from_mapping method.

Parameters

NameTypeDescription
filename`stros.PathLike[str]`
loadt.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]]A callable that accepts a file handle and returns a dictionary-like mapping of configuration values
silentbool = FalseWhether to ignore errors if the file is missing from the filesystem
textbool = TrueWhether to open the file in text mode ('r') or binary mode ('rb')

Returns

TypeDescription
boolTrue if the file was loaded successfully.

from_mapping()

@classmethod
def from_mapping(
mapping: t.Mapping[str, t.Any]| None = None,
kwargs: t.Any
) - > bool

Updates the config like update ignoring items with non-upper keys.

Parameters

NameTypeDescription
mapping`t.Mapping[str, t.Any]None` = None
kwargst.AnyArbitrary keyword arguments to be added to the configuration

Returns

TypeDescription
boolAlways returns True.

get_namespace()

@classmethod
def get_namespace(
namespace: str,
lowercase: bool = True,
trim_namespace: bool = True
) - > dict[str, t.Any]

Returns a dictionary containing a subset of configuration options that match the specified namespace/prefix.

Parameters

NameTypeDescription
namespacestrThe prefix string used to filter configuration keys
lowercasebool = TrueWhether to convert the resulting dictionary keys to lowercase
trim_namespacebool = TrueWhether to remove the namespace prefix from the resulting dictionary keys

Returns

TypeDescription
dict[str, t.Any]A dictionary containing the filtered configuration keys, optionally transformed