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
| Attribute | Type | Description |
|---|---|---|
| root_path | `str | os.PathLike[str]` |
Constructor
Signature
def Config(
root_path: str | os.PathLike[str],
defaults: dict[str, t.Any]| None = None
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| root_path | `str | os.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
| Name | Type | Description |
|---|---|---|
| variable_name | str | The name of the environment variable that contains the path to the configuration file |
| silent | bool = False | Set to True if you want silent failure for missing files. |
Returns
| Type | Description |
|---|---|
bool | True 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
| Name | Type | Description |
|---|---|---|
| prefix | str = "FLASK" | Load env vars that start with this prefix, separated with an underscore (_). |
| loads | t.Callable[[str], t.Any] = json.loads | Pass each string value to this function and use the returned value as the config value. |
Returns
| Type | Description |
|---|---|
bool | Always 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
| Name | Type | Description |
|---|---|---|
| filename | `str | os.PathLike[str]` |
| silent | bool = False | Set to True if you want silent failure for missing files. |
Returns
| Type | Description |
|---|---|
bool | True 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
| Name | Type | Description |
|---|---|---|
| obj | `object | str` |
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
| Name | Type | Description |
|---|---|---|
| filename | `str | os.PathLike[str]` |
| load | t.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. |
| silent | bool = False | Ignore the file if it doesn't exist. |
| text | bool = True | Open the file in text or binary mode. |
Returns
| Type | Description |
|---|---|
bool | True 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
| Name | Type | Description |
|---|---|---|
| mapping | `t.Mapping[str, t.Any] | None` = None |
| kwargs | t.Any | Arbitrary keyword arguments to be used as configuration values |
Returns
| Type | Description |
|---|---|
bool | Always 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
| Name | Type | Description |
|---|---|---|
| namespace | str | A configuration namespace string to filter keys by |
| lowercase | bool = True | A flag indicating if the keys of the resulting dictionary should be lowercase |
| trim_namespace | bool = True | A flag indicating if the keys of the resulting dictionary should not include the namespace |
Returns
| Type | Description |
|---|---|
dict[str, t.Any] | A dictionary containing the filtered configuration keys, optionally lowercased and with the namespace prefix removed |