Server-side processing with Python and Oracle
Server-side processing with Python and Oracle
donci
Posts: 11Questions: 4Answers: 0
I want to include in my JS function a json response produces by a python file:
Up to now I have this:
JS:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server_processing.py"
} );
} );
Python:
...
def runQueries(self):
cursormeta = self.db.cursor()
cursormeta.execute("SELECT * FROM "+ _sTable)
res_tuple_list = cursormeta.fetchall()
json_res = json.dumps( { "data": res_tuple_list } , ensure_ascii=False, encoding="latin-1")
print(json.JSONEncoder().encode(json_res))
...
Is there anyway I can get a response from python script without Frameworks like Flask and without cgi?
This discussion has been closed.
Answers
AJAX sends a XMLHttpRequest to a webserver. For your Python script you would need some sort of framework. I use CherryPy. Its very easy to get a simple website running. I can post an example from my project if you are interested.
Kevin
Hi Kevin, if you could post an example from your project that would be very nice, thank you!
I think my cherrypy config is pretty basic and mostly default. Basically you need to create a
class
for your website. Use@cherrypy.expose
for any function that can be accessed via the web browser. Don't use@cherrypy.expose
for helper functions.I have many web pages but this example shows just one. The url to access this page is
http://<ip address>:8000/device_types
. Datatables then sends an ajax request tomanage_device_types
. That function performs the SQL query, any needed data manipulation and returns the result in JSON format.In my device_types.html I simply have this for the ajax request:
I don't have server side processing enabled. My tables are relatively small. Client side processing is preferred unless you have large tables and performance issues. I recommend you start with disabling server side processing until you get the basics working.
If you need to use server side processing then your Python scripts will need to support the request / response architecture described here:
https://datatables.net/manual/server-side
Post any questions.
Kevin
Thank you for your answer. However, my problem is exactly the server side processing.
I already worked with the examples in https://datatables.net/manual/server-side, and managed to make a connection with php and mysql (https://datatables.net/examples/server_side/simple.html), but now I need Python and Oracle.
I don't know how to make the Ajax get the response directly by producing it by Python.
In my Javascript the Python script is not "callable": I don't know what do I have to do to execute it locally by calling in JS:
So the Python file server_processing.py produces a JSON by calling a table in my Oracle DB. I want this JSON to be used in JS, but "ajax": "server_processing.py"
does not "call" the python script.
JS:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server_processing.py"
} );
} );
AFAIK the Python script needs to be deployed on a web server that supports CGI or WSGI in order to run from Javascript. Here is the Python doc:
https://docs.python.org/2/howto/webservers.html
You will need to set this up in your web server or use a framework to run your Python scripts.
How many rows are in your table that you are returning?
Kevin
Thanks, I will try that!
I have 6 Tables, the longest table has about million rows.