PeeJays

by ObjectRealms, LLC last modified Dec 30, 2008 05:52 PM
How to use PJS (Python Javascript Syntax) to create javascript behaviors with python

PJS (Pee-Jays)

Python to JavaScript Bridge

PJS is simple enough to use. By default you are given a page object. Here we import and set this up manually.

>>> from pjs import JavascriptPage
>>> page = JavascriptPage()

All the javascript you wish to produce is generated through the page object. 'page' is a proxy though which you obtain reference to other Python proxy objects.

Using page we can take advantage of many of the features of the Prototype and Scriptaclous Javascript libraries. Lets suppose we are exectuing Javascript relative to a page that has a number of ids defined within it. Suppose 'my-portlet' is one such id. To refer to it from Python we might say

>>> mp = page["my-portlet"]

This will generate javascript into the page object which can later be extracted using str() or print. To verify that commands are working as expected in these test we can look in to the buffer being generated inside the page object.

>>> page.last
'$("my-portlet");'

Using 'mp' we can invoke methods directly on the proxy. This will create the correct Javscript. The funny formatting in the examples below ignores the return value (which would be another proxy) and then shows the last line.or us.

>>> assert mp.show(); page.last
'$("my-portlet").show();'

The prefered form for doing such statements however is to always reference the object through 'page'. This has the effect of beginning a new statement in Javascript and removes ambiguity from the generation and comprehension process.

>>> page['my-portlet'].show('first').up(); page.last
'$("my-portlet").show("first").up();'

For the next test we will reset the page object >>> page = JavascriptPage()

Lets suppose for example that we wanted to return a Tree object populated with elements from Python. In this very simple example we use a simple list as a place holder for your dynamically generated tree elements.

This says create a new Javascript object of class Tree. >>> tree = page.new('Tree')

>>> for i in ['a', 'b', 'c']:
...     dummy = tree.addItem(i, icon="document.gif")

Populate each element

>>> print page
var tree1 = new Tree();
var _v_1 = tree1.addItem("a", {"icon":"document.gif"});
var _v_2 = tree1.addItem("b", {"icon":"document.gif"});
var _v_3 = tree1.addItem("c", {"icon":"document.gif"});

We can see that the page will have the properly generated Javascript in it.

Collections

Manpulating lists and collections is simple as well. To gather a list of objects using a CSS style selector we can say

>>> page = JavascriptPage()
>>> assert page.select('p'); page.last
'$$("p");'

Which would select all the paragraph tags in the page it was being executed for. Supposing we wanted to do something to each of them we might say the following.

>>> page.select("p").each('highlight', delay=1.0)
>>> page.last
'$$("p").each(function(el) {el.highlight( {"delay":1.000000})};);'

Its typical to process a collection with a series of commands in Javascript. To express something like this we can say.

## Inprogress ##>>> assert page.select("#results tr").collect("el.foo = 'bar'") ##>>> page.last ##'$$("#results tr").collect(function(el) {el.foo = 'bar'()};);'

Variables

To refer to an existing Javascript variable in the page you can use attribute styled access.

>>> assert page.my_var.select()
>>> page.last
'var _v_1 = my_var.select();'