Recall & Review
beginner
What is the purpose of the Flask extension initialization pattern?
It allows you to create an extension instance separately and initialize it later with a Flask app. This helps organize code and supports multiple app instances.
Click to reveal answer
beginner
How do you create a Flask extension instance without binding it to an app immediately?
You create the extension object by calling its constructor without passing the app, for example:
db = SQLAlchemy().Click to reveal answer
beginner
What method do you call to bind a Flask extension instance to an app later?
You call the
init_app(app) method on the extension instance, passing the Flask app object.Click to reveal answer
intermediate
Why is the extension initialization pattern useful in Flask applications?
It supports creating extensions before the app exists, helps with testing, and allows sharing one extension instance across multiple apps.
Click to reveal answer
beginner
Show a simple example of the Flask extension initialization pattern using SQLAlchemy.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() # create extension instance
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db.init_app(app) # initialize with app
return appClick to reveal answer
What does the
init_app() method do in Flask extensions?✗ Incorrect
The init_app() method connects the extension instance to a Flask app, allowing it to work with that app.
Why might you create a Flask extension instance without passing the app immediately?
✗ Incorrect
Creating the extension instance first allows you to initialize it later with different apps, useful in app factories and testing.
Which of these is a benefit of the extension initialization pattern?
✗ Incorrect
This pattern allows one extension instance to be used with many apps by calling init_app() multiple times.
In the extension initialization pattern, when is the extension instance typically created?
✗ Incorrect
The extension instance is created first, then later initialized with the app.
What is the common name for the Flask pattern that uses
init_app()?✗ Incorrect
This pattern is called the extension initialization pattern because it initializes extensions separately from app creation.
Explain the Flask extension initialization pattern and why it is useful.
Think about how you can prepare an extension before the app exists.
You got /4 concepts.
Write a simple example showing how to use the extension initialization pattern with a Flask app and SQLAlchemy.
Remember to separate extension creation and app creation.
You got /5 concepts.