我是新生的烧瓶&网络开发我想将算法的输出传递给模板,以便我可以向用户显示。但是我做错了什么并且没有看到HTML中的任何输出,除了空的子弹点。
routes.py
from flask import Flask, request, jsonify, render_template
from image_processing import find_cross_v4
import json
app = Flask(__name__)
def run_algorithms():
return {'file_name': f.filename, 'set_min': 'hello world 1','rep_sec':'hello world 2'}
@app.route('/upload', methods=['POST'])
def upload():
f = request.files['file']
f.save("image_processing/query.jpg")
data = run_algorithms()
#jsondata = jsonify(data)
#data =json.loads(jsondata)
return render_template('results.html',data=data)
@app.route('/test',methods=['POST'])
def test():
try:
f = request.files['file']
f.save("image_processing/query.jpg")
except KeyError:
return jsonify({'error': 'File Missing'})
result = run_algorithms(f)
return jsonify(result)
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0')
results.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1 class="logo">Results</h1>
<ul>
{% for data in data %}
<li>{{data.file_name}}</li>
<li>{{data.set_min}}</li>
<li>{{data.rep_sec}}</li>
{% endfor %}
</ul>
</body>
</html>
我点击&#39; / test&#39;从命令行
curl --form file=@somefile.jpg http://0.0.0.0:5000/test
以下输出。
{ &#34; file_name&#34;:&#34; somefile.jpg&#34;, &#34; rep_sec&#34;:&#34;你好世界2&#34;, &#34; set_min&#34;:&#34;你好世界1&#34; }
我通过浏览器尝试的结果
答案 0 :(得分:2)
我在HTML模板中删除了'for'语句并且它有效!
答案 1 :(得分:0)
你的意思是:
result = find_cross_v4.image_processing(f)
您没有使用f
参数做任何事情,是否打算这样做?由于那里可能会发生奇怪的事情,因此使用模拟函数来测试与烧瓶相关的功能,即:
def run_algorithms(f):
return {'file_name': 'sample file name', 'set_min': 'minimum value','rep_sec': 'other placeholding data'}
如果按预期输出数据(它应该),那么您已经隔离了图像处理代码中的错误(或者更可能是返回的值)。
答案 2 :(得分:0)
你的run_algorithms方法没有任何参数,但在返回字典时,
{'file_name': f.filename, ... }
使用'f'对象返回文件名,但它不是来自任何地方。所以你应该做的是,在上传方法中:
data = run_algorithms(f) # call the method with f parameter.
并将此参数添加到run_algorithms方法中:
def run_algorithms(f):
return {'file_name': f.filename, 'set_min': 'hello world 1','rep_sec':'hello world 2'}
我希望这会有所帮助。 祝你好运!