小编cra*_*bbs的帖子

Python Flask获取要显示的json数据

我目前正在尝试显示每5秒更新一次的值列表到sqlite数据库.

我可以使用以下代码将结果转换为JSON格式:

@app.route('/_status', methods= ['GET',  'POST'])
def get_temps():
    db = get_db()
    cur = db.execute('select sensor_name, temp from cur_temps ORDER BY sensor_name')
    #cur_temps = cur.fetchall()
    return jsonify(cur.fetchall())
Run Code Online (Sandbox Code Playgroud)

通过浏览器导航到网页返回

{
  "BoilerRoom": 26.44, 
  "Cylinder1": 56.81, 
  "Cylinder2": 39.75, 
  "Cylinder3": 33.94
}
Run Code Online (Sandbox Code Playgroud)

我希望定期在网页上更新这些数据,而无需再次加载整个页面.我陷入了第一道障碍,无法显示实际数据.我正在使用的HTML代码是

{% extends "layout.html" %}
{% block body %}
<script type=text/javascript>
  $(function() {
    $("#submitBtn").click(function() {
         $.ajax({
            type: "GET",
            url: $SCRIPT_ROOT + "_status",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                $('#Result').text(data.value);
            }
        });
    });
  });
</script>

<strong><div id='Result'></div></strong>

{% endblock %}
Run Code Online (Sandbox Code Playgroud)

我从示例中选择了代码,但我需要一个指针.

解决了!!

新的HTML代码

<script …
Run Code Online (Sandbox Code Playgroud)

python jquery json flask

7
推荐指数
1
解决办法
7803
查看次数

标签 统计

flask ×1

jquery ×1

json ×1

python ×1