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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14415500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:23:31  来源:igfitidea点击:

Common folder/file structure in Flask app

pythonflask

提问by kasperhj

I have just created a flask application and so far I have a router for my "Hello world!" template.

我刚刚创建了一个 Flask 应用程序,到目前为止我有一个用于“Hello world!”的路由器。模板。

I would like to add a little (a lot) more functionality, but I wonder how I should structure the app directory.

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

What's the most common way of structuring a Flask app? For instance, should I create a routes.pyfor all my routes? Where does the SQLAlchemy stuff go? Should models be in models.py?

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

采纳答案by dirn

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.

您应该查看 Flask 文档的 Patterns 部分中的 Larger Applications 页面:http: //flask.pocoo.org/docs/patterns/packages/。当他们的应用程序调用包而不是模块时,这似乎是大多数人遵循的模型。

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

我相信views.py这就是你所说的routes.py。之后,模型会进入models.py,表格会进入forms.py,等等。

回答by Mohammad Efazati

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

这靠近 Django 结构

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

我建议你看一些项目来给你你想要的

回答by Shankar ARUL - jupyterdata.com

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

您可以从这里的 cookiecutter 模板中获得灵感,从而快速启动您的应用程序开发

回答by simanacci

An example of a FlaskApp directory:

FlaskApp 目录的示例:

    /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 routesare 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 htmltemplates 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

run.py- 包含将导入应用程序并启动开发服务器的实际 python 代码。
config.py- 为您的应用程序存储配置。
__init__.py- 初始化您的应用程序,创建 Flask 应用程序实例。
views.py- 这是routes定义的地方。
models.py- 这是您为应用程序定义模型的地方。
static-包含静态文件,即CSS,Javascript,图片
templates-这是您存储html模板,即index.htmllayout.html
requirements.txt-这就是你存储你的包依赖关系,您可以使用pip
yourappenv-你的虚拟环境对于发展

回答by Kimmo Hintikka

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.

这种类型的结构非常适用于不同用户组使用不同类型视图的市场或 SaaS 应用程序。API only flask 应用程序我可能会使用功能拆分。

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

以下是 Flask 蓝图的示例。蓝图本质上是关于如何拆分 Flask 应用程序以获得更易于管理的部分的文档建议。有关更多信息,请访问: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

回答by Nuhman

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'.

这里的“电影”是主应用程序的名称。它包含“ run.py”和一个名为“ app”的文件夹。“应用程序”文件夹包含所有必要的文件烧瓶诸如“模板”文件夹,“ __init __.py”和“ routes.py”。

Contents of:

内容:

run.py:

运行.py

from app import app

__init.py__:

__init.py__

from flask import Flask

app = Flask(__name__)

from app import routes


app.run(debug=True)

routes.py:

路线.py

from app import app

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

回答by userx

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.

烧瓶的美在于它的灵活性。您可以轻松构建类似项目结构的 django。Django 普及了应用程序中功能的抽象并使它们可重用,但对于许多项目来说这可能是一种矫枉过正。

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

  2. 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

  1. 最小骨架

    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

  2. django 像骨架

    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 articleon this.

这是一篇关于这方面的优秀文章

回答by Ryabchenko Alexander

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

好主意是在 github 上使用 goole for sceletons/template 项目

for example you can look at this

例如你可以看看这个

回答by Manish Meshram

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.

static 文件夹包含网站的所有静态文件。模板文件夹包含所有 HTML 页面。和 app.py 包含您的 python 视图。