Flask Project Structure
As your Flask application grows beyond a single file, a good folder structure keeps your code organized and easy to maintain. Flask does not enforce one specific layout, but the community follows conventions that work well for most projects.
Why Structure Matters
Imagine a library where every book sits randomly on any shelf. Finding a book becomes painful. A project without structure works the same way — every developer wastes time searching for files. A clear structure means you always know where a template lives, where a database model sits, and where configuration goes.
Small App Structure (Single File)
For a tiny project with 2–3 routes, a single file is perfectly fine:
myapp/ ├── app.py ├── requirements.txt └── venv/
This structure works for quick experiments and learning projects. It breaks down when the project grows larger.
Standard Small-to-Medium Structure
Once you add templates, static files, and more routes, use this layout:
myapp/
├── app.py
├── requirements.txt
├── venv/
├── templates/
│ ├── base.html
│ ├── index.html
│ └── about.html
└── static/
├── css/
│ └── style.css
├── js/
│ └── main.js
└── images/
└── logo.png
What Each Folder Does
| Folder / File | Purpose |
|---|---|
app.py | Entry point — starts the Flask app |
requirements.txt | Lists all Python packages the project needs |
venv/ | Virtual environment (never commit this to version control) |
templates/ | HTML files that Flask renders with Jinja2 |
static/ | CSS, JavaScript, and images served directly to the browser |
Flask Finds Templates Automatically
Flask looks for a folder called templates in the same directory as app.py. When you call render_template('index.html'), Flask searches inside that folder. You do not need to tell Flask the full path.
render_template('index.html')
│
▼
Flask looks in: myapp/templates/index.html ✓
Flask Finds Static Files Automatically
Flask serves files from the static folder at the URL path /static/. A file at static/css/style.css becomes accessible at http://127.0.0.1:5000/static/css/style.css.
Larger App Structure with Blueprints
When a project has many features — user authentication, a blog, an admin panel — splitting code into modules becomes essential. Flask uses Blueprints for this. A typical larger structure looks like:
myapp/
├── run.py ← starts the app
├── requirements.txt
├── venv/
└── app/
├── __init__.py ← creates the Flask app
├── config.py ← configuration settings
├── models.py ← database models
├── auth/ ← authentication blueprint
│ ├── __init__.py
│ ├── routes.py
│ └── templates/
│ └── login.html
├── blog/ ← blog blueprint
│ ├── __init__.py
│ ├── routes.py
│ └── templates/
│ └── post.html
├── templates/ ← shared templates
│ └── base.html
└── static/ ← shared static files
└── css/
└── style.css
The .gitignore File
When you push your project to GitHub or another version control system, some folders and files should stay on your local machine only. Create a file called .gitignore in the project root with these entries:
venv/ __pycache__/ *.pyc .env instance/
The venv/ folder can be several hundred megabytes and contains packages anyone can install from requirements.txt. The .env file holds secret keys and database passwords — never commit those.
The Instance Folder
Flask supports a special folder called instance/. Files inside it override configuration for a specific deployment without changing your main code. Database files and environment-specific config files often live here. Flask automatically looks for instance/config.py and merges its settings.
myapp/
├── app.py
├── config.py ← default config (committed to git)
└── instance/
└── config.py ← machine-specific config (NOT committed)
Summary
Start simple with a single app.py file and grow the structure as the project demands. Put HTML in templates/, put CSS and images in static/, and use Blueprints when features need their own folders. A consistent structure lets you find any file in seconds and makes collaboration with other developers much easier.
