Skip to content
Advertisement

Passing JSON data from Flask to JavaScript

I am trying to pass a JSON data from flask to JavaScript.

The code I tried is from:


The steps below are what I did :

  1. I first got my data from postgreSQL in Python
  2. I transformed the data format from DataFrame to JSON
JavaScript
  1. I modified @Ilya V. Schurov’s code
JavaScript

And this is my index.html file:

JavaScript

However, it kept showing the error

JavaScript

And the webpage(http://127.0.0.1:5000/) showing:

JavaScript

I spent almost 2 days on this problem, passing the json data to the JavaScript, but still don’t know how to solve this… Could anyone give me some suggestion on this?

Advertisement

Answer

In the template, you are using the variable json2:

JavaScript

but, when rendering, you are passing in variable data:

JavaScript

Replace json2 with data in your template:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement