Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
Pass a Python list to JavaScript

I'm having some issues passing a Python list to JavaScript as an array. My code is below, and outputs a blank screen, with error: 'Uncaught ReferenceError: array is not defined'.

main.py

from flask import Flask, render_template
import json

app = Flask(__name__)

@app.route('/')
def my_view():
    array = [1, 2, 3, 4, 5]
    jsarray = json.dumps(array)
    return render_template('index.html', array=jsarray)

app.run()

index.html

<script>
    var data = JSON.parse(array)
    document.write(data)
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use the Jinja2 filter tojson. A conversion of the data to JSON within the route is not necessary. You can simply pass the list to the template.

@app.route('/')
def my_view():
    array = [1, 2, 3, 4, 5]
    return render_template('index.html', array=array)
<script>
   const data = {{ array | tojson }};
   console.log(data);
</script>
about 4 years ago · Juan Pablo Isaza Report

0

change your index.html to

index.html

<script>
    var data = JSON.parse({{ array }})
    document.write(data)
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!