0
0
Flaskframework~20 mins

Application factory pattern in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does the Flask app factory function return?
Consider this Flask app factory function:
def create_app():
    app = Flask(__name__)
    @app.route('/')
    def home():
        return 'Hello!'
    return app

What does create_app() return?
Flask
def create_app():
    app = Flask(__name__)
    @app.route('/')
    def home():
        return 'Hello!'
    return app
AA function that returns 'Hello!'
BA Flask application instance ready to run
CNone
DA string 'Hello!'
Attempts:
2 left
💡 Hint
Think about what the variable 'app' is inside the function.
state_output
intermediate
1:30remaining
What is the output when running the app created by the factory?
Given this Flask app factory:
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())
AHello!
B404 Not Found
CHi there!
DTypeError
Attempts:
2 left
💡 Hint
Check the route defined inside the factory.
lifecycle
advanced
1: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 app
Flask
def create_app():
    app = Flask(__name__)
    # setup code
    return app
AEach time create_app() is called
BOnly once when the module is imported
CWhen the server starts automatically
DWhen the first request is received
Attempts:
2 left
💡 Hint
Think about what happens inside the function.
📝 Syntax
advanced
1: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 app
Flask
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
Aregister_blueprint(app, bp)
Bbp.register_blueprint(app)
Capp.blueprint_register(bp)
Dapp.register_blueprint(bp)
Attempts:
2 left
💡 Hint
Check the Flask app method to add blueprints.
🔧 Debug
expert
2:00remaining
Why does this app factory cause a runtime error?
Examine this code:
def create_app():
    app = Flask(__name__)
    @app.route('/count')
    def count():
        return str(counter)
    counter = 0
    return app

What 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
ANameError because 'counter' is used before assignment
BAttributeError because 'counter' is not an attribute
CNo error, returns '0'
DTypeError because 'counter' is not a string
Attempts:
2 left
💡 Hint
The route handler function body executes at request time, after the factory function has fully executed including the assignment.