0
0
Flaskframework~5 mins

Application factory pattern deep dive in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Application Factory pattern in Flask?
It is a way to create Flask application instances inside a function. This allows you to configure and initialize the app dynamically, making it easier to manage different environments and testing.
Click to reveal answer
beginner
Why use the Application Factory pattern instead of creating a Flask app globally?
Using the factory pattern helps avoid issues with global state, supports multiple app instances, and improves testing by allowing fresh app creation with different settings each time.
Click to reveal answer
intermediate
How do you register extensions like SQLAlchemy in the Application Factory pattern?
You create the extension object outside the factory function without binding it to an app. Then inside the factory, you call the extension's init_app(app) method to bind it to the current app instance.
Click to reveal answer
intermediate
What is the role of Blueprints in the Application Factory pattern?
Blueprints let you organize your app into reusable components. In the factory pattern, you register blueprints inside the factory function to attach routes and views to the app instance.
Click to reveal answer
beginner
How does the Application Factory pattern improve testing in Flask?
It allows tests to create a new app instance with specific configurations for each test, avoiding shared state and making tests isolated and reliable.
Click to reveal answer
What is the main benefit of using an application factory in Flask?
AWrite less code for routes
BAvoid using Flask extensions
CCreate multiple app instances with different configs
DAutomatically generate HTML templates
Where do you call init_app() for Flask extensions in the factory pattern?
AOutside the factory function
BInside the factory function after creating the app
CIn the main script only
DYou don't call init_app() with the factory pattern
How do Blueprints relate to the application factory pattern?
AThey are not compatible with the factory pattern
BThey replace the factory function
CThey are only used for static files
DThey are registered inside the factory function
Which of these is NOT a benefit of the application factory pattern?
ASimplifies deployment by removing config
BAvoid global app state
CBetter testing support
DSupports multiple app instances
What is a common first step inside an application factory function?
ACreate the Flask app instance
BRegister Blueprints
CRun the app server
DImport Flask extensions
Explain how the Application Factory pattern works in Flask and why it is useful.
Think about how you create the app inside a function instead of globally.
You got /5 concepts.
    Describe how Flask extensions and Blueprints are used within the Application Factory pattern.
    Focus on where and how extensions and Blueprints connect to the app.
    You got /4 concepts.