MCPcopy
hub / github.com/pallets/flask / from_envvar

Method from_envvar

src/flask/config.py:102–124  ·  view source on GitHub ↗

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']) :param variable_name: name of

(self, variable_name: str, silent: bool = False)

Source from the content-addressed store, hash-verified

100 self.root_path = root_path
101
102 def from_envvar(self, variable_name: str, silent: bool = False) -> bool:
103 """Loads a configuration from an environment variable pointing to
104 a configuration file. This is basically just a shortcut with nicer
105 error messages for this line of code::
106
107 app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
108
109 :param variable_name: name of the environment variable
110 :param silent: set to ``True`` if you want silent failure for missing
111 files.
112 :return: ``True`` if the file was loaded successfully.
113 """
114 rv = os.environ.get(variable_name)
115 if not rv:
116 if silent:
117 return False
118 raise RuntimeError(
119 f"The environment variable {variable_name!r} is not set"
120 " and as such configuration could not be loaded. Set"
121 " this variable and make it point to a configuration"
122 " file"
123 )
124 return self.from_pyfile(rv, silent=silent)
125
126 def from_prefixed_env(
127 self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads

Callers 2

test_config_from_envvarFunction · 0.80

Calls 2

from_pyfileMethod · 0.95
getMethod · 0.45

Tested by 2

test_config_from_envvarFunction · 0.64