0

I have a simple code populating a list of string data from python/flask to the template. In the rendered html, strings are within ' instead of apostrophes or double quotes. It is not a problem for the plain text, but JS cannot decode it. Do you have any idea how to solve this problem?

app.py

def index():
  data = ['2021-01-13 05:35:01', '2021-01-13 05:40:02', '2021-01-13 05:45:01', '2021-01-13 05:50:02']
  return render_template('index.html', data=data)

index.html - code

<body>
  {{ data }}
</body>

<script>
  var x = {{ data }};
</script>

index.html - rendered html

<body>
[&#39;2021-01-13 05:35:01&#39;, &#39;2021-01-13 05:40:02&#39;, &#39;2021-01-13 05:45:01&#39;, &#39;2021-01-13 05:50:02&#39;]
</body>

<script>
  var x = [&#39;2021-01-13 05:35:01&#39;, &#39;2021-01-13 05:40:02&#39;, &#39;2021-01-13 05:45:01&#39;, &#39;2021-01-13 05:50:02&#39;];
</script>
Samto
  • 101
  • 7
  • You shouldn't HTML escape the characters you want to execute as JS code. That's not really a JS problem - needs to be handled on the backend side. – VLAZ Jan 13 '21 at 12:32
  • 1
    Does this help? https://stackoverflow.com/questions/3206344/passing-html-to-template-using-flask-jinja2 – Zsolt Meszaros Jan 13 '21 at 12:34

0 Answers0