Originally Published: Monday, 2 October 2000 Author:
Published to: develop_articles/Development Articles Page: 1/1 - [Printable]

Unix Web Application Architectures - Part 5: An Alternate Approach

In this last chapter of the document I'll mention a completely different approach to web application development. I call this style "Abstracted HTML."

   Page 1 of 1  

Unix Web Application Architectures
1. Introduction and Basic Approaches
2. The Web Server
3. Sessions, Authentication, and Databases
4. Other Issues
5. An Alternate Approach

15 An Alternate Approach: Abstracted HTML

In this last chapter of the document I'll mention a completely different approach to web application development. I call this style "Abstracted HTML."

In this approach, web programming is made to feel to the programmer as much like traditional GUI programming as possible. HTML elements and constructs are wrapped in persistent objects that know how to render themselves using the browser, and HTTP requests are handled as GUI callbacks. The objects are kept in memory or stored on disk between requests. When a callback for an object arrives, the appropriate method of the object is invoked.

Below is the commented source of that application. Notice that the code is short and easy to understand even for someone who has never done web programming. The question is, does this approach scale to larger programs, or is the web technology too different from traditional GUI programming to make this viable?

    #!/usr/bin/python

    Import the Web Application Toolkit module.
    import wat

    Create a new class which subclasses wat.Application. All WAT apps must derive this class and override the construct method.
    class TextViewer(wat.Application):

      Override the construct method, which returns a widget to the caller that represents how the application will render itself.
      def construct(self):

        Create a centered dialog object with the title "Text Viewer".
        dialog = wat.Dialog(self, "Text Viewer", align = "center")

        Create a text box widget (a form input control).
        self.file_input = wat.TextBox(self) Bind the "changed" event to the display_file method. This means when the user hits enter, self.display_file will be called.
        self.file_input.connect("changed", self.display_file) Insert the input control created above into the dialog box at position 0, 0.
        dialog.form.set_cell(0, 0, self.file_input)

        Create a button on the right of the text box, and bind also its "clicked" event to self.display_file.
        button = wat.Button(self, "Display File")
        button.connect("clicked", self.display_file)
        dialog.form.set_cell(1, 0, button, width = "100%")

        Insert a Text widget containing <hr> at position 1,0 to create a horizontal line below the two widgets above.
        dialog.form.set_cell(0, 1, wat.Text(self, "<hr>"), colspan = 2)

        Store the dialog so that we can access it in other methods, and return it to the caller.
        self.dialog = dialog
        return dialog

      The display_file method is called when the user clicks the Display File button or hits enter in the input control. This is a user-defined method and was bound to those events in the construct() method. The o parameter is the object which this method was connected to. If the user clicks the button, o will be the Button widget. If the user hits enter in the input control, o will be the Input widget. We don't actually use it below, but Python requires it to be in the parameter list.
      def display_file(self, o):

        Create a new Text control that will hold the contents of the file.
        contents = wat.Text(self)
        try:

          self.file_input is our input control. It has an attribute 'value' which holds the text in the input control. Here we open the filename in this value and stuff the lines into the text attribute of the contents text widget.
          for line in open(self.file_input.value).readlines():
            contents.text = contents.text + line + "<br>"
        except:
          We're here if the file open failed (permission denied, say).
          contents.text = "Unable to open file:" + self.file_input.value

        Finally insert the contents widget at position 0,2 in the dialog (below the horizontal line).
        self.dialog.form.set_cell(0, 2, contents, colspan = 2)

    Start the program.
    app = TextViewer()
    app.run()

Copyright (c) 2000 by Samuli Kärkkäinen <skarkkai@woods.iki.fi>. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).

Up-to-date versions of the entire Unix Web Application Architectures document is available at webapparch.sourceforge.net.





   Page 1 of 1