0
0
Flaskframework~20 mins

Application factory pattern deep dive 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
2: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()
AApp created is printed once before the server starts, and visiting '/' shows 'Hello from factory!'
BApp created is printed every time '/' is accessed, and the page shows 'Hello from factory!'
CNothing is printed, and visiting '/' returns a 404 error
DApp created is printed once, but visiting '/' returns an empty page
Attempts:
2 left
💡 Hint
Remember when the print statement inside the factory function runs.
state_output
intermediate
1: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()
ARaises a KeyError
BFalse
CNone
DTrue
Attempts:
2 left
💡 Hint
Check how config values are set inside the factory.
🔧 Debug
advanced
2: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()
ANameError because 'message' is used before assignment in the route function
BTypeError because 'message' is not a string
CSyntaxError due to incorrect indentation
DNo error, it runs and returns 'Hello from factory'
Attempts:
2 left
💡 Hint
Look at when 'message' is defined versus when it is used inside the route.
🧠 Conceptual
advanced
1:30remaining
Which statement about Flask application factory pattern is true?
Select the correct statement about the Flask application factory pattern.
AIt prevents registering blueprints after app creation
BIt requires using global variables for app state
CIt allows creating multiple app instances with different configurations
DIt disables Flask's debug mode by default
Attempts:
2 left
💡 Hint
Think about why factories are useful in software design.
📝 Syntax
expert
2: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
A
def create_app():
    app = Flask(__name__)
    app.register(bp)
    return app
B
def create_app():
    app = Flask(__name__)
    app.register_blueprint(bp)
    return app
C
def create_app():
    app = Flask(__name__)
    bp.register_blueprint(app)
    return app
D
def create_app():
    app = Flask(__name__)
    app.blueprint_register(bp)
    return app
Attempts:
2 left
💡 Hint
Check the Flask method name for blueprint registration.