Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a python script from which I pass an argument to execute a query in an Oracle Database

prov.py

#!/usr/local/bin/python2.7
import sys
import argparse
import cx_Oracle
import json
import cgi
import cgitb
cgitb.enable()

lst_proveedores=[{}]
conn_str = 'user/pass@DATABASE'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
qstr = qstr = """ select id_proveedor, nombre, tipo from mpc_proveedores where tipo = '%s' """ %sys.argv[1]
c.execute(qstr)
for row in c:
    record1 = {"id":row[0], "nombre":row[1],"tipo":row[2],"tipo":row[2]}
    lst_proveedores.append(record1)
json_string = json.dumps(lst_proveedores)
print json_string
conn.close()

I need to show the data in an HTML page.

index.html

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ajax_get_json(){
    var results = document.getElementById("results");
    var hr = new XMLHttpRequest();
    hr.open("GET", "prov.py", true);
    hr.responseType = "JSON";
    hr.setRequestHeader("Content-Type", "application/json", true);
    hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                    var data = JSON.parse(hr.responseText);
                        results.innerHTML = "";
                        for(var obj in data){
                                results.innerHTML += 
                                "<tr><td>"+data[obj].id+"</td><td>"+data[obj].nombre+"</td><td>" +data[obj].tipo+"</td></tr>";
                        }
            }
    }
    hr.send(null);
    results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div class="container">
<div class+"row">
<table  id="results" class="table table-bordered table-striped table-hover">
    <tr>
        <td>Id Proveedor</td>
        <td>Nombre</td>
        <td>Tipo</td>
    </tr>
<script type="text/javascript">ajax_get_json();</script>
</table>
</div>
</div>
</body>
</html>

How can I execute the script from a button, and how can I pass a value from an input text to javascript?

Also, I know my javascript is not well designed. Any advice?

share|improve this question
    
What web server and framework are you using? –  Ken Hampson Aug 1 at 22:12
    
I am using apache and python. I am not using any framework like Django. As database I am using Oracle 11g –  Joseleg Aug 1 at 22:26

2 Answers 2

To run Python code from Apache you need to use a proper interface for it. There are many posibilities but CGI or mod_wsgi are popular alternatives. The Python docs has lots of useful info about how to set it up.

share|improve this answer

I think what you really want is a web framework.

That would provide a structured approach to exactly the type of stuff you're trying to do.

django is great and very popular. Pyramid and Flask are also very popular.

I have a good amount of experience with Flask and highly recommend it.

share|improve this answer
    
There's a problem. I can't install for IT poitycs. I try it but doesn't work well. For that reason I try to work with oure Python. And I get stucked because I am new with Python and with AJAX. If you know another alternative that will be great. And thanks for your reply. –  Joseleg yesterday
    
I would talk to IT and see if they will install virtualenv for you. Then you can install whatever packages you need. Either that or download and build your own python from source. I've written a tutorial here that covers the process. Just skip the IPython parts. –  anders yesterday
    
Thanks @anders for the replies. Finally I have the permissions to install Django. And I start reading a tutorial. If you know some books or other resources where I can learn Django and flask that will great. –  Joseleg 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.