Challenge - 5 Problems
Flask Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask app factory code?
Consider this Flask application factory code snippet. What will be printed when the app runs and the root URL is accessed?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Hello from factory!' print('App created') return app app = create_app() if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Remember when the print statement inside the factory function runs.
✗ Incorrect
The print statement runs once when create_app() is called, which happens before the server starts. The route returns the string when accessed.
❓ state_output
intermediate1:30remaining
What is the value of app.config['DEBUG'] after app creation?
Given this factory function, what will be the value of
app.config['DEBUG'] after calling create_app()?Flask
from flask import Flask def create_app(): app = Flask(__name__) app.config['DEBUG'] = True return app app = create_app()
Attempts:
2 left
💡 Hint
Check how config values are set inside the factory.
✗ Incorrect
The config key 'DEBUG' is explicitly set to True inside the factory before returning the app.
🔧 Debug
advanced2:30remaining
Why does this factory pattern code raise an error?
Examine this code. Why does it raise an error when running?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return message message = 'Hello from factory' return app app = create_app() if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Look at when 'message' is defined versus when it is used inside the route.
✗ Incorrect
The route function tries to return message before it is assigned, causing a NameError.
🧠 Conceptual
advanced1:30remaining
Which statement about Flask application factory pattern is true?
Select the correct statement about the Flask application factory pattern.
Attempts:
2 left
💡 Hint
Think about why factories are useful in software design.
✗ Incorrect
The factory pattern lets you create multiple app instances with different settings, useful for testing or different environments.
📝 Syntax
expert2:30remaining
Which option correctly registers a blueprint inside an app factory?
Given a blueprint
bp, which code snippet correctly registers it inside the Flask application factory?Flask
from flask import Flask, Blueprint bp = Blueprint('bp', __name__) @bp.route('/hello') def hello(): return 'Hello Blueprint!' # Choose the correct factory code to register bp
Attempts:
2 left
💡 Hint
Check the Flask method name for blueprint registration.
✗ Incorrect
The correct method to register a blueprint on the app is app.register_blueprint(bp).