大型的应用

For larger applications it’s a good idea to use a package instead of a module. 这非常简单。设想一个如下结构的小型应用:

/yourapplication
    yourapplication.py
    /static
        style.css
    /templates
        layout.html
        index.html
        login.html
        ...

简单的包

若要将它转换为一个更大的应用,只要在已存在的文件夹下面创建一个新的yourapplication文件夹,然后将所有的东西都移动到它下面。然后重命名yourapplication.py__init__.py(请务必首先删除所有的.pyc文件,否则很有可能出问题)

你最后得到的东西应该像下面这样:

/yourapplication
    /yourapplication
        __init__.py
        /static
            style.css
        /templates
            layout.html
            index.html
            login.html
            ...

But how do you run your application now? The naive python yourapplication/__init__.py will not work. Let’s just say that Python does not want modules in packages to be the startup file. 但这不是一个大问题,仅仅添加一个名叫setup.py的新文件,把这个文件放在yourapplication文件夹里,其内容如下:

from setuptools import setup

setup(
    name='yourapplication',
    packages=['yourapplication'],
    include_package_data=True,
    install_requires=[
        'flask',
    ],
)

In order to run the application you need to export an environment variable that tells Flask where to find the application instance:

export FLASK_APP=yourapplication

If you are outside of the project directory make sure to provide the exact path to your application directory. Similiarly you can turn on “debug mode” with this environment variable:

export FLASK_DEBUG=true

In order to install and run the application you need to issue the following commands:

pip install -e .
flask run

What did we gain from this? Now we can restructure the application a bit into multiple modules. The only thing you have to remember is the following quick checklist:

  1. Flask应用对象的创建必须在__init__.py文件里完成。这样我们就可以安全的导入每个模块,而 __name__变量将会被分配给正确的包。
  2. 所有(上面有route()装饰器的那些)视图函数必须导入到__init__.py文件。此时,请通过模块而不是对象本身作为路径导入这些视图函数。Import the view module after the application object is created.

这里是__init__.py的一个例子:

from flask import Flask
app = Flask(__name__)

import yourapplication.views

views.py应该看起来像这样:

from yourapplication import app

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

你最终应该得到的程序结构应该是这样:

/yourapplication
    setup.py
    /yourapplication
        __init__.py
        views.py
        /static
            style.css
        /templates
            layout.html
            index.html
            login.html
            ...

Circular Imports

每个Python程序员都讨厌他们,而我们还添加了几个:循环导入(两个模块相互依赖的时候。在这里views.py依赖于__init__.py)。Be advised that this is a bad idea in general but here it is actually fine. 之所以如此,是因为我们实际上没有在__init__.py里使用这些视图,而仅仅是保证模块被导入了,并且我们是在文件的结尾这么做的。

There are still some problems with that approach but if you want to use decorators there is no way around that. 检查Becoming Big这一章来寻找解决问题的些许灵感吧。

使用Blueprints

If you have larger applications it’s recommended to divide them into smaller groups where each group is implemented with the help of a blueprint. For a gentle introduction into this topic refer to the Modular Applications with Blueprints chapter of the documentation.