Skip to content
Advertisement

List of list from flask to JS

My Python code calculate coordonates of devices and put it in a list. So i get a list of list in a python var my_devices_list = [ ["name1", 11.1, 22.2] , ["name2", 33.3, 44.4] ]

i’m trying to pass this list to my JS code

my python code :

JavaScript

my JS code :

JavaScript

the alert I get from the JS alert :

JavaScript

What i would like to do: a for loop to place waypoints on my map like:

JavaScript

I know that the following code works, but i can’t get a loop that way

JavaScript

I try to pass the list from the python code using json.dumps(), but i can’t loop on it in my JS code

JavaScript

Thanks for your help!!

Advertisement

Answer

Instead of attempting to format the list as an array in the JS file, you can send a request to the web app via AJAX to get the proper jsonified response. First, update your routes to include another route that receives the AJAX request and returns the result:

JavaScript

Then, in your JS file (after adding <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>) to your HTML header):

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