Writing a trivial application to display "Hello, world!" is something of a computing tradition but it's surprisingly educational.

Application Structure

We are going to implement a simple application but we'll wind up with a lot more potential functionality than we'll use. In its simplest form, this entire page could be rendered using just the html:

<head>
 <head>
 </head>
 <body>
  Hello, World!
 </body>
</html>

Static pages like this aren't useful in an interactive sense. Making an Albatross application to do this is a little more complicated because we need to initialise a lot of extra machinery even though we won't use the bulk of it.

File Organisation

We'll use a model-view-controller to drive the application even though it's overkill. This is immediately simplified because it's a display-only application so there are no data models so we'll only need a view and controller.

XXX Pretty diagram of file structure here:

Here's the main app in hello.cgi:

   1 #! /usr/bin/env python
   2 
   3 from albatross import RandomModularSessionApp, SessionAppContext
   4 from albatross.cgiapp import Request
   5 
   6 class App(RandomModularSessionApp):
   7     def __init__(self):
   8         RandomModularSessionApp.__init__(self,
   9                                          base_url='hello.cgi',
  10                                          page_path='pages',
  11                                          start_page='hello',
  12                                          secret='-=-secret-=-',
  13                                          session_appid='random')
  14 
  15     def create_context(self):   # XXX is this necessary?
  16         return SessionAppContext(self)
  17         
  18 if __name__ == '__main__':
  19     app = RandomModularSessionApp(base_url='hello.cgi',
  20                                   page_path='pages',
  21                                   start_page='hello',
  22                                   secret='-=-secret-=-',
  23                                   session_appid='random')
  24     app.run(Request())

This is the model part of the app where we display to the user (???/hello.py):

   1 def page_display(ctx):
   2     ctx.locals.message = 'Hello, world!'
   3     ctx.run_template('hello.html')

And here's what the html to display (in html/hello.html):

<html>
 <head>
 </head>
 <body>
  <al-value expr="message">
 </body>
</html>

None: "Hello_world"_example (last edited 2011-02-15 06:05:18 by localhost)