【问题标题】:Jinja2 to load new dataJinja2 加载新数据
【发布时间】:2014-08-13 00:30:02
【问题描述】:

我已经花了两天时间,但无法弄清楚。

这在index() 中的print value 行中每次都会打印出不同的值,但是一旦它通过render_template('index.html', car_value=value) 进入html,它似乎不会收到新的不同值。 console.log(y) 只保持打印出第一个接收到的值,而不是获取第一个值之后传递的其他值。

请告诉我如何传递并使index.html 能够通过FlaskJinja2 接收这些新值


在 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


【解决方案1】:

由于您总是使用 AJAX 访问相同的 URL,因此很有可能它被缓存了。尝试将其添加到负责数据 AJAX 的 Flask 控制器的顶部:

response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'

或者,您可以将 URL 更改为:

url: "/?" + new Date().getTime(),

所以不可能缓存(因为每次都不一样)。

编辑:我刚刚注意到,您在模板中设置了y 的值,这与AJAX 调用无关。要刷新该值,您必须重新加载页面,AJAX 就目前而言是无用的,因为它会获取一些数据然后忽略它

url: '/get_car_value',
success: function(data) {
  var y = data.car_value;
  console.log(y)
  series.addPoint([x, y], true, true);
}

并制作一个新的控制器:

@app.route('/get_car_value', methods=['GET'])
def get_car_value():
    import json
    data = json.dumps({ car_value: get_data_route() })
    return data, 200, {'Content-Type': 'application/json; charset=utf-8'}

(您可以使用现有的 get_data_route 控制器,但我喜欢一致的 API,并且在使用 AJAX 传输数据时始终使用 JSON 非常有用)。

编辑:JSON 脑放屁。

【讨论】:

  • 我做了但仍然没有工作。你能看到我的更新吗?好郁闷
  • 哦!我刚刚注意到,您从 模板 中获得 car_value,而不是从 AJAX 调用中获得!
  • 是的,我需要传递并更新模板中的car_value,以便模板以新的不同值呈现并每次显示不同的图
  • Nononono,你绝对应该在模板中有car_value。我更新了我的答案 - 你应该从 AJAX 响应中得到它。
  • index 不需要car_value,也不需要get_data_route() 呼叫。将数据生成留给 AJAX 例程。
猜你喜欢
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 2023-03-31
  • 2011-11-07
  • 2012-06-15
相关资源
最近更新 更多