Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Project Structure

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.

  1. Simple Project Structure: Suitable for small applications, with all code concentrated in a single file.
  2. Medium Project Structure: Divides the application into multiple modules for easier management and extension.
  3. 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

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

- **`__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.

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

- **`main.py`**: Routes of the main module. - **`auth.py`**: Authentication-related routes. - **`user.py`**: User model.

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

)