Flask Project Structure | Beginner's Tutorial
A Flask application can be as simple as a single file.
For example, the following hello.py file is a Flask application:
Example
from flask import Flask
app = Flask( __name__ )
@app.route('/')
def hello():
return 'Hello, World!'
However, as the project grows, it is not practical to put all the code in one file.
Flask project structure can vary depending on the scale and complexity of the application.
Here are several common Flask project structures, suitable for simple applications and more complex applications respectively.
- Simple project structure: Suitable for small applications, with all code concentrated in one file.
- Medium project structure: Divides the application into multiple modules for easier management and expansion.
- Complex project structure: Supports higher modularity, suitable for large applications, including directories for routes, models, templates, and static files.
1. Simple Project Structure
For a simple Flask application, the project structure can be very concise:
my_flask_app/
β
βββ app.py
βββ requirements.txt
app.py: The main Flask application file, containing the definitions of routes and view functions.requirements.txt: Lists the project's dependency libraries, used to record the versions of Flask and other packages.
The app.py file in the my_flask_app directory:
Example
from flask import Flask
app = Flask( __name__ )
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
requirements.txt example:
Flask==2.2.3
2. Medium Project Structure
For slightly more complex applications, the project is usually divided into multiple modules and directories:
my_flask_app/
β
βββ app/
β βββ __init__.py
β βββ routes.py
β βββ models.py
β
βββ config.py
βββ requirements.txt
βββ run.py
app/: Contains the main code of the Flask application.__init__.py: Initializes the Flask application and configures extensions.routes.py: Defines the application's routes and view functions.models.py: Defines the application's data models.
config.py: Configuration file, containing the application's configuration information.requirements.txt: Lists the project's dependency libraries.run.py: Used to start the Flask application.
app/__init__.py example:
Example
from flask import Flask
def create_app():
app = Flask( __name__ )
app.config.from_object('config.Config')
from . import routes
app.register_blueprint(routes.bp)
return app
app/routes.py example:
Example
from flask import Blueprint
bp = Blueprint('main', __name__)
@bp.route('/')
def home():
return 'Hello, World!'
run.py example:
Example
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
3. Complex Project Structure
For larger applications, a more complex project structure may be needed to support higher modularity and scalability:
my_flask_app/
β
βββ app/
β βββ __init__.py
β βββ routes/
β β βββ __init__.py
β β βββ main.py
β β βββ auth.py
β βββ models/
β β βββ __init__.py
β β βββ user.py
β βββ templates/
β β βββ layout.html
β β βββ home.html
β βββ static/
β βββ css/
β βββ js/
β
βββ config.py
βββ requirements.txt
βββ migrations/
β βββ ...
βββ run.py
app/routes/: Separates route management for different functional modules.main.py: Routes for the main module.auth.py: Routes related to authentication.
app/models/: Manages data models, usually related to database operations.user.py: User model.
app/templates/: Stores HTML template files.app/static/: Stores static files, such as CSS and JavaScript.migrations/: Database migration files, usually related to SQLAlchemy.
app/routes/main.py example:
Example
from flask import Blueprint, render_template
bp = Blueprint('main', __name__)
@bp.route('/')
def home():
return render_template('home.html')
app/models/user.py example:
Example
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
YouTip