Overview | Intro | Anatomy | Play | Ideation | Sign-Up | Login | Profile | Style | Deploy | Scrum |
Flask File Structure
Categories: Python Flask Breadcrumb: /An overview of the file structure of flask
Flask Project Structure: Explained
This guide breaks down a typical Flask backend project, especially one used for building real-world APIs and dashboards.
π File & Folder Overview
/flask-app
β
βββ init.py
βββ main.py
β
βββ /api
β βββ users.py
β βββ calendar.py
β βββ issues.py
β
βββ /models
β βββ user.py
β βββ event.py
β βββ issue.py
β
βββ /scripts
β βββ db_init.py
β
βββ /templates
β βββ dashboard.html
β
βββ /static
β βββ styles.css
β
βββ requirements.txt
π§ Top-Level Python Files
main.py
- Purpose: Entry point of your app.
- Runs the server using Flask.
from __init__ import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
init.py Purpose: App configuration and Blueprint registration.
Sets up Flask and brings together all your routes.
from flask import Flask
from api.users import user_api
from api.calendar import calendar_api
def create_app():
app = Flask(__name__)
app.register_blueprint(user_api)
app.register_blueprint(calendar_api)
return app
π§ /api/ β Route Handlers (Controllers) Each file handles specific features of the app (users, events, issues).
Define HTTP endpoints (@app.route(β¦)) and return JSON responses.
Examples:
- users.py: /register, /login
- calendar.py: /calendar/create
- issues.py: /issues/report
ποΈ /models/ β Database Models Define database schemas using SQLAlchemy.
Each file represents a table (User, Event, Issue, etc).
# models/user.py
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
π /scripts/ β Utility Scripts One-off tools, like initializing your database.
# scripts/db_init.py
from __init__ import create_app
from models import db
app = create_app()
with app.app_context():
db.create_all()
πΌ /templates/ β HTML Views Flask uses Jinja2 to render these templates. Used for simple backend UIs or dashboards.
<h1>User Dashboard</h1>
<ul>
</ul>
π¨ /static/ β CSS, JS, Images Assets for the frontend views.
Flask serves these alongside /templates.
π requirements.txt β Dependency File Lists all Python packages your app needs.
Install everything with:
pip install -r requirements.txt
β Pro Tip: Where to Add New Logic Want to add a route that uses an external API?
-
Create a new file in /api/ (e.g., weather.py)
-
Add a route using requests.get()
-
Register the Blueprint in init.py
Donβt put it in /models/ unless youβre saving data to the database!