Use Cheetah Templates with Google App Engine

Community help for Google App Engine has been less than stellar because nobody takes a moment to explain how to do the supposedly “basic” python stuff. It took me a long time but I finally figured out the process to install Cheetah Templates for use with Google App Engine.

Here is what I had to do on Mac OS X 10.5:

  • run the install command:
python setup.py install —install-lib /path/to/myapp/

Running the command above compiled and installed Cheetah templates into a new Cheetah subdirectory within my single Google App Engine application. For Mac the install path was similar to /Users/bart/myapp/Cheetah.

Now, you can import Cheetah in your Google App Engine project! In the Google Documentation example helloworld.py I would put the Cheetah include line near the top of the file with the other includes

from Cheetah.Template import Template

Then, to actually have a Cheetah Template return something, you would use something like this to the MainPage class to output the rendered template. This is a more complete example.

from Cheetah.Template import Template #woohoo! Cheetah!
import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
class MainHandler(webapp.RequestHandler):
def get(self):
        #find the desired file
        path = os.path.join(os.path.dirname(file), ‘templates/cheetahtest.tmpl’)
        user = users.get_current_user()
        template_values = { ‘user’:user,}
        #render template with values inserted
        tmpl = Template( file = path, searchList = (template_values,) )
        #output the template to screen
        self.response.out.write(tmpl)

My template (i.e. templates/cheetahtest.tmpl) might contain this:

<p>$user is using Cheetah</p>

Let us assume that the logged in user is ‘Jeff’ for sample output. The rendered output would be:

<p>Jeff is using Cheetah</p>

Notes

If you have more applications you need to copy or re-install the Cheetah directory into each different application individually. You need to do this because the applications get deployed to Google servers and will not have access to each others’ libraries.

Google says that App Engine works with “Pure Python” extensions and the process is simply to upload your extensions with the app. Since I put the install in /path/to/myapp/Cheetah I presume you can put other extensions and/or frameworks in there as well. Just download the Python source and configure/compile them to go into the app’s folder.

Why Cheetah?

Why did I go through this? First, I do not like the Django templating system. I have gotten accustomed to Cheetah because I use it at work. Second, Django’s hardline stance is to separate logic from presentation. Yes, the Django approach works for many but I like running loops and instantiating usable variables within my templates, a luxury afforded by Cheetah. Finally, Cheetah’s syntax is much closer to regular Python than the Django template syntax.

Rice rationing in Daly City?

Last week I went to Costco to buy a bag of rice for my friend. To my confusion, there was not a single bag of rice where once there usually are 20 bags each of 4 or 5 different varieties and sizes. I noticed a couple Chinese standing near the missing rice area talking and they asked a couple Costco employees the whereabouts of the rice.

Not paying much attention to what was going on I did the rest of my shopping. A few minutes later some Costco employees came out with pallets of rice and out of nowhere half the people in Costco were swarming over him trying to take the rice off the cart before he put up the price and the 2 bag limit. “Hold on people, don’t touch anything until I set this on the floor!” he yelled. Near pandemonium ensued as people grabbed up two 50-pound bags each. It was like a scene from a movie where aid workers show up with food and all the people swarm in a mad dash to get food just to survive another day.

Since I don’t frequently buy rice, I thought maybe it was on special. But $16 for a 50 pound bag didn’t seem like a fantastic deal to me. What I didn’t know at the time is that there is a worldwide rice shortage going on. This morning my coworker sent me a link to this NY Sun article: Food rationing confronts breadbasket of the world. Stores are limiting rice along the West and East coast and countries like India and Vietname are halting exports of their rice.

Unfortunately for me, I didn’t know that I personally am low on rice and I should have bought the extra 50-pound bag. It looks like I’ll be eating a different kind of carb for a while until the rice shortage works itself out.

Google App Engine on a machine with Python 2.4 and Python 2.5

I want to try using the recently released Google App Engine on my Mac OS X 10.5.2 Leopard machine.

The SDK requires Python 2.5 but I need Python 2.4 because of work. The webapp framework included with the App Engine SDK requires the wsgi module which is part of Python 2.5 but not in Python 2.4. Consequently the Hello World application in the Google App Engine tutorials will not work.

I am trying to get around the version problem and I think I am getting close.

Useful knowledge: the which command will tell you the path for executables that you run. Useful in the case you have the same executable in different locations. which python give /opt/local/bin/python

Update 5/10/08 method 3

Another user just sent me this:

find your path first

echo $PATH

/Users/ryan/bin:/usr/local/bin:/opt/local/bin: /opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin: /usr/local/bin:/usr/X11/bin

now route your path to find 2.5, which i believe is on our macs already from the leopard update

export PATH=/Users/ryan/bin: /usr/local/bin: /usr/bin:/bin: /usr/sbin: /sbin: /usr/local/bin: /usr/X11/bin

Update 5/8/08: method 2

Rather than compiling your own Python using the method above, my friend told me he followed the README included with the App Engine SDK. Following that he was able to get Python 2.5 by just downloading it from the Python website and running the installer. Then he ran the Google installer.

<div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;">INSTALLING ON Mac OSX
=================
1) Download and install Python 2.5 from http://www.python.org/download/
2) Download the SDK installer from http://code.google.com/appengine/downloads
3) Install the SDK by double-clicking on the GoogleAppEngine.dmg file and running the installer.

To run it you would use this

python2.5 /usr/local/google_appengine/dev_appserver.py \
<app location>

Update April ’08: method 1

Rather than trying to trick Google App Engine SDK into cooperating with Python 2.4, a guy in the UK has handy instructions for getting things to work without trying to have 2 globale versions of Python. Instead, I can have a global Python (2.4) and a local version in my home directory (2.5) for tinkering with the SDK.

Down the wrong path

Do not do it this way because it doesn’t work. I found an ONLamp article that introduces the use of WSGI. Apparently you can checkout WSGI and run it on Python 2.4. Simply checkout the library and copy the entire WSGI directory to your python site-packages directory.

svn co http://svn.python.org/projects/python/trunk/Lib/wsgiref

The site-packages directory on my Mac OS X 10.5 system is

/opt/local/lib/python2.4/site-packages
Syndicate content