/ Published in: Python
A simple Ajax database app built with the Karrigell web framework for Python; PyDbLite, a non-SQL Python database engine; and JQuery in the browser
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
## HTML and JQuery source: ## <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> <script src="http://code.jquery.com/jquery-1.4.4.js"></script><script type="text/javascript"> $(document).ready(function(){ $('#country').change(function(){ $country = ($(this).val()); $.get('mydata.py', { value : $country }, function(data){ $('#txtHint').html(data); }); }); }); </script> </head> <body> <form action=""> <select name="customer" id="country"> <option value="" id="select">Select a customer by country:</option> <option value="Argentina" class="list">Argentina</option> <option value="Canada" class="list">Canada</option> <option value="France" class="list">France</option> <option value="Germany" class="list">Germany</option> <option value="Mexico" class="list">Mexico</option> <option value="Sweden" class="list">Sweden</option> </select> </form> <br /> <div id="txtHint">Customer info will be listed here...</div> </body> </html> ## CSS for table: ## .customers { border-collapse:collapse; border:2px solid #B7AFA3; width:39%; border-bottom: 4px solid #6D929B; border-top: 3px solid #6D929B; margin-left:10px; margin-right:auto; margin-bottom:auto; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } td.row_even th { background:#fff; padding-left:5px; } tr.row_odd, td.row_odd { color:#000000; background-color:#DCF0F7; } ## Python script: ## #mydata.py #Import the PyDbLite and HTMLTags modules from PyDbLite import Base from HTMLTags import * #Store the value of the query string (the value sent via GET from the dropdown menu) q = QUERY['value'] #Create a database and insert database records. db = Base('address.pdl') db.create('<b>Customer ID</b>','<b>Company Name</b>','Address','City','Postal Code','Country','Phone','Fax',mode="override") db.open() db.insert('<b>ALFKI</b>','<b>Alfreds Futterkiste</b>','Obere Str. 57','Berlin',12209,'Germany','030-0074321','030-0076545') db.insert('<b>ANATR</b>','<b>Ana Trujillo Emparedados y helados</b>','Avda. de la Constitución 2222','México D.F.',5021,'Mexico','(5) 555-4729','030-0076545') db.insert('<b>ANTON</b>','<b>Antonio Moreno TaquerÃ���Ã�¯Ã�¿Ã�½Ã���Ã��Ã�ÂÂa</b>','Mataderos 2312','México D.F.',5023,'México','(5) 555-3932','030-0076471') db.insert('<b>AROUT</b>','<b>Around the Horn</b>','120 Hanover Sq.','London','WA1 1DP','UK','(171) 555-7788','(171) 555-6750') db.insert('<b>BERGS</b>','<b>Berglunds snabbköp</b>','Berguvsvägen 8','Luleå','S-958 22','Sweden','0921-12 34 65','0921-12 34 67') db.insert('<b>BLAUS</b>','<b>Blauer See Delikatessen</b>','Forsterstr. 57','Mannheim',68306,'Germany','0621-08460','0621-08924') db.insert('<b>BLONP</b>','<b>Blondesddsl père et fils</b>','24, place Kléber','Strasbourg',67000,'France','88.60.15.31','88.60.15.32') db.insert('<b>BONAP</b>','<b>12, rue des Bouchers</b>','Marseille',13008,'France','91.24.45.40','91.24.45.41') db.insert('<b>BOTTM</b>','<b>Bottom-Dollar Markets</b>','23 Tsawassen Blvd.','Tsawassen','T2F 8M4','Canada','(604) 555-4729','(604) 555-3745') db.insert('<b>BSBEV</b>','<b>B\'s Beverages</b>','Fauntleroy Circus','London','EC2 5NT','UK','(171) 555-1212','(171) 555-1214') db.insert('<b>CACTU</b>','<b>Cactus Comidas para llevar</b>','Cerrito 333','Buenos Aires',1010,'Argentina','(1) 135-5555','(1) 135-4892') db.insert('<b>CENTC</b>','<b>Centro comercial Moctezuma</b>','Sierras de Granada 9993','México D.F.',5022,'Mexico','(5) 555-3392','(5) 555-7293') db.commit() #Use Karrigell's HTMLTags module to build an HTML table for the data head = HEAD() head <= TITLE('Customer Records') head <= LINK(rel="Stylesheet",href="css/style.css") body = BODY() table = TABLE(Class="customers") #Iterate on the database records; return those where the query string (q) matches the records stored in Country recs = [ r for r in db if q == r['Country']] for record in recs: for f in db.fields: table <= TR(TD(f)+TD(record[f])) #This tricky-looking bit of code is entirely optional. I took it off of the HTMLTags page of the Karrigell Reference Manual. It, along with the css style sheet for this app, creates the striped effect on the table. classes = ['row_odd','row_even'] lines = table.get_by_tag('TR') for i,line in enumerate(lines): cells = line.get_by_tag('TD') for cell in cells: cell.attrs['Class'] = classes[i%2] #Append the table to the body of the HTML page, and then print the table. body <= table print HTML(head+body)