Flask Project Structure

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, when the project gets bigger, it is unrealistic to put all the code in one file.

The Flask project structure can vary depending on the size and complexity of the application.

The following are several common Flask project structures, suitable for simple applications and more complex applications respectively.

  1. Simple project structure: Suitable for small applications, with all code concentrated in a single file.
  2. Medium-sized project structure: Divides the application into multiple modules for easier management and expansion.
  3. Complex project structure: Supports higher modularity, suitable for large applications, including routes, models, templates, and static file directories.

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 dependencies, used to record version information for Flask and other packages.

Code for 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: Define the application's routes and view functions.
    • models.py: Define the application's data models.
  • config.py: Configuration file, containing the application's configuration information.
  • requirements.txt: List 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 extensibility:

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/: Manages routes for different functional modules separately.
    • main.py: Routes for the main module.
    • auth.py: Authentication-related routes.
  • app/models/: Manage data models, usually related to database operations.
    • user.py: User model.
  • app/templates/: Store HTML template files.
  • app/static/:Store 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)
other extensions