将 Python Flask 应用程序拆分为多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15231359/
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
Split Python Flask app into multiple files
提问by user1751547
I'm having trouble understanding how to split a flask app into multiple files.
我无法理解如何将 Flask 应用程序拆分为多个文件。
I'm creating a web service and I want to split the api's into different files (AccountAPI.py, UploadAPI.py, ...), just so I don't have one huge python file.
我正在创建一个 Web 服务,我想将 api 拆分为不同的文件(AccountAPI.py、UploadAPI.py,...),这样我就没有一个巨大的 python 文件。
I've read that you can do this with Blueprints, but I'm not entirely sure that route is the right one for me.
我读到您可以使用蓝图执行此操作,但我并不完全确定该路线是否适合我。
Ultimately I want to run one Main python file and include other files so that when it runs, they are considered one big file.
最终,我想运行一个 Main python 文件并包含其他文件,以便在运行时将它们视为一个大文件。
For example if I have Main.py and AccountAPI.py I want to be able to do this:
例如,如果我有 Main.py 和 AccountAPI.py,我希望能够做到这一点:
Main.py:
主要.py:
from flask import Flask
import AccountAPI
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
AccountAPI.py:
账户API.py:
@app.route("/account")
def accountList():
return "list of accounts"
I know with this example it obviously won't work, but is it possible to do something like that?
我知道这个例子显然行不通,但是有可能做这样的事情吗?
Thanks
谢谢
采纳答案by cyroxx
Yes, Blueprints are the right way to do it. What you are trying to do can be achieved like this:
是的,蓝图是正确的方法。你想要做的事情可以这样实现:
Main.py
主文件
from flask import Flask
from AccountAPI import account_api
app = Flask(__name__)
app.register_blueprint(account_api)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
AccountAPI.py
账户API.py
from flask import Blueprint
account_api = Blueprint('account_api', __name__)
@account_api.route("/account")
def accountList():
return "list of accounts"
If this is an option, you might consider using different URL prefixes for the different APIs/Blueprints in order to cleanly separate them. This can be done with a slight modification to the above register_blueprintcall:
如果这是一个选项,您可以考虑为不同的 API/蓝图使用不同的 URL 前缀,以便将它们干净地分开。这可以通过对上述register_blueprint调用稍作修改来完成:
app.register_blueprint(account_api, url_prefix='/accounts')
For further documentation, you may also have a look at the official docs.
有关更多文档,您还可以查看官方文档。
回答by Bhaskar
One another way to do this can be with lazy loading, where you would explicitly attach view functions on need basis.
另一种方法是使用延迟加载,您可以根据需要显式附加视图函数。
回答by Thomas Krickl
If you are using blueprints and want to route / redirect to a url of your blueprint inside a template you are using you need to use the correct url_for statement.
如果您正在使用蓝图并希望路由/重定向到您正在使用的模板内的蓝图 url,则需要使用正确的 url_for 语句。
In your case if you would like to open the url account of your blueprint you have to state it like this in your template:
在您的情况下,如果您想打开蓝图的 url 帐户,则必须在模板中这样说明:
href="{{ url_for('account_api.account') }}"
and for the main appit would look like this:
对于主应用程序,它看起来像这样:
redirect(url_for('account_api.account'))
Otherwise the werkzeug library will throw an error.
否则 werkzeug 库会抛出错误。
回答by Searene
Using Blueprintyou can add your routes in the routesdirectory.
使用Blueprint您可以在routes目录中添加您的路线。
Structure
结构
app.py
routes
__init__.py
index.py
users.py
__init__.py
__init__.py
from flask import Blueprint
routes = Blueprint('routes', __name__)
from .index import *
from .users import *
index.py
索引.py
from flask import render_template
from . import routes
@routes.route('/')
def index():
return render_template('index.html')
users.py
用户.py
from flask import render_template
from . import routes
@routes.route('/users')
def users():
return render_template('users.html')
app.py
应用程序
from routes import *
app.register_blueprint(routes)
If you want to add a new route file, say accounts.py, you just need to create the file accounts.pyin the routesdirectory, just like index.pyand users.py, then import it in the routes.__init__.pyfile
如果你想添加一个新的路由文件,比如说accounts.py,你只需要accounts.py在routes目录中创建文件,就像index.pyand一样users.py,然后将它导入到routes.__init__.py文件中
from .accounts import *
回答by seunggabi
see this.
it's exactly guide.
看到这个。
这正是指南。
https://flask-restplus.readthedocs.io/en/stable/scaling.html
https://flask-restplus.readthedocs.io/en/stable/scaling.html

