這篇有投影片可下載, 其中有些參考資源不錯
例如一些 python web framework 的比較, 幾個 hello world 範例
這篇有簡單的範例, 程度差不多就是 1+1=2 的水準
main.py
#!/usr/bin/env python from bottle import route, run, template, debug debug(True) # this will be the dictionary returned by the ajax call. # Bottle convert this in a json compatibile string. items = {1: 'first item', 2: 'second item'} # a simple json test main page @route('/') def jsontest(): return template('json') @route('/getallitems.json') def shop_aj_getallitems(): return (items) run(host='localhost', port=8080)
json.tpl
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type="text/javascript"> var xmlhttp; // Are we using a modern browser or ... if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // This will render the two output which substitute the // elements id="raw" and id="forin" function GetItems() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { // var jsonobj = eval ("(" + xmlhttp.responseText + ")"); var jsonobj = JSON.parse(xmlhttp.responseText); var output = xmlhttp.responseText; document.getElementById("raw").innerHTML = output; output = ""; for (i in jsonobj) { output += '<p>'; output += i + " : " + jsonobj[i]; output += '</p>'; } document.getElementById("forin").innerHTML = output; } else { alert("data not available"); } } // xmlhttp.onreadystatechange = GetArticles; // the GetItems function will be triggered once the ajax // request is terminated. xmlhttp.onload = GetItems; // send the request in an async way xmlhttp.open("GET", "/getallitems.json", true); xmlhttp.send(); </script> </head> <body> <p>The raw result from the ajax json request is:</p> <div id="raw"></div> <br /> <p>The for cycle produces :</p> <div id="forin"></div> </body> </html>
執行: python main.py
另外這篇還有教 MVC 的寫法, 不過現在用不到 @@
沒有留言:
張貼留言