0
0
Flaskframework~30 mins

Application factory pattern preview in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Application factory pattern preview
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Create the app by calling create_app() and run it inside the main guard.