Google App Engine: Twice as Many Randomish Numbers with webapp Framework
May 8, 2008Previous app engine fun resulted in a simple online randomish number generator. This is all well and good but this example didn’t make any use of the webapp framework and, really, don’t the users really deserve twice as many random numbers?
So starting with the previous python to generate random numbers the first step is to import the corresponding code:
import wsgiref.handlers from google.appengine.ext import webapp
Next define a class for the main page. This is / or entry web page for the application. In this case it’s simply some URLs that point to versions one and two of the randomish number generators. This is called with a request handler. Note that the two versions are linked to a specific URLs, in this case /1 and /2.
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = ‘text/html’
self.response.out.write(”)
self.response.out.write(”)
self.response.out.write(’Randomish Numbers: ‘)
self.response.out.write(’<a href=/1 target=”_blank”>’)
self.response.out.write(’<b>version 1</b></a>, ‘)
self.response.out.write(’<a href=/2 target=”_blank”>’)
self.response.out.write(’<b>version 2</b></a><p>’)
self.response.out.write(’<a href=http://boldlentil.wordpress.com target=”_blank”>’)
self.response.out.write(’<b>Bold Lentil</b></a><p>’)
Once the MainPage is defined the next class to define is the version 1 page. This page generates 25 randomish numbers. w00t!
class version1Page(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = ‘text/html’
self.response.out.write(”)
self.response.out.write(”)
self.response.out.write(’<a href=http://boldlentil.wordpress.com target=”_blank”>’)
self.response.out.write(’<b>Randomish Numbers</b></a><p>’)
stop = 25
i = 1
while i <= stop:
self.response.out.write(random.random())
self.response.out.write(’<br>’)
i = i + 1
After the version 1 page, the next step is to the version 2 page. This page generates 50 randomish numbers. Woot! Woot!
class version2Page(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = ‘text/html’
self.response.out.write(”)
self.response.out.write(”)
self.response.out.write(’<a href=http://boldlentil.wordpress.com target=”_blank”>’)
self.response.out.write(’<b>Randomish Numbers</b></a><p>’)
stop = 50
i = 1
while i <= stop:
self.response.out.write(random.random())
self.response.out.write(’<br>’)
i = i + 1
Finally what event loop would be complete without a main()? Note the tuples inside of WSGIApplication that link /1 and /2 to version1Page and version2Page respectively.
def main():
application = webapp.WSGIApplication(
[('/', MainPage),
('/1', version1Page),
('/2', version2Page)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == “__main__”:
main()
Revise the app.yaml with the new version number and then upload. Note that after uploading the code it will be necessary to use the versions menu item to switch the default to the new version.
The final result is here.
Tags: App Engine, coding, google, GQL, randomish numbers

May 8, 2008 at 2:14 pm
Now you need to get it to generate 3 sets of random numbers between 0 and 255, use those for RGB values, and send that color to your color naming app.
May 9, 2008 at 8:38 am
Nah I’ve other ideas for the random numbers… stay tuned.
May 9, 2008 at 9:16 am
First weird stencils and now random numbers and code, what kind of crazy blog is this?
ooo…Random stencils!
May 9, 2008 at 10:40 am
It’s a phase. Just be glad the number two viewed post for the past couple days (make your own toilet plunger thingy) was a short one…