How to call/use data returned by python function in javascript? -


i've defined function in views.py this

def ma_fonction_bokeh(request, numero = none):     cdr_common = settings.dbcon[settings.mongo_cdrstats['cdr_common']]     acc = cdr_common.find()     donnees = [] # on initialise une liste     in acc:         if i['accountcode'] == numero:             donnees.append([calendar.timegm(i["start_uepoch"].timetuple()), i["duration"]])      data = simplejson.dumps(donnees)      return render(request,"frontend/mongraphe.html", {"name": numero, "data": data}) 

then in mongraphe.html file i've created javascript code plot got http://www.highcharts.com/stock/demo/dynamic-update want customize plot own data returned function defined @ first... how can that? i've tried ajax function without result

you can transfer data python code javascript in several ways.

the "proper" way create endpoint returns json data , use ajax request (for example, jquery.getjson(…)) request it.

a quicker way modify template (frontend/mongraphe.html) include json data variable. since didn't specify python library you're using, i'm going assume you're using django. here's add template make data available in javascript:

<script> var data = {{data}}; </script> 

make sure put <script> tag above place before try access data. {{data}} part output json string created in python script final output var data = {"hello": "world"};

now should able access data variable in javascript , put data highcharts.js chart.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -