To create python screens we use exactly the same methodology than for
normal java screens. First you need a /screens subdirectory in your
services.AssemblerBrokerService.python.path. This is where your
screen code will live. Layouts goes in /layouts, actions in /actions
and so forth (just remember to register the appropriate assembler
factories).
I'm going to create a sample WebMacro screen. First we need a .py file
where this screen can live. The .py file should have the same name as
your .wm file and it must be all lower case. Inside this file we
create a new class with the same name as the file, but in this the
first letter is uppercase (the rest is always lower case). To override
a method is fairly simple (just create a new method with the same
name).
There is one problem however - Python does not support method
overloading based on type signatures, so usually the simplest for is
used, which is doBuildTemplate( RunData data ) for WebMacroSiteScreen
in stead of doBuildTemplate( RunData data, WebContext context ). You
have two options to overcome this problem. (1) Create a new superclass
that exposes a method under a different name or (2) just call
getContext in your python code. I usually just go for the second
option because it is fairly straight forward anyway.
Now for the code. Let's create a screen called Test. First create a
test.wm file (I assumer everybody knows how to do this). Now create a
test.py and place it in the python-path/screens directory. It should
look something like this:
 |
 |
 |
 |
class Subjectslist(WebMacroSiteScreen):
def doBuildTemplate (self,data):
context = self.getContext(data)
context.put ("me","Leon")
context.put ("text","Python is cool");
|
 |
 |
 |
 |
For more information about the self parameter see the Python docs. You
can call any java code that you would normally be able to use (just
remember to add it to conf.py) including database, services, etc. You
can also freely use any of the Python built-in types and any Python
module that was built on 100% pure Python.