Performance: Configuration management
This affects the app startup time and memory usage by managing how configuration data is loaded and accessed.
import os class Config: SECRET_KEY = os.getenv('SECRET_KEY', 'default') DEBUG = False app.config.from_object(Config) # Use environment variables and simple config classes to keep config lightweight
app.config.from_pyfile('config.py') # config.py contains heavy computations or large data structures # Configuration is loaded synchronously at startup with heavy processing
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous config file loading | 0 | 0 | Blocks initial server response causing delayed LCP | [X] Bad |
| Lightweight config via environment variables and classes | 0 | 0 | Minimal startup delay, faster initial response | [OK] Good |
app.config in a Flask application?app.configapp.config is a special dictionary in Flask used to keep app settings organized.app.config.DEBUG to True in Flask?app.config['KEY'] = value.app = Flask(__name__)
app.config['DEBUG'] = False
app.config.from_mapping({'DEBUG': True, 'TESTING': True})
print(app.config['DEBUG'])DEBUG is set to False. Then from_mapping updates it to True.DEBUG becomes True.app = Flask(__name__)
app.config.from_pyfile('config.py')
app.config['SECRET_KEY'] = 'mysecret'config.py does not exist in the project folder.from_pyfile tries to load a Python file and raises an error if missing.config.py does not exist, a FileNotFoundError occurs immediately.os.getenv('KEY', default) returns the environment value or default if missing.os.getenv with default False, correctly providing fallback. app.config['DEBUG'] = os.environ['DEBUG'] or False may raise KeyError if 'DEBUG' missing. app.config['DEBUG'] = os.environ.get('DEBUG') returns None if missing but no default. app.config['DEBUG'] = os.getenv('DEBUG') or None returns None if missing, not False.Pass the test and this pattern moves unknown