home     documentation     download     Spyce logo

Documentation - Language

Prev: 2.5 - Python Chunks Up: 2 - Language Next: 2.7 - Python Expressions

2.6. Python Class Chunks

Syntax: [[! Python class chunk ]]

Behind the scenes, your Spyce files are compiled into a class called spyceImpl. Your Spyce script runs in a method of this class called spyceProcess. Class chunks allow you to specify code to be placed inside the class, but outside the main method, analogously to the "<%!" token in JSP code. (If you would like to see your Spyce file in compiled Python form, use the following command-line: spyce.py -c myfile.spy.)

This is primarily useful when defining active handlers without using a separate .py file: active handlers are the first thing that the spyceProcess calls, even before any "python chunks." For a handler callback to be visible at this stage, it needs to be defined at the class level. Class chunks to the rescue:

def calculate(self, api, x, y):
    self.result = x * y

<spy:parent title="Active Handler example" />
    <f:text name="x:float" default="2" label="first value" />
    <f:text name="y:float" default="3" label="second value" />

    <f:submit handler="self.calculate" value="Multiply" />

Result: [[= hasattr(self, 'result') and self.result or '(no result yet)' ]]
Run this code

Prev: 2.5 - Python Chunks Up: 2 - Language Next: 2.7 - Python Expressions

Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo