Flask 应用程序中常见的文件夹/文件结构

我刚刚创建了一个烧瓶应用程序,到目前为止,我已经为我的“ Hello world!”模板设置了一个路由器。

我想添加一点(很多)更多的功能,但我不知道我应该如何构造应用程序目录。

构建 Flask 应用程序最常用的方法是什么? 例如,我应该为我所有的路线创建一个 routes.py吗? SQLAlchemy 的东西放哪儿了? 模型应该在 models.py

126539 次浏览

You should check out the Larger Applications page in the Patterns section of the Flask docs: http://flask.pocoo.org/docs/patterns/packages/. It seems to be the model that most people follow when their application calls for a package instead of a module.

I believe views.py is what you are calling routes.py. After that, models would go in models.py, forms would go in forms.py, etc.

I think flask is micro framework and now you must decide how create files and folders.

i use this way :

this is near Django structure

i suggest you see some project to give you what you want

You could get inspired by the cookiecutter templates here to jumpstart your app development

An example of a FlaskApp directory:

    /yourapp
/run.py
/config.py
/app
/__init__.py
/views.py
/models.py
/static/
/main.css
/templates/
/base.html
/requirements.txt
/yourappenv

run.py - contains the actual python code that will import the app and start the development server.
config.py - stores configurations for your app.
__init__.py - initializes your application creating a Flask app instance.
views.py - this is where routes are defined.
models.py - this is where you define models for your application.
static - contains static files i.e. CSS, Javascript, images
templates - this is where you store your html templates i.e. index.html, layout.html
requirements.txt - this is where you store your package dependancies, you can use pip
yourappenv - your virtual environment for development

I would say if you split the application use divisional rather than functional structure. I advocate this because you are more likely to work on 1 of these divisional components at any one time.

This type of structure lends itself well on marketplace or SaaS apps where different user groups use a different type of views. API only flask app I might use functional splitting.

Here are examples from Flask Blueprints. Blueprints are essentially documented advice how to split Flask application for more manageable pieces. More on this at : http://exploreflask.com/en/latest/blueprints.html

Here is an example of divisional splitting. See how each feature is grouped together.

yourapp/
__init__.py
admin/
__init__.py
views.py
static/
templates/
home/
__init__.py
views.py
static/
templates/
control_panel/
__init__.py
views.py
static/
templates/
models.py

Here is the functional example >

yourapp/
__init__.py
static/
templates/
home/
control_panel/
admin/
views/
__init__.py
home.py
control_panel.py
admin.py
models.py

Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:

   |__movies
|__run.py
|__app
├── templates
│   └── index.html
│   └── signup.html
└── __init__.py
└── routes.py

Here 'movies' is the name given for the main application. It contains 'run.py' and a folder called 'app'. 'app' folder contains all necessary flask files such as 'templates' folder, '__init __.py', and 'routes.py'.

Contents of:

run.py:

from app import app

__init.py__:

from flask import Flask


app = Flask(__name__)


from app import routes




app.run(debug=True)

routes.py:

from app import app


@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"

Beauty of flask lies in its flexibility. You can build django like project-structure easily. Django popularized abstraction of features in apps and making them reusable but it can be a overkill for many projects.

But with flask you can go either way. Write reusable apps or write simple apps. Check these cookiecutter skeletons -

  1. minimal skeleton
    myproject
├── config.py
├── instance
│   └── config.py
├── myproject
│   ├── commands.py
│   ├── controllers.py
│   ├── extensions.py
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── routes.py
│   └── ui
│       ├── static
│       │   ├── css
│       │   │   └── styles.css
│       │   └── js
│       │       └── custom.js
│       └── templates
│           └── index.html
├── README.md
├── requirements.txt
└── wsgi.py
  1. django like skeleton
    myproject
├── config.py
├── development.py
├── instance
│   └── config.py
├── myproject
│   ├── auth
│   │   ├── controllers.py
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   └── routes.py
│   ├── helpers
│   │   ├── controllers.py
│   │   ├── __init__.py
│   │   └── models.py
│   ├── __init__.py
│   └── ui
│       └── templates
│           ├── 404.html
│           ├── 500.html
│           └── base.html
├── README.md
├── requirements.txt
├── tests
│   ├── auth
│   │   ├── __init__.py
│   │   └── test_controllers.py
│   └── __init__.py
└── wsgi.py

This is an excellent article on this.

good idea is to goole for sceletons/template projects on github

for example you can look at this

Here is the basic file structure for flask I use regularly.

yourapp/
static/
js
css
img


templates/
home.html
index.html


app.py

static folder contains all the static files of the website. The templates folder contains all the HTML pages. and app.py contains your python views.

In flask we can maintain the mvc structure like as separate all thinks for example 1 Templets folder contains all html files 2 Static folder contains all css and boostrap related files 3 User defined db folder for db connection and crud operations 4 User defined Model folder for accessing/ binding the data from Templets/front- end to db/back-end connectivity and after the main activity file
for reference flask file structure link as follow https://flask.palletsprojects.com/en/1.1.x/tutorial/layout/

I decided to nest the source code folder under src/.

my_flask_app (project folder)
├── app.py
├── setup.py
├── src/
│   ├── my_flask_app/ (source code folder)
│   │   ├── config.py
│   │   ├── errors.py
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── routes.py
│   │   ├── static/
│   │   └── templates/
│   └── my_flask_app.egg-info/
|
└── tests/ (test folder)

Because of this, I added package_dir={'': 'src'} in setup.py.

For larger projects here's what my directory structure looks like

app_name(root folder)
│  .env
│  .gitignore
│  .gitattributes
│  README.MD
│  pyproject.toml
│
└── app(source code)
│   │   __init__.py
│   │   __main__.py
│   │   db.py
│   │   auth.py
│   │   forms.py
│   │   utils.py
│   │   config.py
│   │
│   └─── routes
│          __init__.py
│          views.py
│          api.py
│          auth.py
│
└─ resources
│   |─── static
│   │       css
│   │       js
│   │       images
│   │
│   └─── templates
│           base
|           components
│
└─ tests
└─ venv
└─ docs