Search code examples
javascriptflaskerror-handlingjinja2

How to assign javascript array as jinja variable?


I am trying to assign javascript array as jinja variable but it throws error

this is what it shows

Python code

  • i want the data to be taken as a array

Solution

  • You can simply pass your list to the template. You can do without the conversion to JSON here.

    return render_template('game.html', game=game, board=board)
    

    To then use this variable in javascript, you can use the Jinja2 filter tojson. The result is your list as an array.

    window.onload = start({{ board | tojson }});