YouTip LogoYouTip

Flask Layout

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.

  1. Simple project structure: Suitable for small applications, with all code concentrated in one file.
  2. Medium 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 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)
← Flask Views FunctionsFlask Step1 β†’