Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
刷新Javascript以从Flask获取新值_Javascript_Python_Ajax_Flask_Jinja2 - Fatal编程技术网

刷新Javascript以从Flask获取新值

刷新Javascript以从Flask获取新值,javascript,python,ajax,flask,jinja2,Javascript,Python,Ajax,Flask,Jinja2,我需要autorefresh下面的load功能,这样我就不必手动刷新整个页面,仍然可以获得my_值的新值。如果我重新加载页面,图表就会重新开始,而我不想重新开始 我认为它每5秒返回不同的值,但每5秒只返回相同的值 如何刷新此脚本并每5秒更新其值 有谁能告诉我为什么不更新数据 $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false }

我需要
autorefresh
下面的
load
功能,这样我就不必手动刷新整个页面,仍然可以获得
my_值的新值。如果我重新加载页面,图表就会重新开始,而我不想重新开始

我认为它每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);
                    }
                }
            }
        });
    });

});
有谁能告诉我为什么不更新数据

$(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);
                    }
                }
            }
        });
    });

});
下面是烧瓶脚本:

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'
    )

您可以执行ajax调用以获取数据:

var x = (new Date()).getTime();
// get data from python script
$.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);
  }
});
@app.route('/get_data', methods=['GET'])
def get_data():
    df = sqlio.read_sql(query, conn)
    value = df['my_column'][0]
    return value
在您的服务器中,让Flask获取数据:

var x = (new Date()).getTime();
// get data from python script
$.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);
  }
});
@app.route('/get_data', methods=['GET'])
def get_data():
    df = sqlio.read_sql(query, conn)
    value = df['my_column'][0]
    return value

但是我需要使用
模板
。如何使用模板将数据传递给此ajax调用?谢谢!我有
setInterval
,我还需要这个吗?试着让它工作,但不是是的,你需要它,如果你想保持更新,所以我尝试了这个,但仍然没有更新。你能看看我的编辑吗?我得到
405方法不允许