Note that there are many unused imports in this script; that's intentional, because examples in the following sections are modifications of this script, and i want to skip the common part in those examples. (Of course, you wouldn't copy reusable code from script to script in the real life!)

We start with simple wrapper component, itself doing most of the request processing. peak.web.Location seems to be a good base for such wrapper: request path traversal and security rules will be processed by PEAK, so we can focus on our application.

When HTTP request path ends at our location, PEAK looks for default request handler which name is given in config setting peak.web.defaultMethod and usually is index_html (i.e., for example http://my.host/ works like an alias for http://my.host/index_html). At the same location, we could have other handlers with other names too; they would be accessible by URLs with handler name put instead of index_html.

We must have our handler accessible by web users. We do this by declaring that access to index_html is controlled by security rule Anybody, i.e. this method is accessible for everyone.

We use fixed address and port for our HTTP server. Contents can be viewed at http://localhost:8917/.

In real applications, templates usually come from disk files; in this example, we put template text into the program body to simplify example setup.

Insertion of dynamic content is controlled by "magic" element attributes in namespaces this, content and with. this means we want to do something with the element holding the attribute, content specifies actions on the contents of the element, and with provides parameters for the element rendering.

Namespaces this and content out-of-the box allow following attribute names:

is - asserts that contents are known under the name set by attribute value. E.g. <lithis:is="listItem"content:replace="title"/> sets listItem template for list renderer (see below).

replace - contents (whole element for this or element contents for contents) is replaced by result of path traversal; attribute value is path.

xml - contents are replaced by result of path traversal without escaping XML markup.

list - render a sequence of values. This DOMlet accepts parameters listItem, header, emptyList and footer. listitem is rendered for each element of the list, emptyList is rendered when there are no elements in the list, and header and footer are inserted at start and end of the list, respectively.

expects - an assertion as to the type or protocol of the thing currently being handled. Attribute value is protocol import specifier; DOMlet value will be adapted to that protocol at the time of rendering.

Names of attributes in namespace with are names of parameters for operating DOMlet. In the above example, content-type is a parameter of HTML page renderer.

You may develop custom content handlers and register them for use in this: and content: attributes of template elements. In the following example, we display the tree image when our slide refers to the larch:

Real applications are often much more complex than just a single web page, like one shown in above examples. Here come sitemaps, allowing you to build a site out of collection of components, templates and resources.

Our setup in this example is also more complex. First, we make python package directory for web resources (templates and static files):

There is near to zero web code in this script! If our sitemap knew where to take the component it publishes (i.e. if we used valid import specifications in containerobject and contenttype), we wouldn't need our launch script at all. To do so we first need to change our sitemap.xml:

Notice the import of the module slides, wich is our original slides.py. Next we need to start the server using the sitemap.xml, we use the command serve to do
it. There is a special flag -p<PORT> to specify the port we want our webserver to listen to. Finally we run peak as the following:

$ peak serve -p 8917 ref:sitemap@file:sitemap.xml

If the sitemap was located inside a Python package directory we could specify it using a pkgfile: URL in place of a file: URL. For example, if the sitemap file was in the package directory for a package named some.package, we could launch the web browser displaying our application:

$ peak launch ref:sitemap@pkgfile:some.package/sitemap.xml

(The peakserve command just starts a web server. The peaklaunch command is similar, but also launches a web browser to display the root of the application -- a convenient development tool.)