As described in the overview, calling the App produces a Page, which is converted to a Response. . The Page is combined with some contextual information to create a Response instance, which is returned.
The following are specializations of Page.
HtmlPage. The commonest kind of Page. It is discussed in detail below.
RawFile. The constructor takes a filename. The MIME type of the file is determined from the filename suffix, or it may be explicitly specified by providing keyword argument type. If there is no suffix, it defaults to 'txt'.
Data. Unlike RawFile, this is not data that is associated with
a file. When constructed, it represents an empty byte sequence. One
adds data by calling write(bs)
, where bs is a byte
array.
Text. Like Data, except that one may write strings to it. One may also pass a string to the constructor to set the contents.
Redirect. The constructor takes a URI.
HttpException. The constructor takes a message. Subclasses are: PermissionDenied, PageNotFound, HttpUserError, and HttpSystemError.
A Response instance packages up a Page along with cookie information taken from the Context. It has methods that make it suitable for use within a WSGI application. See Translating responses.
The following members of Page are relevant for producing WSGI output:
The available specializations of Page are as follows.
One creates a web page by instantiating HtmlPage. The HtmlPage constructor has one obligatory argument, parent, which is usually an HtmlDirectory, though we use None in the following example to keep things simple. A commonly-used optional parameter is title:
>>> page = HtmlPage(None, title='Test Page')
One then adds UI elements to the page. Creating an element with the page as parent automatically adds it to the page:
>>> par = P(page)
Strings can be added using the function String:
>>> String(par, 'This is a ') 'This is a ' >>> B(par, 'test') <BasicElement b> >>> String(par, '.') '.'
Printing the page shows the contents that will be sent to the client:
>>> print(page, end='') <html> <head> <title>Test Page</title> <link rel="stylesheet" type="text/css" href="/.lib/default.css" /> </head> <body> <p>This is a <b>test</b>.</p> </body> </html>
There are two lowlevel methods for adding material to an HtmlPage: add, for adding elements, and write(), for writing raw HTML. Usually one does not call these methods directly. Rather, one adds a new element as in the previous example: simply by creating an element and passing it the page, or an element already on the page, as its parent.
Other methods are provided for adding information to the page that is rendered in the head or foot. Several of these methods access script or stylesheet files; all such files reside in the Seal subdirectory data/seal.
k=v
to the body element.
Adding an item to an HtmlPage simply adds it to the page's contents. When rendering the page, the function iterhtml is called on each item in the contents to convert it into strings or bytes. In particular: