Creating application factories
Leveraging a factory pattern is a great way of organizing your application object, allowing for multiple application objects with different settings. As discussed in Chapter 1, it is always possible to create multiple application instances by using different configurations, but application factories allow you to have multiple application objects inside the same application process. It also aids in testing, as you can choose to have a fresh or different application object with different settings for each test case.
Getting ready
We will use our application from the previous recipe and modify it to use the application factory pattern.
How to do it…
The following are the changes that need to be made:
- We will start by creating a function named
create_app()
in ourmy_app/__init__.py
:def create_app(alt_config={}):
app = Flask(__name__, template_folder=alt_config
.get(&apos...