如何将一个字符串列表从flask python main函数传递给html中的javascript?

时间:2017-03-15 21:30:07

标签: javascript python html flask mustache

我正在使用烧瓶建立一个网站。在app.py文件中,我计算了一些传递给html页面的统计信息。

@app.route('/')
def index():
    values = [10, 11, 7, 15, 19, 5, 7.5, 10.9]
    days= ["12/18/18","12/25/18","1/1/19","1/8/19","1/15/19","1/22/19","1/29/19","2/5/19"]

return render_template('index.html', days = days, values=values)

index.html中的javascript如下

var ctx = document.getElementById("lineChart");
var lineChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: {{days}}
    datasets: {
      label: "All users",
      backgroundColor: "rgba(30,144,255, 0.31)",
      borderColor: "rgba(30,144,255, 0.7)",
      pointBorderColor: "rgba(30,144,255, 0.7)",
      pointBackgroundColor: "rgba(30,144,255, 0.7)",
      pointHoverBackgroundColor: "#fff",
      pointHoverBorderColor: "rgba(220,220,220,1)",
      pointBorderWidth: 1,
      data: {{values}}
    }
  },
});

我在javascript中测试了两个选项:

  1. 不工作(无法生成图表)

    labels: {{days}}
    data: {{values}}
    
  2. 如果我明确提供字符串列表

    ,则工作
    labels: ["12/18/18","12/25/18","1/1/19","1/8/19","1/15/19","1/22/19","1/29/19","2/5/19"]
    data: {{values}}    
    
  3. 似乎传递数值数组(本例中的值)很好,但传递字符串列表会带来麻烦。

    您有什么建议可以解决这个问题吗? 非常感谢!

0 个答案:

没有答案