0
0
Flaskframework~5 mins

Flask project structure conventions

Choose your learning style9 modes available
Introduction

Organizing your Flask project helps keep your code clean and easy to understand. It makes working with others and growing your app simpler.

When starting a new Flask web app to keep files tidy.
When your app grows and you add more pages or features.
When working with a team to make collaboration easier.
When you want to separate code, templates, and static files clearly.
Syntax
Flask
my_flask_app/
├── app/
│   ├── __init__.py
│   ├── routes.py
│   ├── models.py
│   ├── templates/
│   │   └── home.html
│   └── static/
│       ├── css/
│       └── js/
├── venv/
├── config.py
├── requirements.txt
└── run.py

app/ folder holds your main application code.

templates/ stores HTML files, static/ holds CSS, JavaScript, and images.

Examples
Basic structure with main app code, templates, and static files.
Flask
my_flask_app/
├── app/
│   ├── __init__.py
│   ├── routes.py
│   ├── templates/
│   └── static/
Adding a config file for settings and a run script to start the app.
Flask
my_flask_app/
├── app/
│   ├── __init__.py
│   ├── routes.py
│   ├── models.py
│   ├── templates/
│   └── static/
├── config.py
├── run.py
Using blueprints to organize different parts of a bigger app.
Flask
my_flask_app/
├── app/
│   ├── __init__.py
│   ├── blueprints/
│   │   ├── auth.py
│   │   └── blog.py
│   ├── templates/
│   └── static/
Sample Program

This simple Flask app uses the project structure where templates/home.html holds the HTML page. The app runs and shows the home page at the root URL.

Flask
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Keep __init__.py to make the app folder a package.

Use requirements.txt to list your Python packages.

Separate templates and static files for clarity and easier updates.

Summary

Organize Flask projects with folders for app code, templates, and static files.

Use __init__.py to initialize your app package.

Adding config and run files helps manage settings and start the app easily.