0

I'm building a website using the Python Flask Framework which includes the Jinja2 templating engine. I now want to pass a simple list of strings from Flask to Jinja2 to be included in a Javascript in the website. Using the tips in this SO answer I tried doing this as follows:

@app.route('/index')
def index():
    theList = ['A', 'B']
    return render_template('index.html', theList=json.dumps(theList))

and in the template its Javascript I do this:

var theList = {{ theList }};

But when I look into the source of the generate Javascript I see this:

var theList = ["A", "B"];

I also tried theList=map(json.dumps, theList)), which results in the even weirder:

var theList = ['"A"', '"B"'];

Does anybody know how I can properly pass a simple list through Jinja2 on to a Javascript array? All tips are welcome!

Community
  • 1
  • 1
kramer65
  • 50,427
  • 120
  • 308
  • 488

0 Answers0