App
Overview
In the context of this chapter, the term app refers to the Flask application instance, which serves as the central hub for a Flask-based application. The app instance is initially created directly in the myblog.py file and is responsible for connecting URL endpoints to their corresponding functionalities. The chapter explores restructuring the application to utilize an application factory function for creating the app instance, enhancing modularity and scalability.
Directory Structure
To organize the application, the app directory is transformed into a Python package by creating an __init__.py file. The directory structure is as follows:
├── app
│ ├── __init__.py
│ ├── static
│ │ ├── css
│ │ │ └── base.css
│ │ ├── images
│ │ │ ├── myblog_banner.png
│ │ │ └── myblog_banner_50.png
│ │ └── js
│ │ └── index.js
│ └── templates
│ ├── base.html
│ └── index.html
└── myblog.py
Application Factory
The myblog.py file initially creates the Flask app instance directly. To gain more control over the creation of the app instance and facilitate its use with external modules, an application factory function named create_app() is implemented within the app/__init__.py file. The code for this function is as follows:
from flask import Flask, render_template
from datetime import datetime
from random import sample
class PageVisit:
COUNT = 0
def counts(self):
PageVisit.COUNT += 1
return PageVisit.COUNT
class BannerColors:
COLORS = [
"lightcoral", "salmon", "red", "firebrick", "pink",
"gold", "yellow", "khaki", "darkkhaki", "violet",
"blue", "purple", "indigo", "greenyellow", "lime",
"green", "olive", "darkcyan", "aqua", "skyblue",
"tan", "sienna", "gray", "silver"
]
def get_colors(self):
return sample(BannerColors.COLORS, 5)
def create_app():
app = Flask(__name__)
with app.app_context():
@app.route("/")
def home():
return render_template("index.html", data={
"now": datetime.now(),
"page_visit": PageVisit(),
"banner_colors": BannerColors().get_colors()
})
return app
This code essentially replicates the functionality of myblog.py within the app package's __init__.py file. Consequently, myblog.py is updated to:
from app import create_app
app = create_app()
Running the Application
To run the application, navigate to the appropriate directory and execute the following commands in a terminal:
For Mac or Linux:
export FLASK_ENV=development
export FLASK_APP=app.py
flask run
For Windows:
set FLASK_ENV=development
set FLASK_APP=app.py
flask run
Accessing 127.0.0.1:5000 in a browser will display the application view using the new MyBlog application structure.
Challenges with the Current Structure
The current MyBlog application setup presents challenges in creating modules to contain features and functionality, as these modules require access to the app instance. Figure 7.5 illustrates the existing application code structure, where features are contained within the app.py file. This setup can lead to circular reference problems, making it difficult to scale the application.
Figure 7.5 The application code structure where features are contained in the app.py file
FAQ (Frequently asked questions)
What does 'app' refer to in the context of Flask?
How is the 'app' instance created in a Flask application?
What is the purpose of the 'app' instance in Flask?
What is an application factory function in Flask?