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 = FalseSet to True if you want silent failure for missing files.

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 = "FLASK"Load env vars that start with this prefix, separated with an underscore (_).
loadst.Callable[[str], t.Any] = json.loadsPass each string value to this function and use the returned value as the config value.

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 = FalseSet to True if you want silent failure for missing files.

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. Objects are usually either modules or classes. from_object loads only the uppercase attributes of the module/class.

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 takes a file handle and returns a mapping of loaded data from the file.
silentbool = FalseIgnore the file if it doesn't exist.
textbool = TrueOpen the file in text or binary mode.

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 used as configuration values

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
namespacestrA configuration namespace string to filter keys by
lowercasebool = TrueA flag indicating if the keys of the resulting dictionary should be lowercase
trim_namespacebool = TrueA flag indicating if the keys of the resulting dictionary should not include the namespace

Returns

TypeDescription
dict[str, t.Any]A dictionary containing the filtered configuration keys, optionally lowercased and with the namespace prefix removed