Oracle, JSON and Python
Oracle, JSON and Python
donci
Posts: 11Questions: 4Answers: 0
What do I have to do in order to make testdb.js getting json response from the python script?
I don't really understand which server/framework should I use and why, so please explain it for a beginner in web-programming.
import cx_Oracle
import json
# Array of database columns which should be read and sent back to DataTables
_columns = ['L', 'col1', 'col2', 'col3', 'col4' ]
# Indexed column (used for fast and accurate table cardinality)
_indexColumn = "L"
# DB table to use
_sTable = "X.CATEGORY"
_dbinfo= dict(
ip = 'localhost',
port = 1521,
SID = 'xe',
user = "myuser",
passwd = "mypass",
)
class DataTablesServer:
def __init__(self):
self.db = cx_Oracle.connect(_dbinfo["user"], _dbinfo["passwd"], cx_Oracle.makedsn(_dbinfo["ip"], _dbinfo["port"], _dbinfo["SID"]))
self.runq = self.runQueries()
def runQueries(self):
cursormeta = self.db.cursor()
cursormeta.execute("SELECT * FROM "+ _sTable)
## collect all the data (fetch -> result is a list of tuples)
res_tuple_list = cursormeta.fetchall()
json_res = json.dumps( { "draw":1, "recordsTotal":100, "recordsFiltered":100, "data": res_tuple_list[:400] } , ensure_ascii=False, encoding="latin-1")
print(json.JSONEncoder().encode(json_res))
# Perform the server-side actions for DataTables
dtserver = DataTablesServer()
server_processing2.py
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="testdb.js"></script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
</tr>
</thead>
</table>
</body>
</html>
testdb.js:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server_processing2.py"
} );
} );
This question has accepted answers - jump to:
This discussion has been closed.
Answers
I can't help you with Python, but this example shows the components of a server-side process with HTML, JS and PHP:
https://datatables.net/examples/server_side/simple.html
Is there a reason you want to you Python versus the PHP code that is already available for Datatables. This page has example code:
https://github.com/DataTables/DataTables/tree/master/examples/server_side/scripts
Do you have restrictions to use your corporate standard web environment/servers? If you are restricted then you will need to determine how that environment can run Python scripts.
If you have the ability to use something other than your corporate standard then you can use a Python web framework. Choosing the web framework or how to run Pyhton scripts from your webserver is beyond the scope of this forum.
I mentioned to you, in this thread, that cherrypy is fairly easy to get started with and provided a snippet of code as an example.
Basic steps to modify my example:
1. Rename the
device_types
function totest
.2. The
test
function would return your html document including the JS. Put the document into thepublic/html
directory.3. Rename the
manage_device_types
function toget_oracle
.4. Change your ajax config to
"ajax": "get_oracle"
5. Take your
__init__
andrunQueries
functions, combine them (removeself.
) and place them in theget_oracle
function.6. In the
get_oracle
function add the linereturn json_res
.7. Change the class from
Testbed
toWebsite
.8. Change line 33 to
webapp = Website``.
9. Run the python script then browse to
http://localhost:8000/test`There are probably steps I missed but the above should get you close.
There are other types of frameworks like Flask and Django but they require more up front setup. I've seen other posts on the forum from people using Flask so you may want to search if you don't like cherrypy.
Kevin
Hallo Kevin,
Thank you very much, your script helped a lot. You are right that my question was a little bit beyond the scope of this forum, sorry for that.
Great, good luck. Feel free to post any questions and I'll try to help with the Python side. Don't think there are many regulars on the forum using Python with Datatables.
Kevin
Hi! I think I am getting there, but now I have another isse: I don´t know how to pass the request parameters to the server, so instead of getting the pagination etc done, I get the whole large table (>50 000 rows) from the server.to the browser, and by attempting to sort the columns it always returns the same original request.
js:
python (cherrypy):
This function should receive the parameters in
kwargs
from the web page :kwargs should have the parameters whether you use a GET or POST request.
You can
print kwargs
to verify. You can then pass**kwargs
to your query Class. For example:dtserverresponse = DataTablesServer().getJson(**kwargs)
. you will need to parse them in theDataTablesServer
Class. To facilitate passing the parameters you may want to change the function call torunQueries
, for example:In
runQueries
parse the parameters, execute the query then callgetJson
and return the value. Something like this:Also I think you need to return the
draw
value sent to you in the request in this line:Kevin