简体   繁体   English

JS脚本中的模板变量注入

[英]Template variable injection in JS script

I have encountered a quite peculiar issue in my flask app trying to inject a template variable withing a Js script, I don't really understand what the problem is so here it goes.我在尝试使用 Js 脚本注入模板变量的 flask 应用程序中遇到了一个非常特殊的问题,我真的不明白问题出在哪里,所以就在这里。

I am specifically trying to inject two variables: a list of numbers and a list of strings (same length), so that I can plot the data on a chart.js plot.我特别想注入两个变量:一个数字列表和一个字符串列表(相同长度),以便我可以 plot chart.js Z32FA6E1B78A9D4028953E60564A2AA4 上的数据。

The problem is that when injecting a single variable (the list of numbers) the page renders the pie chart with no errors, but then when i add the second variable (list of strings) the page goes blank.问题是当注入单个变量(数字列表)时,页面呈现饼图没有错误,但是当我添加第二个变量(字符串列表)时,页面变为空白。

Here is the JS snippet:这是 JS 片段:

 <div class='chart'>
    <canvas id="myChart" width="400" height="400"></canvas>
    <script type="text/javascript">
    var data = {{values}};
    var labels = {{labels}};
    const ctx = document.getElementById('myChart').getContext('2d');
    const myChart = new Chart(ctx, {
        type: 'pie',
        data: {
            labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
            datasets: [{
                labels: '# of Votes',
                data: data,
                backgroundColor: [
                    'rgba(255, 99, 132, 1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255,1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderColor: [
                    'rgba(255, 99, 132, 1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                y: {
                    beginAtZero: true
                }
            }
        }
    });
    </script>

And flask:和 flask:

@app.route('/test')

def test():
    screener = Signals()
    major_news = screener.major_news()
    distribution = screener.industry_distribution(major_news)
    
    return render_template('test.html', labels=[str(key) for key in distribution], values=[distribution[key] for key in distribution])

Keep in mind that the variables have both been checked and they both contain what they should.请记住,变量都已经过检查,并且它们都包含它们应该包含的内容。

In the JS script the labels variable is doing nothing but still throws an error as soon as declared.在 JS 脚本中, labels变量什么也不做,但在声明后仍然会抛出错误。

Thanks for helping.感谢您的帮助。

Refering to this SO Answer https://stackoverflow.com/a/48237270/17798307参考这个 SO 答案https://stackoverflow.com/a/48237270/17798307

Flask provides a Jinja filter for this: tojson dumps the structure to a JSON string and marks it safe so that Jinja does not autoescape it. Flask 为此提供了 Jinja 过滤器:tojson 将结构转储到 JSON 字符串并将其标记为安全,以便 Jinja 不会自动转义它。

Please try to declare your variables like this:请尝试像这样声明您的变量:

var labels = {{labels | tojson}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM