我是Flask和Web开发人员的新手。我想将算法的输出传递给模板,以便可以向用户展示。但是我做错了什么,除了空的项目符号之外,看不到HTML的任何输出。
route.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>
我从命令行打了'/ test'
curl --form [email protected] http://0.0.0.0:5000/test
在下面得到了输出。
{“ file_name”:“ somefile.jpg”,“ rep_sec”:“ hello world 2”,“ set_min”:“ hello world 1”}
通过浏览器尝试操作时的结果
我在HTML模板中删除了“ for”语句,它起作用了!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句