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 does the Flask app factory return?
Consider this Flask application factory function. What does it return when called?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Hello from factory!' return app
Attempts:
2 left
💡 Hint
Remember, the factory function creates and returns the Flask app object.
✗ Incorrect
The create_app function builds a Flask app instance, sets up routes, and returns the app object itself. This object can then be run by the server.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this factory pattern code
Which option shows the syntax error in this Flask app factory snippet?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Welcome!' return app
Attempts:
2 left
💡 Hint
Check each line carefully for missing punctuation or indentation.
✗ Incorrect
The code is correctly indented and all syntax is valid. The factory function returns the app instance properly.
❓ state_output
advanced2:00remaining
What is the output when running the app created by this factory?
Given this Flask app factory, what will be the output when accessing '/' route?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Factory says hi!' return app app = create_app()
Attempts:
2 left
💡 Hint
Look at the return value of the home route inside the factory.
✗ Incorrect
The '/' route returns the string 'Factory says hi!'. This is what the browser will display.
🔧 Debug
advanced2:00remaining
Why does this factory app raise an error on run?
This Flask app factory code raises an error when running. What is the cause?
Flask
from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Hi!' app = create_app() if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Check if the create_app function returns the app object.
✗ Incorrect
The create_app function does not return the app instance, so app is None. Calling app.run() causes an AttributeError.
🧠 Conceptual
expert2:00remaining
Why use the application factory pattern in Flask?
Which option best explains the main advantage of using the application factory pattern in Flask?
Attempts:
2 left
💡 Hint
Think about flexibility and testing benefits.
✗ Incorrect
The factory pattern lets you create multiple Flask app instances with different settings, which helps in testing and modular design.