convert python list to javascript array

894 views
Skip to first unread message

1904

unread,
Dec 2, 2010, 10:12:19 AM12/2/10
to web2py-users
Hello,

i m totally new to web2py and im trying to use it with google maps api
and javascript to build a map.
So I got some longtiudes and latidues from a MySQL database.
The problem is that I cant convert the Python list [with the lat and
lng values] from the database into a javascript array which is needed.
I build the list with the values in a controller file and return it to
the view file.

Python list: coordinates((1,2) , (3,4) , (5,6))

I tried it on this way:

while(j<=len()) //len = length of coordinates
{
var coor[{{=x}}][0] = {{=coordinates[y][0]}};
var coor[{{=x}}][1] = {{=coordinates[y][1]}};
j++;
}

but it is not possible because Y doesnt raise... So the question is
how can I use JS variables and/or Python variables together?


Thanks for help

Josh Jaques

unread,
Dec 2, 2010, 5:45:07 PM12/2/10
to web...@googlegroups.com
Sounds like you're looking for response.json

def test():
coords = ((1,2), (3,4), (5,6))
return response.json(coords=coords)

Returns a javascript array formatted like: [[1, 2], [3, 4], [5, 6]]

Or you can use response.json(dict(coords=coords)) to get a javascript object:
{"coords": [[1, 2], [3, 4], [5, 6]]}

Note that you also don't have to immediately return the result of response.json, you can save it and output it in the view, inside a <script> tag, for example

Hello,


Thanks for help
This communication, including any attachments, does not necessarily represent official policy of Seccuris Inc.
Please see http://www.seccuris.com/Contact-PrivacyPolicy.htm for further details about Seccuris Inc.'s Privacy Policy.
If you have received this communication in error, please notify Seccuris Inc. at in...@seccuris.com or at 1-866-644-8442.

1904

unread,
Dec 9, 2010, 5:53:05 AM12/9/10
to web2py-users
Thank u very much =)
> Please seehttp://www.seccuris.com/Contact-PrivacyPolicy.htm for further details about Seccuris Inc.'s Privacy Policy.
> If you have received this communication in error, please notify Seccuris Inc. at i...@seccuris.com or at 1-866-644-8442.
>
>
Reply all
Reply to author
Forward
0 new messages