我花了两天时间还是没搞清楚这个问题。
index()中的打印值每次都不同,而且一旦它通过rendertemplate('index.html', carvalue=value)转到html,它就接收不到新的不同值了。console.log(y)只能打印出第一个接收到的值,之后传递的其他值就接收不到了。请告诉我如何传递并使index.html能够通过Flask和Jinja 2接收新值?
更新:
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任何东西都不输出。