📖 Scenario: You are building a simple Flask web app that can be created using a factory function. This pattern helps you make multiple app instances easily and keeps your code clean.
🎯 Goal: Create a Flask application using the application factory pattern. You will first set up the basic app creation function, then configure it, add a simple route, and finally run the app.
📋 What You'll Learn
Create a function called create_app that returns a Flask app instance
Inside create_app, create the Flask app with Flask(__name__)
Add a configuration variable DEBUG set to True inside create_app
Add a route / that returns the text 'Hello, Flask!'
Return the app instance from create_app
💡 Why This Matters
🌍 Real World
The application factory pattern is used in real Flask projects to create multiple app instances with different settings, useful for testing and deployment.
💼 Career
Understanding this pattern is important for backend developers working with Flask, as it is a common best practice in professional Flask applications.
Progress0 / 4 steps
1
Create the application factory function
Write a function called create_app that creates a Flask app instance with Flask(__name__) and returns it.
Flask
Hint
Remember to import Flask first. Then define create_app and create the app inside it.
2
Add configuration to the app
Inside the create_app function, add a configuration variable DEBUG set to True on the app.config dictionary.
Flask
Hint
Use app.config['DEBUG'] = True to enable debug mode.
3
Add a route to the app
Inside the create_app function, add a route for / using @app.route('/'). Define a function called home that returns the string 'Hello, Flask!'.
Flask
Hint
Use the @app.route('/') decorator and define a function that returns the greeting.
4
Run the app using the factory
Outside the create_app function, create an app instance by calling create_app(). Then add the block if __name__ == '__main__': and inside it call app.run() to start the server.
Flask
Hint
Create the app by calling create_app() and run it inside the main guard.
Practice
(1/5)
1. What is the main purpose of the application factory pattern in Flask?
easy
A. To avoid using blueprints and extensions
B. To write all routes directly in the main script without functions
C. To automatically generate HTML templates
D. To create the Flask app inside a function for better configuration and testing
Solution
Step 1: Understand the pattern's purpose
The application factory pattern creates the Flask app inside a function instead of a global variable.
Step 2: Benefits of this approach
This allows flexible configuration, easier testing, and clean registration of extensions and blueprints.
Final Answer:
To create the Flask app inside a function for better configuration and testing -> Option D
Quick Check:
Application factory = create app in function [OK]
Hint: Remember: factory means 'make inside a function' [OK]
Common Mistakes:
Thinking app is created globally
Confusing with blueprint usage
Assuming it auto-generates templates
2. Which of the following is the correct way to define an application factory function in Flask?
easy
A. def create_app():\n app = Flask\n return app
B. app = Flask(__name__)\ndef create_app():\n return app
C. def create_app():\n app = Flask(__name__)\n return app
D. def create_app():\n return Flask
Solution
Step 1: Check function definition and app creation
The factory function must create a Flask instance inside it using Flask(__name__)
Step 2: Verify return of the app instance
The function should return the created app instance, not the class or a global variable.
Final Answer:
def create_app():\n app = Flask(__name__)\n return app -> Option C
Quick Check:
Factory returns new Flask instance [OK]
Hint: Create and return app inside function with Flask(__name__) [OK]
Common Mistakes:
Returning Flask class instead of instance
Using global app outside function
Not calling Flask with parentheses
3. Given this code snippet, what will print(app.config['DEBUG']) output?