【发布时间】:2014-08-13 00:30:02
【问题描述】:
我已经花了两天时间,但无法弄清楚。
这在index() 中的print value 行中每次都会打印出不同的值,但是一旦它通过render_template('index.html', car_value=value) 进入html,它似乎不会收到新的不同值。 console.log(y) 只保持打印出第一个接收到的值,而不是获取第一个值之后传递的其他值。
请告诉我如何传递并使index.html 能够通过Flask 和Jinja2 接收这些新值
在 Amadan 的帮助下进行编辑
def get_data():
df = sqlio.read_sql(qry1, conn)
value = df['count'][0]
return value
@app.route('/get_data', methods=['GET','POST'])
def get_data_route():
value = get_data()
return value
@app.route('/get_car_value', methods=['GET'])
def get_car_value():
data = "{ car_value: %s }" % get_data_route()
return data, 200, {'Content-Type': 'application/json; charset=utf-8'}
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(
port=8000,
host='0.0.0.0'
)
还有
$(function() {
var chart;
$('#car_id').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime();
$.ajax({
type: "GET",
url: "/get_car_value",
success: function(data) {
var y = data.car_value;
console.log(y)
series.addPoint([x, y], true, true);
}
});
}, 5000);
}
}
},
仍然无法正常工作...console.log 中没有打印出任何内容
【问题讨论】:
-
在 Amadan 的帮助下解决了
标签: javascript python ajax flask jinja2