Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

418
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda