passing python list to javascript -
i trying pass python list javascript function, doesn't work... think javascript function see 1 long string. 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() 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 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 this:
var printchart = function macrocallback(macro, args, chartinfo) { document.getelementbyid("chartinfo").innerhtml=chartinfo; var arlength=chartinfo.length; console.log("arlength is: ",arlength); (var i=0; i<arlength; i++) { console.log(chartinfo[i]); } }
and console prints every character of python list on seperate line, this:
[ " 2 0 1 5 etc...
i can't format above console.log output, every character on seperate line.
also length of array same length of total string, conclusion python list transformed javascript 1 long string...
i hope can me out!
you right, looping through string. because json strings. makes pretty easy pass data between different programming languages strings data types implemented in every programming language. since strings need decode string usable format/object. in javascript can use methodjson.parse()
.
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) (var i=0; i<obj.length; i++) { console.log(obj[i]); } }
Comments
Post a Comment