You want to create a Flask app using the application factory pattern that supports multiple configurations (development and production). Which approach correctly applies this pattern?
hard📝 lifecycle Q15 of 15
Flask - Ecosystem and Patterns
You want to create a Flask app using the application factory pattern that supports multiple configurations (development and production). Which approach correctly applies this pattern?
Step 1: Understand configuration loading in factory
The factory should accept a parameter to select configuration and load it properly using app.config.from_object().
Step 2: Evaluate options
def create_app(config_name):
app = Flask(__name__)
if config_name == 'dev':
app.config.from_object('config.DevConfig')
else:
app.config.from_object('config.ProdConfig')
return app correctly uses a parameter and loads config classes conditionally inside the factory function.
Step 3: Identify incorrect options
app = Flask(__name__)
if config_name == 'dev':
app.config.from_object('config.DevConfig')
else:
app.config.from_object('config.ProdConfig') defines app globally, breaking factory pattern. Options C and D do not load config properly.
Final Answer:
def create_app(config_name):
app = Flask(__name__)
if config_name == 'dev':
app.config.from_object('config.DevConfig')
else:
app.config.from_object('config.ProdConfig')
return app -> Option A
Quick Check:
Factory with config param loads correct settings [OK]
Quick Trick:Pass config name to factory and load config inside [OK]
Common Mistakes:
MISTAKES
Creating app globally instead of inside factory
Assigning config directly instead of loading
Not using parameter to select config
Master "Ecosystem and Patterns" in Flask
9 interactive learning modes - each teaches the same concept differently