0
0
Flaskframework~20 mins

Application factory pattern preview 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 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
AA function that returns 'Hello from factory!'
BA Flask app instance ready to run
CA string 'Hello from factory!'
DNone
Attempts:
2 left
💡 Hint
Remember, the factory function creates and returns the Flask app object.
📝 Syntax
intermediate
2: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
AMissing parentheses in Flask(__name__)
BIndentation error in the home function
CNo syntax error
DMissing colon after def home()
Attempts:
2 left
💡 Hint
Check each line carefully for missing punctuation or indentation.
state_output
advanced
2: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()
AFactory says hi!
B500 Internal Server Error
C404 Not Found error
DHello from factory!
Attempts:
2 left
💡 Hint
Look at the return value of the home route inside the factory.
🔧 Debug
advanced
2: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()
AThe factory function does not return the app instance
BMissing parentheses in app.run
CRoute decorator is outside the factory function
DFlask is not imported
Attempts:
2 left
💡 Hint
Check if the create_app function returns the app object.
🧠 Conceptual
expert
2:00remaining
Why use the application factory pattern in Flask?
Which option best explains the main advantage of using the application factory pattern in Flask?
AIt removes the need to import Flask in your code
BIt makes the app run faster by precompiling routes
CIt automatically generates HTML templates for routes
DIt allows creating multiple app instances with different configurations easily
Attempts:
2 left
💡 Hint
Think about flexibility and testing benefits.