我是Flask的新人。我尝试从main传递参数,但它不起作用。希望有人可以帮助我。这是我的代码
from flask import Flask, render_template, request
import controllFlask
import pickle
app = Flask(__name__) # creates a flask object
import subprocess
@app.route('/', methods=['GET', 'POST'])
def ner():
'''controlls input of Webapp and calls proccessing methods'''
knownEntities = pickle.load( open( "entityDictThis.p", "rb" ))
print("loades")
content = ""
if request.method == 'POST':
testText = (request.form['input'])
app.ma = controllFlask.controll(testText, knownEntities)
subprocess.call("controllFlask.py", shell=True)
with open("test.txt", "r") as f:
content = f.read()
return render_template("ner.jinja2", content=content)
def flaskApp():
app.debug = True
app.run()
我想在flaskApp中打开entityDictThis并将其发送给ner-function。因为我希望通过这种方式只加载一次。目前,每次重新加载页面时都会加载它,并且需要很长时间。有一个简单的方法吗?
答案 0 :(得分:1)
这似乎只是一个范围问题,只是将加载pickle文件的行放在上面的范围内,它应该解决问题。
from flask import Flask, render_template, request
import controllFlask
import pickle
app = Flask(__name__) # creates a flask object
import subprocess
knownEntities = pickle.load( open( "entityDictThis.p", "rb" ))
@app.route('/', methods=['GET', 'POST'])
def ner():
'''controlls input of Webapp and calls proccessing methods'''
print("loades")
content = ""
if request.method == 'POST':
testText = (request.form['input'])
app.ma = controllFlask.controll(testText, knownEntities)
subprocess.call("controllFlask.py", shell=True)
with open("test.txt", "r") as f:
content = f.read()
return render_template("ner.jinja2", content=content)
def flaskApp():
app.debug = True
app.run()
我还建议,正如@bouteillebleu所提到的,使用with
关键字来关闭加载的文件,这会为您自动执行此操作。
with open( "entityDictThis.p", "rb" ) as f:
knownEntities = pickle.load(f)