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 | Whether to suppress errors and return False if the environment variable is not set or the file is missing |
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 | The prefix used to filter environment variables, which will be stripped from the resulting config keys |
| loads | t.Callable[[str], t.Any] = json.loads | A function used to deserialize the environment variable string value into a Python object |
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 | Whether to suppress OSError exceptions if the file does not exist |
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. An object can be of one of the following two types: a string (imported as a module) or an actual object reference.
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 accepts a file handle and returns a dictionary-like mapping of configuration values |
| silent | bool = False | Whether to ignore errors if the file is missing from the filesystem |
| text | bool = True | Whether to open the file in text mode ('r') or binary mode ('rb') |
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 added to the configuration |
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 | The prefix string used to filter configuration keys |
| lowercase | bool = True | Whether to convert the resulting dictionary keys to lowercase |
| trim_namespace | bool = True | Whether to remove the namespace prefix from the resulting dictionary keys |
Returns
| Type | Description |
|---|---|
dict[str, t.Any] | A dictionary containing the filtered configuration keys, optionally transformed |