next up previous Python Library Reference contents modules index

Wpre - Advanced Features

The following features allow for more flexibility in creating dynamic data documnets. For each feature an example is provided to help guide you harness the feature's power.

The Wpre system allows you to embed Python expressions directly into the raw string or mark-up file.

Inclusion Feature

When defining a document to be preprocessed, you can make use of basic inclusion. The semantics of include are very precisely defined (unlike in C). A file may be included once as a result of a particular include. Thus if an attempt is made to perform a circular include, the inclusion (recursion) is cut off. You can include the same file multiple times, provided it is not done recursively. We show how to use file inclusion in a file called test2.html , which is shown below.

<html>
<title>`docTitle</title>
<body>

>heading.html

>abc.html

>footer.html

</body>
</html>

The contents of heading.html are:
<h1>Welcome to `company's home page</h1>
Likewise, the contents of abc.html are:
<p> Welcome to Wpre, which is really easy to use. We are particularly upset with ourselves, because this tool only took about a hundred lines of Python code to be implemented. Thus we barely are making use of the memory available on our computer: a sign of poor utilization of resources. Please forgive us Father for we have sinned. </p>
There could be other files, such as footer.html:
<h1>Thank you for visiting `company. We love you!</h1>
<address>`email</address>
Let us write the code to preprocess this file:
 
# import the WpreFile object
from WpreFile import WpreFile
 

def testInclusion():

# define all the variables used
docTitle = "Happy Days"
company = "Tools of Computing"
email = "gkt@cs.depaul.edu"

# call the HTML file that contains the inclusion tags
wpf = WpreFile('test2.html', ["."], vars())

def Main():
testInclusion()


Main()
 

Not surprisingly, it looks a lot like the old code. We include "." in the search path, since all of the files are contained in a common directory. One great feature of Wpre is that the HTML need not be located in the same directory as your CGI scripts. Thus you can keep things organized.

The output is the following:

<title>Happy Days</title>

<h1>Welcome to Tools of Computing's Home Page</h1>

<p> Welcome to Wpre, which is really easy to use. We are particularly upset with ourselves, because this tool only took about a hundred lines of Python code to be implemented. Thus we barely are making use of the memory available on our computer: a sign of poor utilization of resources. Please forgive us Father for we
have sinned. </p>

There could be more.

<h1>Thank you for visiting Tools of Computing. We love you!</h1>

<address>gkt@cs.depaul.edu</address>


File Rules

The WpreFile object provides a means to define variables that call specific functions during the time of processing,  This is useful when you would like to calculate or retrieve the value of a variable as opposed to defining it before calling the process method.  In a sense, this feature allows you to embed functions within HTML documents.

The following example uses the file rule feature of Wpre to obtain the current date and time and display it inside an HTML page. The HTML document, test3.html, is as follows:

<html>
<body>
<h1> The current date and time are: `date_and_time </h1>
</body>
</html>
Now we write the script:
 
# import the time module that is used to get the local time
import time

# import the WpreFile object
from WpreFile import WpreFile
 

def getDate( wpre, env ):

# assign to the variable the current date and time
date_and_time = time.asctime( time.gmtime( time.time() ) )

# replace the date_and_time variable in the HTML doc with the new value
print wpre.evaluate( vars() )


def testAvg():

# create a WpreFile object
wpf = WpreFile( 'test3.html', ["."], vars() )

# assign the getDate function to the tag date_and_time
wpf.addFileRule( 'test3.html', 'date_and_time', getDate )

# process the internal representation and print to standard output
wpf.process()

def Main():
testAvg()


Main()

As before, we first create an HTML document that contains the variable which is to be dynamically defined. We then assign a function, getDate, to the variable. This indicates to the WpreFile object that the value of the date_and_time variable is to be obtained by calling the getDate function. When we call the process function, WpreFile will call the getDate function to find the value of date_and_time and then print the output to the standard output stream. The output of the script is as follows:
<html>
<body>
<h1> The current date and time are: Fri Feb 11 01:46:54 2000 </h1>
</body>
</html>
The assigned function must follow two constraints in order for the WpreFile object to properly call the function. First, the function must take two parameters, wpre and env. The wpre parameter is a reference to the WpreFile object that calls the method. The env parameter is a reference to an environment dictionary that contains the name and value of all the defined variables and functions (see the Python reference document for more detail). Second, the function should define the value of the declared variable. Although the value of the variable may be assigned outside the function, the purpose of the file rule is to define the value upon calling the function. That's it!


next up previous Python Library Reference contents modules index

Send comments on this document to pshafae@leibniz.jhpc.cs.depaul.edu

Copyright(C) Java and High-Performance Computing Laboratory (JHPC) at DePaul University Chicago
Documentation template borrowed from python.org