【发布时间】:2014-08-12 14:04:02
【问题描述】:
我需要autorefresh 下面的load 函数,这样我就不必手动刷新整个页面并且仍然获得my_value 的新值。如果我重新加载页面,图表会从我不想要的地方重新开始。
我认为这每 5 秒返回不同的值,但每 5 秒只返回相同的值。
如何刷新此脚本并每 5 秒更新一次其值?
谁能告诉我为什么不更新数据?
谁能告诉我为什么不更新数据?
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$(function() {
var chart;
$('#my_id').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime();
// var y = {{my_value}}; // Math.random() * 100;
// series.addPoint([x, y], true, true);
$.ajax({
type: "GET",
url: "/get_data",
data: data, // your "template" data goes here
success: function(my_value) {
var y = my_value;
series.addPoint([x, y], true, true);
}
});
}, 5000);
}
}
}
});
});
});
这是 Flask 脚本:
conn = MySQLdb.connect(host=host,
user=username,
passwd=password,
db=database,
port=port)
@app.route('/', methods=['GET'])
def index():
request.args.get('key', '')
df = sqlio.read_sql(qry1, conn)
value = df['count'][0]
return render_template('index.html', my_value=value)
@app.route('/get_data', methods=['GET'])
def get_data():
df = sqlio.read_sql(qry1, conn)
value = df['count'][0]
return value
if __name__ == '__main__':
app.run(
port=1114,
host='0.0.0.0'
)
【问题讨论】:
标签: javascript python ajax flask jinja2