【问题标题】:Refresh Javascript to get new values from Flask刷新 Javascript 以从 Flask 获取新值
【发布时间】: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


    【解决方案1】:

    您可以进行 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);
      }
    });
    

    然后在你的服务器中让 Flask 获取数据:

    @app.route('/get_data', methods=['GET'])
    def get_data():
        df = sqlio.read_sql(query, conn)
        value = df['my_column'][0]
        return value
    

    【讨论】:

    • 但我需要使用template。您将如何使用模板将数据传递给此 ajax 调用?非常感谢!
    • 我有setInterval,我还需要这个吗?试图让它工作,但没有
    • 是的,如果你想继续更新,你需要它
    • 所以我尝试了这个但仍然没有更新。你能看看我的编辑吗?
    • 我收到405 Method Not Allowed
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-10-05
    • 2015-09-17
    • 1970-01-01
    • 2020-09-28
    相关资源
    最近更新 更多