Config Class — flask Architecture
Architecture documentation for the Config class in config.py from the flask codebase.
Entity Profile
Dependency Diagram
graph TD 1e067a59_9e9f_8edb_0434_65b5c5690e36["Config"] 2e858a02_7725_54fd_8d55_a9c969ab89bf["config.py"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|defined in| 2e858a02_7725_54fd_8d55_a9c969ab89bf f8f295b1_0f6b_3be2_9cd9_10328a9724cd["__init__()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| f8f295b1_0f6b_3be2_9cd9_10328a9724cd 64c4024a_6f73_9984_501a_d823589da302["from_envvar()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 64c4024a_6f73_9984_501a_d823589da302 e5ada208_dbe0_c092_5c6e_6d8eff3adee0["from_prefixed_env()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| e5ada208_dbe0_c092_5c6e_6d8eff3adee0 992a2a65_31c4_7dbd_d0e9_54f209631cda["from_pyfile()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 992a2a65_31c4_7dbd_d0e9_54f209631cda e968f33d_fca8_c3ea_6616_3fcd3d3dd07c["from_object()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| e968f33d_fca8_c3ea_6616_3fcd3d3dd07c 55ea84cc_e473_fddf_9541_660cc445cb55["from_file()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 55ea84cc_e473_fddf_9541_660cc445cb55 78c05fd2_59f8_47ae_0cf0_811efaed6c49["from_mapping()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 78c05fd2_59f8_47ae_0cf0_811efaed6c49 4547c641_144e_31b0_3192_eceb552bfdef["get_namespace()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 4547c641_144e_31b0_3192_eceb552bfdef 04d594ed_3c68_e531_9c22_b80fd36ed797["__repr__()"] 1e067a59_9e9f_8edb_0434_65b5c5690e36 -->|method| 04d594ed_3c68_e531_9c22_b80fd36ed797
Relationship Graph
Source Code
src/flask/config.py lines 50–367
class Config(dict): # type: ignore[type-arg]
"""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.
Either you can fill the config from a config file::
app.config.from_pyfile('yourconfig.cfg')
Or alternatively you can define the configuration options in the
module that calls :meth:`from_object` or provide an import path to
a module that should be loaded. It is also possible to tell it to
use the same module and with that provide the configuration values
just before the call::
DEBUG = True
SECRET_KEY = 'development key'
app.config.from_object(__name__)
In both cases (loading from any Python file or loading from modules),
only uppercase keys are added to the config. This makes it possible to use
lowercase values in the config file for temporary values that are not added
to the config or to define the config keys in the same file that implements
the application.
Probably the most interesting way to load configurations is from an
environment variable pointing to a file::
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
In this case before launching the application you have to set this
environment variable to the file you want to use. On Linux and OS X
use the export statement::
export YOURAPPLICATION_SETTINGS='/path/to/config/file'
On windows use `set` instead.
:param root_path: path to which files are read relative from. When the
config object is created by the application, this is
the application's :attr:`~flask.Flask.root_path`.
:param defaults: an optional dictionary of default values
"""
def __init__(
self,
root_path: str | os.PathLike[str],
defaults: dict[str, t.Any] | None = None,
) -> None:
super().__init__(defaults or {})
self.root_path = root_path
def from_envvar(self, 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'])
:param variable_name: name of the environment variable
:param silent: set to ``True`` if you want silent failure for missing
files.
:return: ``True`` if the file was loaded successfully.
"""
rv = os.environ.get(variable_name)
if not rv:
if silent:
return False
raise RuntimeError(
f"The environment variable {variable_name!r} is not set"
" and as such configuration could not be loaded. Set"
" this variable and make it point to a configuration"
" file"
)
return self.from_pyfile(rv, silent=silent)
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
Domain
Defined In
Source
Frequently Asked Questions
What is the Config class?
Config is a class in the flask codebase, defined in src/flask/config.py.
Where is Config defined?
Config is defined in src/flask/config.py at line 50.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free