Google App Engine: Twice as Many Randomish Numbers with webapp Framework

May 8, 2008

Previous 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=https://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=https://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=https://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.

Advertisements

4 Responses to “Google App Engine: Twice as Many Randomish Numbers with webapp Framework”

  1. pfmagic Says:

    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.

  2. boldlentil Says:

    Nah I’ve other ideas for the random numbers… stay tuned.

  3. BleedSnoot Says:

    First weird stencils and now random numbers and code, what kind of crazy blog is this?

    ooo…Random stencils!

  4. boldlentil Says:

    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…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: