Javascript 修复用于GoogleCharts api for live graph的JSON数据格式
我在我的网站上的一个页面上使用flask和google图表。我有一个页面,显示json对象格式的数据库的最后一行,如下所示:Javascript 修复用于GoogleCharts api for live graph的JSON数据格式,javascript,html,flask,charts,google-api,Javascript,Html,Flask,Charts,Google Api,我在我的网站上的一个页面上使用flask和google图表。我有一个页面,显示json对象格式的数据库的最后一行,如下所示: {"temperature": 11.0, "datetime": "12-12-12 23-23-23"} 在flask中,我制作此getjson页面,如下所示: @app.route('/getjson') def getjson(): tempdata = getTemperatureData() return render_template('g
{"temperature": 11.0, "datetime": "12-12-12 23-23-23"}
在flask中,我制作此getjson页面,如下所示:
@app.route('/getjson')
def getjson():
tempdata = getTemperatureData()
return render_template('getjson.html', **locals())
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px"></div>
<script type="text/javascript">
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date Time');
data.addColumn('number', 'Temperature');
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var options = {
title: 'Temperature Data',
curveType: 'none',
legend: { position: 'bottom' }
};
drawChart();
setInterval(drawChart, 10000);
function drawChart() {
$.getJSON({
url: 'removed from public view just in case',
type: 'get'
}).done(function (jsonData) {
data.addRows([
[jsonData.datetime, jsonData.temperature]
]);
chart.draw(data, options);
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
});
}
},
packages: ['corechart']
});
</script>
</body>
</html>
这会将局部变量tempdata传递给它。在此之后,我创建了一个html文件(getjson.html),其中包含以下简单代码:
{{tempdata}}
然而,当我加载网页并在inspect element选项卡中进行检查时,我得到了一个类似于Pythonywhere或flask添加了所有html数据的结果:
<html>
#shadow-root (open)
<head></head>
<body>{"temperature": 11.0, "datetime": "12-12-12 23-23-23"}</body>
</html>
我的谷歌图表应该能够实时更新,其代码如下:
@app.route('/getjson')
def getjson():
tempdata = getTemperatureData()
return render_template('getjson.html', **locals())
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px"></div>
<script type="text/javascript">
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date Time');
data.addColumn('number', 'Temperature');
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var options = {
title: 'Temperature Data',
curveType: 'none',
legend: { position: 'bottom' }
};
drawChart();
setInterval(drawChart, 10000);
function drawChart() {
$.getJSON({
url: 'removed from public view just in case',
type: 'get'
}).done(function (jsonData) {
data.addRows([
[jsonData.datetime, jsonData.temperature]
]);
chart.draw(data, options);
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
});
}
},
packages: ['corechart']
});
</script>
</body>
</html>
真的在寻找解决此问题的有效方法,谢谢。您的烧瓶端点不应返回此信息
return render_template('getjson.html', **locals())
它将返回html。相反,你可以这样做:
from flask import jsonify
return jsonify(your_data)