Skip to main content
 首页 » 编程设计

python之如何在 Bottle 模板中输出未转义的python列表

2024年11月01日10飞鱼

我有一个 Bottle 模板home.tpl

<script> 
function googleChart() {    
    google.load('visualization', '1', {packages: ['corechart']}); 
 
    function drawVisualization() { 
        // Create and populate the data table. 
 
        var data = google.visualization.arrayToDataTable({{net_data}}); 
        ...  
    } 
} 
</script>  
... 

这是 python 脚本的一部分:

test = "[['Year', 'Q1', 'Q2', 'Q3', 'Q4'], ['2005', 369193000.0, 342814000.0, 381182000.0,372208000.0], ['2006', 592291000.0, 721077000.0, 733361000.0, 1030717000.0], ...]" 
return template('home', net_data=test) 

我试过了:

test = "[[],[],[]]" 
test = [[],[],[]] 
test = json.dumps([[],[],[]]) 

html输出最终被编码成这样
[['年', 'Q1', 'Q2', 'Q3'], ...]

正确的做法是什么?

这是我期望的:

var data = google.visualization.arrayToDataTable([['Year', 'Q1', 'Q2', 'Q3', 'Q4'], ['2005', 369193000.0, 342814000.0, 381182000.0,372208000.0], ['2006', 592291000.0, 721077000.0, 733361000.0, 1030717000.0], ...]); 

请您参考如下方法:

正如 Bottlepy SimpleTemplateEngine 帮助说明的那样 http://bottlepy.org/docs/dev/stpl.html#inline-expressions

试试这个

    var data = google.visualization.arrayToDataTable({{!net_data}});