Home / Function/ from_prefixed_env() — flask Function Reference

from_prefixed_env() — flask Function Reference

Architecture documentation for the from_prefixed_env() function in config.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  e5ada208_dbe0_c092_5c6e_6d8eff3adee0["from_prefixed_env()"]
  1e067a59_9e9f_8edb_0434_65b5c5690e36["Config"]
  e5ada208_dbe0_c092_5c6e_6d8eff3adee0 -->|defined in| 1e067a59_9e9f_8edb_0434_65b5c5690e36
  9f175f92_04cc_1b7c_fbf0_a4e8f8a10306["loads()"]
  e5ada208_dbe0_c092_5c6e_6d8eff3adee0 -->|calls| 9f175f92_04cc_1b7c_fbf0_a4e8f8a10306
  style e5ada208_dbe0_c092_5c6e_6d8eff3adee0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/config.py lines 126–185

    def from_prefixed_env(
        self, 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.

        Keys are loaded in :func:`sorted` order.

        The default loading function attempts to parse values as any
        valid JSON type, including dicts and lists.

        Specific items in nested dicts can be set by separating the
        keys with double underscores (``__``). If an intermediate key
        doesn't exist, it will be initialized to an empty dict.

        :param prefix: Load env vars that start with this prefix,
            separated with an underscore (``_``).
        :param loads: Pass each string value to this function and use
            the returned value as the config value. If any error is
            raised it is ignored and the value remains a string. The
            default is :func:`json.loads`.

        .. versionadded:: 2.1
        """
        prefix = f"{prefix}_"

        for key in sorted(os.environ):
            if not key.startswith(prefix):
                continue

            value = os.environ[key]
            key = key.removeprefix(prefix)

            try:
                value = loads(value)
            except Exception:
                # Keep the value as a string if loading failed.
                pass

            if "__" not in key:
                # A non-nested key, set directly.
                self[key] = value
                continue

            # Traverse nested dictionaries with keys separated by "__".
            current = self
            *parts, tail = key.split("__")

            for part in parts:
                # If an intermediate dict does not exist, create it.
                if part not in current:
                    current[part] = {}

                current = current[part]

            current[tail] = value

        return True

Subdomains

Defined In

Calls

Frequently Asked Questions

What does from_prefixed_env() do?
from_prefixed_env() is a function in the flask codebase, defined in src/flask/config.py.
Where is from_prefixed_env() defined?
from_prefixed_env() is defined in src/flask/config.py at line 126.
What does from_prefixed_env() call?
from_prefixed_env() calls 1 function(s): loads.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free