Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing python list to javascript

I am trying to pass a python list to a javascript function, but it doesn't work... I think the Javascript function will see it as one long string. This is what I do:

@webiopi.macro
def calcSunriseSunsetOneYear():
    o=ephem.Observer()
    o.lat='51.21828'
    o.long='3.94958'
    s=ephem.Sun()
    d=datetime.date.today()

    t=timedelta(days=1)
    d=d+t
    o.date=d

    riseTime=[]
    setTime=[]
    s.compute()
    for x in range (0,365):
        o.date=o.date+1
        riseTime.append(str(ephem.localtime(o.next_rising(s))))
        setTime.append(str(ephem.localtime(o.next_setting(s))))
    return(json.dumps(riseTime))

This is the python data:

["2015-03-22 06:43:02.000006", "2015-03-23 06:40:46", "2015-03-24 06:38:31.000001", "2015-03-25 06:36:15.000002", "2015-03-26 06:33:59.000003", "2015-03-27 06:31:44.000004", "2015-03-28 06:29:28.000005", "2015-03-29 07:27:13.000006", "2015-03-30 07:24:57", "2015-03-31 07:22:42.000001", "2015-04-01 07:20:27.000002", "2015-04-02 07:18:13.000003", "2015-04-03 07:15:58.000004", "2015-04-04 07:13:44.000005", "2015-04-05 07:11:31.000006", "2015-04-06 07:09:17", "2015-04-07 07:07:04.000001", "2015-04-08 07:04:52.000002", "2015-04-09 07:02:40.000003", "2015-04-10 07:00:28.000004"]

In Javascript I do this:

var printChart = function macroCallBack(macro, args, chartInfo) {
        document.getElementById("chartInfo").innerHTML=chartInfo;
        var arLength=chartInfo.length;
        console.log("arLength is: ",arLength);
        for (var i=0; i<arLength; i++) {
            console.log(chartInfo[i]);
        }
    }

And the console prints every character of the python list on a seperate line, like this:

[ " 2 0 1 5 etc...

I can't format the above console.log output, but every character is on a seperate line.

Also the length of the array is exactly the same as the length of the total string, so my conclusion is the python list is transformed to Javascript as one long string...

I hope someone can help me out!

like image 873
Marco Avatar asked Nov 20 '25 20:11

Marco


1 Answers

You are right, you are looping through a string. This is because json are strings. This makes it pretty easy to pass data between different programming languages as strings are data types implemented in almost every programming language. However since it are strings you need to decode the string to a usable format/object. In javascript you can use the methodJSON.parse().

jsfiddle demo

var frompythonjsonstring ='["2015-03-22 06:43:02.000006", "2015-03-23 06:40:46", "2015-03-24 06:38:31.000001", "2015-03-25 06:36:15.000002", "2015-03-26 06:33:59.000003", "2015-03-27 06:31:44.000004", "2015-03-28 06:29:28.000005", "2015-03-29 07:27:13.000006", "2015-03-30 07:24:57", "2015-03-31 07:22:42.000001", "2015-04-01 07:20:27.000002", "2015-04-02 07:18:13.000003", "2015-04-03 07:15:58.000004", "2015-04-04 07:13:44.000005", "2015-04-05 07:11:31.000006", "2015-04-06 07:09:17", "2015-04-07 07:07:04.000001", "2015-04-08 07:04:52.000002", "2015-04-09 07:02:40.000003", "2015-04-10 07:00:28.000004"]';

macroCallBack(frompythonjsonstring);

function macroCallBack (str) {
        obj = JSON.parse(str)

        for (var i=0; i<obj.length; i++) {
            console.log(obj[i]);
        }
}
like image 92
kasper Taeymans Avatar answered Nov 23 '25 09:11

kasper Taeymans