Haroon Idrees

2008/12/18

Templates in google appengine

Filed under: appengine — haroonidrees @ 2:26 am

Externalizing html from core business logic is very good technique to create code simple and understandable. Embedded HTML in code is difficult to maintain. To Avoid embedded HTML in code python provides us different templating systems which are EZT, Cheetah, ClearSilver, Quixote, and Django , which are supported by google appengine. We use Django template engine in our example.

Create Model class with name message.py


from google.appengine.ext import db

class Message(db.Model):

content = db.TextProperty(required=True)

who = db.StringProperty()

when = db.DateTimeProperty(auto_now_add=True)

create main.py



class MainHandler(webapp.RequestHandler):

def get(self):

messages = db.GqlQuery(‘SELECT * FROM Message ‘

‘ORDER BY when DESC’).fetch(100)

values = {‘messages’: messages}

self.response.out.write( template.render(‘list.html’, values))

def main():

application = webapp.WSGIApplication([('/', MainHandler)], debug=True)

wsgiref.handlers.CGIHandler().run(application)

if __name__ == ‘__main__’:

main()

list.html


<head><title>…………..</title>

<style>

.even {

background-color:#DDDDDD

}

.odd {

background-color:#FFFFFF

}

</style>

</head>

<body>

<table width=”100%” cellspacing=”0″ cellpadding=”0″ >

{% for message in messages %}

<tr class=”{% cycle even,odd %}” >

<td colspan=”3″>

<table width=’100%’ border=1>

<tbody>

<tr class=”{% cycle even,odd %}”>

<td width=”20%” valign=”top”>Nick</td>

<td align=”right” width=”20%” valign=”top”>{{message.when}}</td>

</tr>

<tr height=”40″>

<td width=”20%” valign=”top”>{{message.who}} </td>

<td width=”80%” valign=”top”>{{message.content}}</td>

</tr>

</tbody>

</table>

</td>

</tr>

<tr height=”5px”><td colspan=’3′ align=’center’> </td></tr>

{% endfor %}

</table>

</body>

</html>

create app.yaml



application: template

version: 1

runtime: python

api_version: 1

– url: /.*

script: main.py

What the above code does

  1. In meesage.py file we define a model Message with three properties: content,who and when which plays a role of domain object and a db.This is part of App Engine not a python and It uses App Engine big table infrastructure which takes care of all of the distribution, replication and load balancing.

    for detail refrence of entities and model visit http://code.google.com/appengine/docs/datastore/

  2. In main.py we define MainHandler class which serve as request handler of all get requests.We use GQL in our code for getting all message model objects. After fetching all results we assign these meesage to values and reponse back to list.html page. self.response.out.write use for writing reponse and template.render method use for rendring list template.
  3. After that we create html file with the name of list.html .In this html file we use Django script for manupolation Message objects. {% for message in messages %} is just foreach like code structure and “{% cycle even,odd %}” uses for applying style class alternatively each time. you can use any number of values, separated by commas for alternatively.

Using google webapp framework in google appengine

Filed under: appengine — haroonidrees @ 2:23 am

Google includes python based web application framework with app engine.This framework pretty similar with MVC pattern.A webapp application has three parts:

  • one or more RequestHandler classes that process requests and build responses
  • a WSGIApplication instance that routes incoming requests to handlers based on the URL
  • a main routine that runs the WSGIApplication using a CGI adaptor.

Now the time to look at example code of webapp framwork.We are going to write webapp.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
self
.response.headers['Content-Type'] = 'text/plain'
self
.response.out.write('Hello World from the webapp framework!')

application
= webapp.WSGIApplication(
[('/', MainPage)],
debug
=True)

def main():
run_wsgi_app
(application)

if __name__ == "__main__":
main
()

our app.yaml


application: webapptest

version: 1

runtime: python

api_version: 1

handlers:

– url: /

script: webapp.py

http://code.google.com/appengine/docs/webapp/

What the above code does

  1. The webapp module is in the google.appengine.ext
  2. This code defines one request handler, MainPage, mapped to the root URL (/).We can map diffrent urls with diffrent handlers
  3. When webapp receives an HTTP GET request to the URL /, it instantiates the MainPage class and calls the instance’s get method. Inside the method, information about the request is available using self.request. Typically, the method sets properties on self.response to prepare the response.

Now copy the webapptest folder to google appengine folder and type localhost:8080/ !

2008/12/17

learning google appengine in 5 minutes

Filed under: appengine — haroonidrees @ 4:11 pm

Creating an App Engine application is easy, and only takes a few minutes.

First you need to download the following packages

  1. Python www.python.org/download/releases/2.5.2/
  2. SDK www.code.google.com/appengine/downloads.html

Creating a Simple Request Handler

Create a directory named helloworld. All files for this application reside in this directory.Inside the helloworld directory, create a file named helloworld.py, and give it the following contents:

print 'Content-Type: text/plain'  
print
'' print 'Hello, world!'


This Python script responds to a request with an HTTP header that describes the
content, a blank line, and the message Hello, world!.

Creating the Configuration File

An App Engine application has a configuration file called app.yaml. Among other things, this file describes which handler scripts should be used for which URLs.

Inside the helloworld directory, create a file named app.yaml with the following contents:

application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: helloworld.py

Testing the Application

google_appengine/dev_appserver.py helloworld

Type the following URL and enjoy!

http://localhost:8080/

Reference
http://code.google.com/appengine/docs/gettingstarted/

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.