如何将烧瓶app分成多个py文件?

时间:2023-01-24 13:05:08

My flask application currently consists of a single test.py file with multiple routes and the main() route defined. Is there some way I could create a test2.py file that contains routes that were not handled in test.py?

我的flask应用程序当前包含一个带有多个路由的test.py文件,并定义了main()路由。有没有办法我可以创建一个test2.py文件,其中包含未在test.py中处理的路由?

@app.route('/somepath')
def somehandler():
    # Handler code here

I am concerned that there are too many routes in test.py and would like to make it such that I can run python test.py, which will also pick up the routes on test.py as if it were part of the same file. What changes to I have to make in test.py and/or include in test2.py to get this to work?

我担心test.py中有太多的路由,并且想要这样我可以运行python test.py,它也会在test.py上获取路由,就好像它是同一个文件的一部分一样。我必须在test.py和/或包含在test2.py中进行哪些更改才能使其正常工作?

3 个解决方案

#1


90  

You can use the usual Python package structure to divide your App into multiple modules, see the Flask docs.

您可以使用通常的Python包结构将App划分为多个模块,请参阅Flask文档。

However,

然而,

Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications.

Flask使用蓝图概念来制作应用程序组件,并在应用程序或应用程序中支持常见模式。

You can create a sub-component of your app as a Blueprint in a separate file:

您可以在单独的文件中将应用程序的子组件创建为蓝图:

simple_page = Blueprint('simple_page', __name__, template_folder='templates')
@simple_page.route('/<page>')
def show(page):
    # stuff

And then use it in the main part:

然后在主要部分使用它:

from yourapplication.simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)

Blueprints can also bundle specific resources: templates or static files. Please refer to the Flask docs for all the details.

蓝图还可以捆绑特定资源:模板或静态文件。有关所有详细信息,请参阅Flask文档。

#2


12  

I would like to recommend flask-empty at GitHub.

我想在GitHub上推荐使用flask-empty。

It provides an easy way to understand Blueprints, multiple views and extensions.

它提供了一种理解蓝图,多视图和扩展的简便方法。

#3


2  

Dividing the app into blueprints is a great idea. However, if this isn't enough, and if you want to then divide the Blueprint itself into multiple py files, this is also possible using the regular Python module import system, and then looping through all the routes that get imported from the other files.

将应用划分为蓝图是一个好主意。但是,如果这还不够,并且如果您想将Blueprint本身划分为多个py文件,则可以使用常规Python模块导入系统,然后循环遍历从其他文件导入的所有路由。

I created a Gist with the code for doing this:

我用这个代码创建了一个Gist:

https://gist.github.com/Jaza/61f879f577bc9d06029e

https://gist.github.com/Jaza/61f879f577bc9d06029e

As far as I'm aware, this is the only feasible way to divide up a Blueprint at the moment. It's not possible to create "sub-blueprints" in Flask, although there's an issue open with a lot of discussion about this:

据我所知,这是目前划分蓝图的唯一可行方法。在Flask中创建“子蓝图”是不可能的,尽管有一个问题已经公开,有很多关于此的讨论:

https://github.com/mitsuhiko/flask/issues/593

https://github.com/mitsuhiko/flask/issues/593

Also, even if it were possible (and it's probably do-able using some of the snippets from that issue thread), sub-blueprints may be too restrictive for your use case anyway - e.g. if you don't want all the routes in a sub-module to have the same URL sub-prefix.

此外,即使它是可能的(并且它可能可以使用来自该问题线程的一些片段),但子蓝图可能对您的用例过于严格 - 例如如果您不希望子模块中的所有路由具有相同的URL子前缀。

#1


90  

You can use the usual Python package structure to divide your App into multiple modules, see the Flask docs.

您可以使用通常的Python包结构将App划分为多个模块,请参阅Flask文档。

However,

然而,

Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications.

Flask使用蓝图概念来制作应用程序组件,并在应用程序或应用程序中支持常见模式。

You can create a sub-component of your app as a Blueprint in a separate file:

您可以在单独的文件中将应用程序的子组件创建为蓝图:

simple_page = Blueprint('simple_page', __name__, template_folder='templates')
@simple_page.route('/<page>')
def show(page):
    # stuff

And then use it in the main part:

然后在主要部分使用它:

from yourapplication.simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)

Blueprints can also bundle specific resources: templates or static files. Please refer to the Flask docs for all the details.

蓝图还可以捆绑特定资源:模板或静态文件。有关所有详细信息,请参阅Flask文档。

#2


12  

I would like to recommend flask-empty at GitHub.

我想在GitHub上推荐使用flask-empty。

It provides an easy way to understand Blueprints, multiple views and extensions.

它提供了一种理解蓝图,多视图和扩展的简便方法。

#3


2  

Dividing the app into blueprints is a great idea. However, if this isn't enough, and if you want to then divide the Blueprint itself into multiple py files, this is also possible using the regular Python module import system, and then looping through all the routes that get imported from the other files.

将应用划分为蓝图是一个好主意。但是,如果这还不够,并且如果您想将Blueprint本身划分为多个py文件,则可以使用常规Python模块导入系统,然后循环遍历从其他文件导入的所有路由。

I created a Gist with the code for doing this:

我用这个代码创建了一个Gist:

https://gist.github.com/Jaza/61f879f577bc9d06029e

https://gist.github.com/Jaza/61f879f577bc9d06029e

As far as I'm aware, this is the only feasible way to divide up a Blueprint at the moment. It's not possible to create "sub-blueprints" in Flask, although there's an issue open with a lot of discussion about this:

据我所知,这是目前划分蓝图的唯一可行方法。在Flask中创建“子蓝图”是不可能的,尽管有一个问题已经公开,有很多关于此的讨论:

https://github.com/mitsuhiko/flask/issues/593

https://github.com/mitsuhiko/flask/issues/593

Also, even if it were possible (and it's probably do-able using some of the snippets from that issue thread), sub-blueprints may be too restrictive for your use case anyway - e.g. if you don't want all the routes in a sub-module to have the same URL sub-prefix.

此外,即使它是可能的(并且它可能可以使用来自该问题线程的一些片段),但子蓝图可能对您的用例过于严格 - 例如如果您不希望子模块中的所有路由具有相同的URL子前缀。