2

My python function:

def somefunction(self):
  x1 = ['reduced','fully automatic','years']
  return x1

In Flask app.py file

keyword = somefunction()   

JavaScript in html:

<script>
var javaword = '{{ keyword }}';
somefunction {
alert(typeof(javaword));
alert(javaword);
}

----------
Output -
String
[&#39;reduced&#39;, &#39;fully automatic&#39;, &#39;years&#39;]

I want the output as

Object
['reduced','fully automatic','years']

Solved

I figured it out and changed the following

var javaword =JSON.parse('{{ keyword|tojson }}');
Community
  • 1
  • 1

1 Answers1

1

Three things. Firstly, you need to send it as JSON, not a Python string; secondly, you need to disable Jinja2 autoescaping for that variable; and finally, your JS needs to parse it from a JSON string to a JS object. So:

return jsonify(x1)

...

var javaword = JSON.parse('{{ keyword|safe }}');
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895