python /烧瓶/ Jinja2和Json

前端之家收集整理的这篇文章主要介绍了python /烧瓶/ Jinja2和Json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
“我使用的是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,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}}.

原文链接:https://www.f2er.com/python/185551.html

猜你在找的Python相关文章