wsgiservlets.HTMLPage

HTMLPage generates well-formed HTML documents and has many methods and attributes to make generating web pages and processing form data easy.

Here’s a simple example:

from wsgiservlets import *

class helloworld(HTMLPage):
    title = 'Hello, World!'
    css = 'strong {color: red}'

    def write_content(self):

        self.writeln("Here's a simple demo to say "
                     "<strong>Hello, World</strong>!")

The helloworld servlet will generate a HTML page with title Hello, World! and writes to the page:


Here’s a simple demo to say Hello, World!

The tutorial that comes with the distribution has several examples of how to exploit the attributes and methods of HTMLPage to generate dynamic code simply!

API:

class wsgiservlets.HTMLPage

HTMLPage is the servlet that most developers wanting to generate HTML will want to subclass. The WSGIServlet._lifecycle() method is implemented simply:

try:
    self.prep()
    if not self.write_html():
        raise HTTPNoContent
finally:
    self.wrapup()

In other words, for each request, first prep() is called, followed by write_html() and, finally, wrapup().

prep is the place to do any preprocessing of the incoming request that might influence how the page is written (e.g., has the user logged in?). write_html is where content is written. wrapup is a place for post-processing (e.g., logging the success or failure of the request).

Many methods and instance variables offer features to the developer to allow the customization of their content. Use of subclassing can produce site-wide continuity of content; e.g., you can create a servlet called SitePage (that inherits from HTMLPage) that will provide your site-wide look and feel, then individual servlets can inherit from SitePage.

To see how HTMLPage generates an HTML document, look at write_html() and the methods it calls.

Methods and attributes:

_lifecycle()

HTMLPage’s implementation of the servlet lifecycle. It calls the following methods in the specified order:

  1. prep()
  2. write_html()
  3. wrapup()

prep and write_html are called within a try statement with wrapup called in its finally clause.

prep()

This is the first method called by _lifecycle(). It should be used as a means to prep the servlet for write_html(), e.g., opening data files, acquiring db connections, preprocessing form data, etc. The return value is ignored. The default implementation is a no-op.

write_html()

This method produces a well-formed HTML document and writes it to the client.

It first calls write_doctype() which writes the DOCTYPE element. It then writes “<HTML>”. In turn it calls write_head() and write_body() which write the HEAD and BODY sections, respectively. It finishes by writing “</HTML>”. Returns True.

wrapup()

This is the last method called by _lifecycle(), immediately after calling write_html(). This method should be used to “tidy up” after a request, e.g., flush output data, create a log entry, etc. The return value is ignored by the handler. The default implementation is a no-op.

write_doctype()

Writes the <!DOCTYPE...> tag. By default, the following is used:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">        

This can be controlled by setting the doctype instance variable. If set, it will be written, as-is, to the client. If it is not set, then the dtd instance variable will be checked; it can be one of “strict”, “loose” or “frameset”. The default is “loose” and the resulting DOCTYPE is shown above. If set to “strict” or “frameset” an appropriate DOCTYPE will be generated.

doctype

See write_doctype()

dtd

See write_doctype()

write_head()

This writes the HEAD section of the HTML document to the client. It first writes “<HEAD>” then calls write_head_parts() and, lastly, writes “</HEAD>”.

This method need not be typically overridden. To modify the content of the HEAD section, see write_head_parts().

write_head_parts()

This method calls, in order:

  1. write_title()
  2. write_shortcut_icon()
  3. write_base()
  4. write_meta()
  5. write_css()
  6. write_js()

See the above methods for details about what each writes to the client. All of the above methods can have the content they generate controlled by setting (or unsetting) various instance variables.

While the base method will serve most developer needs, this method is a likely candidate to be overridden. If you want to add to the HEAD you will likely want to call the superclass method and then write additional content in your method, e.g.:

class MyServlet(HTMLPage):
   ...

   def write_head_parts(self):
       HTMLPage.write_head_parts(self)
       # add my own content
       self.writeln(...)
write_title()

Writes the TITLE tag to the client.

It checks for the title instance variable. If it does not exist it uses the name of the servlet for the title. If title is callable it will be called (with no arguments) and its output will be used as the title.

title

The title of the HTML page. If not set, it defaults to the name of the servlet. title can be a callable that accepts no arguments, in which case the title will be the output of the callable.

write_shortcut_icon()

Writes a LINK tag to the client specifying the shortcut icon. See shortcut_icon.

shortcut_icon

If non empty, specifies the href to a shortcut icon and produces a LINK tag in the HEAD: <LINK rel=”shortcut icon” href=”shortcut_icon“/>

write_base()

Write BASE tag to the client in the HEAD. See base.

base

If set, this will specify the BASE tag in the HEAD. If base is a string it will be the value of the href attribute. If base is a tuple, it should have two string elements of the form (href, target) which will be the values of those BASE tag attributes.

write_meta()

Writes META tags to the client in the HEAD. See meta and http_equiv.

meta

A dict, which if non-empty will produce META tags in the HEAD. The keys of the dict will be the values of the name attribute of the element and their values will become the content attribute of the element. For example, the following value of meta:

meta = {"Author" : "John Doe",
        "Date"   : "October 6, 2011"}

will produce the following output in the HEAD:

<META name="Author" content="John Doe">
<META name="Date" content="October 6, 2011">

The values of the dict may be a list or tuple of strings, in which case multiple META tags will be produced for the same name attribute. For example:

meta = {"Author" : ["Tom", "Jerry"]}

will produce the following output in the HEAD:

<META name="Author" content="Tom">
<META name="Author" content="Jerry">
http_equiv

http_equiv, like meta, produces META tags in the HEAD, but instead of the keys being the value of the name attribute, they are the value of the http-equiv attribute. See meta for details.

write_css()

Writes CSS to the client in the HEAD. See css_links and css.

css

A string or list of strings of CSS. If non empty, a <STYLE type=”text/css”>...</STYLE> section will be placed in the HEAD. If css is a list of strings, the elements will be joined (seperated by newlines) and placed in a single STYLE element.

A list of hrefs (strings). For each href, a <LINK type=”text/css” rel=”stylesheet” href=HREF/> be placed in the HEAD.

write_js()

Writes javascript to the client in the HEAD. See js_src and js.

js

A string or list of strings of javascript. If non empty, a <SCRIPT type=”text/javascript”>...</SCRIPT> section will be placed in the HEAD. If js is a list of strings, the elements will be joined (seperated by newlines) and placed in a single SCRIPT element.

js_src

A list of hrefs (strings). For each href, a <SCRIPT type=”text/javascript” src=HREF></SCRIPT> will be placed in the HEAD.

write_body()

Writes the BODY section to the client.

This method writes “<BODY...>”, taking into account the body_attrs instance variable. It then calls write_body_parts() and finally writes “</BODY>”.

This method need not be typically overridden. To modify the content of the BODY section, see write_body_parts().

body_attrs

Attributes for BODY tag. Default is empty dict. If non empty, the keys will become attribute names for the BODY tag and the values will be the attribute values.

write_body_parts()

Write the content of the BODY section.

The base implementation simply calls write_content().

For complex pages that have many sections (navigation sidebar, title menu, etc.) this method will typically be overridden, e.g., write the layout of the site look-and-feel and where page-specific content should appear, call write_content.

See the tutorial that comes with the distribution for examples of how overriding this method can be used to generate site-wide look-and-feel.

write_content()

This method must be overridden to produce content on the page with calls to WSGIServlet.writeln().

Previous topic

wsgiservlets.WSGIServlet

Next topic

wsgiservlets.Dispatcher

This Page