项目中想把原来在一个文件中的Flask代码拆分,在另一个文件中调用该Flask代码,这里就用到了Blueprint
拆分前:
main.py
from flask import Flask
import threading
app = Flask(__name__)
class Handle:
def __init__(self):
...
...
...
@app.route('/callback', methods=['POST'])
def callback():
handle = Handle()
threading.Thread(target=handle.handle, args=()).start()
return 'success'
if __name__ == "__main__":
app.run(host="0.0.0.0",port=3100)
拆分后:
main.py
from module import server
simple_page = server.construct_blueprint(self.start_time, self.q)
app = Flask(__name__)
app.register_blueprint(simple_page)
app.run(host="0.0.0.0",port=3100)
server.py
from flask import Blueprint
import threading
class Handle:
def __init__(self):
...
...
...
def construct_blueprint(start_time, q):
simple_page = Blueprint('simple_page', __name__, template_folder='templates')
@simple_page.route('/callback', methods=['POST'])
def callback():
handle = Handle()
threading.Thread(target=handle.handle, args=(start_time)).start()
return 'success'
return simple_page
参考: