Challenge - 5 Problems
Flask Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does the Flask app factory function return?
Consider this Flask app factory function:
What does
def create_app():
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello!'
return appWhat does
create_app() return?Flask
def create_app(): app = Flask(__name__) @app.route('/') def home(): return 'Hello!' return app
Attempts:
2 left
💡 Hint
Think about what the variable 'app' is inside the function.
✗ Incorrect
The function creates a Flask app instance, sets a route, and returns the app instance itself. It does not return the route function or its output.
❓ state_output
intermediate1:30remaining
What is the output when running the app created by the factory?
Given this Flask app factory:
What will be printed?
def create_app():
app = Flask(__name__)
@app.route('/greet')
def greet():
return 'Hi there!'
return app
app = create_app()
with app.test_client() as client:
response = client.get('/greet')
print(response.data.decode())What will be printed?
Flask
def create_app(): app = Flask(__name__) @app.route('/greet') def greet(): return 'Hi there!' return app app = create_app() with app.test_client() as client: response = client.get('/greet') print(response.data.decode())
Attempts:
2 left
💡 Hint
Check the route defined inside the factory.
✗ Incorrect
The route '/greet' returns 'Hi there!'. The test client requests this route and prints the response data decoded to string.
❓ lifecycle
advanced1:30remaining
When is the Flask app instance created in the factory pattern?
In the application factory pattern, when does the Flask app instance get created?
def create_app():
app = Flask(__name__)
# setup code
return appFlask
def create_app(): app = Flask(__name__) # setup code return app
Attempts:
2 left
💡 Hint
Think about what happens inside the function.
✗ Incorrect
The app instance is created inside the function, so every call to create_app() makes a new app instance.
📝 Syntax
advanced1:30remaining
Which option correctly registers a blueprint inside the factory?
You want to register a blueprint inside the Flask app factory. Which code snippet is correct?
from flask import Flask, Blueprint
bp = Blueprint('bp', __name__)
@bp.route('/hello')
def hello():
return 'Hello Blueprint!'
def create_app():
app = Flask(__name__)
# register blueprint here
return appFlask
from flask import Flask, Blueprint bp = Blueprint('bp', __name__) @bp.route('/hello') def hello(): return 'Hello Blueprint!' def create_app(): app = Flask(__name__) # register blueprint here return app
Attempts:
2 left
💡 Hint
Check the Flask app method to add blueprints.
✗ Incorrect
The Flask app instance has a method register_blueprint to add blueprints. The blueprint does not register the app.
🔧 Debug
expert2:00remaining
Why does this app factory cause a runtime error?
Examine this code:
What error occurs when accessing '/count' and why?
def create_app():
app = Flask(__name__)
@app.route('/count')
def count():
return str(counter)
counter = 0
return appWhat error occurs when accessing '/count' and why?
Flask
def create_app(): app = Flask(__name__) @app.route('/count') def count(): return str(counter) counter = 0 return app
Attempts:
2 left
💡 Hint
The route handler function body executes at request time, after the factory function has fully executed including the assignment.
✗ Incorrect
A NameError occurs because the 'count' function tries to access 'counter' before it is assigned. The function 'count' is defined before 'counter' is assigned, so 'counter' is not in the closure at the time of function definition.