“我正在使用 Flask、Jinja2、higHighcharts”
示例(Python/ flask ):
@app.route("/column/")
def column():
data=[{"data": [49.9, 54.4], "name": "Tokyo"}, {"data": [42, 30.4], "name": "AC"}]
return render_template('column.html', data=data)
我的模板
$(document).ready(function() {
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series:{{ data }}
});
});
我查看 highcharts (column.html)
series:[{&\#39;data': [4, 5, 9], &\#39;name&\#39;: &\#39;Jane&\#39;},{&\#39;data&\#39;: [8, 3, 4], &\#39;name&\#39;: &\#39;John&\#39;}]});
我想纠正Jinja2的写法,最终得到想要的结果。
series: [{
name: 'Jane',
data: [1, 0, 4]}, {
name: 'John',
data: [5, 7, 3]
}]
最佳答案
使用 Markup
将您的数据标记为安全 :
Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped.
或者将{{ data }}
更改为{{ data|tojson|safe }}
。
关于python/flask/Jinja2 和 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168539/