How to pass a variable that hold data into datatables js?
How to pass a variable that hold data into datatables js?
jiamaozheng
Posts: 10Questions: 1Answers: 0
From python code: data = json.dumps(json.loads(pandas.DataFrame(data).to_json(orient='records')))
How to pass data variable into the datatables javascript:
"ajax": { "url": "??????????", "dataSrc": '' }
Thanks,
This discussion has been closed.
Replies
I tried the following syntax, but it returns a empty table.
"ajax": {
"url":JSON.parse(data),
"dataSrc": ''
},
I've used Pandas but not enough to know it in detail. However I use Python with Datatables. Looks like you are converting the Pandas data to json with
.to_json
then decoding withjson.loads
then encoding again withjson.dumps
.I'm not sure what the data structure is with
pandas.DataFrame(data).to_json(orient='records')
. But you could potentially just return that to the Datatable.Are you using a Python based web server?
For ajax to work you will need a web server to return the data. Your ajax config would look like this:
"ajax": { "url":"/get_pandas_data", "dataSrc": '' }
/get_pandas_data
would be the URL your Python web server hosts to process the Pandas data and returns it with something like this:return pandas.DataFrame(data).to_json(orient='records')
The
"dataSrc": ''
option tells Datatables that the data is not in adata
object in the returned JSON.Hope this gets you started.
Kevin
Kevin,
The solution you provided works, a common way to get json data via ajax, but that is not what I want.
I would like to return
html
template and passdata
at the same time. So I need to pass that data variable to Datable js"ajax": { "url": "??????????", "dataSrc": '' }
.Yes, I used bottle python web framework.
I would be very appreciated if you could provide more hints. Thanks,
The
ajax.data
allows you to pass data to the server. The doc page has a few examples.Kevin
Hi Kevin, I will give it a try. Thank you very much for your help. Jiamao
Maybe you can use something like this, at first load the data which hold your URL and then use it in your next Ajax request:
In this example, you passed an URL parameter in the DataTables Java script which you can use for further processing.
I forgot to write that you can send data and templates at the same time. Just add them to the response of the first Ajax call and you have them:
Hi Tester2017, thanks for your help and advice. The template I mentioned is the HTML file like
.tpl
. I would be very appreciated IF you could provide some concrete examples. Thanks, JiamaoOK, I am sorry. I do not understand what you are obtaining. Maybe you can try to explain your question a little bit more with an abstract example of what you really want or else wait till someone pass who understand better your question.