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, as a project grows, putting all the code in one file becomes impractical.
Flask project structure can vary depending on the scale and complexity of the application.
Here are several common Flask project structures, suited for simple applications and more complex ones respectively.
- Simple Project Structure: Suitable for small applications, with all code concentrated in a single file.
- Medium Project Structure: Divides the application into multiple modules for easier management and extension.
- Complex Project Structure: Supports a higher degree of 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 dependencies, used to record version information for Flask and other packages.
Code in the app.py file under 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 dependencies.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/: Manage the routes of different functional modules separately.main.py: Routes of the main module.auth.py: Authentication-related routes.
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
)