如何解决jinja2.exceptions错误.UndefinedError: & # 39; get_count& #



我正在使用flask应用程序进行小型python应用程序,同时单击本地主机链接,我没有得到内部服务器错误和jinja2。异常错误我不知道我在哪里做错了正常的python我的脚本正在工作,并给出结果html页面无法读取我的id_count输出如何使用python flask解决这个问题这里是我的脚本

import numpy as np
from flask import Flask, render_template, request
app = Flask(__name__)
x = np.array([0, 7, 18, 24, 26, 27, 26, 25, 26, 16, 20, 16, 23, 33, 27, 27,
22, 26, 27, 26, 25, 24, 25, 26, 23, 25, 26, 24, 23, 12, 22, 11, 15, 24, 11,
12, 11, 27, 19, 25, 26, 21, 23, 26, 13, 9, 22, 18, 23, 26, 26, 25, 10, 22,
27, 25, 19, 10, 15, 20, 21, 13, 16, 16, 15, 19, 17, 20, 24, 26, 20, 23, 23,
])
@app.route('/')
def main():
return render_template('index.html')
@app.route('/send', methods=['POST'])
def get_count(id_count):
sub_lists = np.split(x, np.where(np.diff(x) <0)[0] + 1)
id_count=0
id_list = []
for unit in sub_lists:
if min(unit) ==0 and max(unit)>20 and len(set(unit)) > 1:
id_count += 1
id_list.append(unit)
return id_count
return render_template("index.html",get_count(x))
print("Number of counts: ",get_count(x))
if __name__=="__main__":
app.run(debug=False, port=1223)
``` this is my app.py file 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<form action="/data" method = "POST">
<h2> flask app for counting accelerations</h2>
<p>The acceleration count is "{{get_count(x)}}"</p>
</form>
</head>
</html>
如何纠正我的HTML页面获得计数值预期输出"加速计数为4(某个值)">

我认为你必须在python代码中命名参数:

render_template("index.html",count_result=get_count(x))

在你的模板中使用它,而不是尝试使用python函数:

{{ count_result }}

最新更新