0

I am a bit confused, this is what my flask and JS looks like, wtime shows up correctly in the graph, but I cannot get {{wtech}} to work as a flask value in JS. Been racking my brain for quite some time on this. I would much rather use my Flask variable than having to resort to using JSON, as it is much easier to code and less time consuming.

It works perfectly fine for {{wtime}} (for the data) but will not with {{wtech}} for the labels

Flask:

wtech = ['Tech1','Tech2']
wtime = [3,4]

res = make_response(render_template("timeman.html",     wtech = wtech,
                                                        wtime = wtime))

Javascript:

  <script>
  var cty = document.getElementById("chartBig2").getContext("2d");

  var data = {
    labels: ['Tech1','Tech2'],
    datasets: [{
      label: "Data",
      fill: true,
      backgroundColor: ['#1d1d2e', '#2d2f54'],
      borderColor: '#d048b6',
      borderWidth: 2,
      data: {{wtime}}, }]};
  var myDough = new Chart(cty, {type: 'doughnut', data: data, options: gradientDoughOptionsConfiguration});
  </script>
  • 1
    Does this answer your question? [How to pass a list from Python, by Jinja2 to JavaScript](https://stackoverflow.com/questions/15321431/how-to-pass-a-list-from-python-by-jinja2-to-javascript) – Joe Apr 24 '20 at 16:28
  • 1
    https://sopython.com/canon/93/render-string-in-jinja-to-javascript-variable/ – Joe Apr 24 '20 at 16:28
  • 1
    https://stackoverflow.com/questions/11178426/how-can-i-pass-data-from-flask-to-javascript-in-a-template – Joe Apr 24 '20 at 16:29

1 Answers1

0

Thank you very much Joe, I had found both other post, but the one with the answer I was looking for is the one on sopython --> {{ value|tojson }}.

That is perfect and exactly what I was looking for.