Server-side processing with Python and Oracle

Server-side processing with Python and Oracle

doncidonci 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?

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    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

  • doncidonci Posts: 11Questions: 4Answers: 0

    Hi Kevin, if you could post an example from your project that would be very nice, thank you!

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    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 to manage_device_types. That function performs the SQL query, any needed data manipulation and returns the result in JSON format.

    import cherrypy
    
    class Testbed:
    
    .....
    
        #display device_types.html
        @cherrypy.expose
        def device_types(self, *args, **kwargs):
            with open('public/html/device_types.html') as f:
                form = f.read()
            return form  #opens and returns the html page
    
        #ajax request to this URL, respond with JSON string
        @cherrypy.expose
        def manage_device_types(self, *args, **kwargs):
            result = SQL query result
            return json.dumps('data': result)
    
    .....
    
    if __name__ == '__main__':
        conf = {
            '/': {
                'tools.sessions.on': True,
                'tools.staticdir.root': os.path.abspath(os.getcwd())
            },
            '/static': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': './public'
            }
        }
        webapp = Testbed()
        cherrypy.config.update(
            {'server.socket_host': '0.0.0.0',
             'server.socket_port': 8000,
             'log.screen': True,
             'log.error_file': '',
             'log.access_file': ''
             }
        )
        cherrypy.quickstart(webapp, '/', conf)
    

    In my device_types.html I simply have this for the ajax request:

        var table = $('#edit-table').DataTable( {
            ajax: '/manage_device_types',
           .....
           } );
    

    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

  • doncidonci Posts: 11Questions: 4Answers: 0

    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"

    } );
    } );

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    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.

    However, my problem is exactly the server side processing.

    How many rows are in your table that you are returning?

    Kevin

  • doncidonci Posts: 11Questions: 4Answers: 0

    Thanks, I will try that!
    I have 6 Tables, the longest table has about million rows.

This discussion has been closed.