CherryPy Project Download

Why use jsolait with CherryPy

JSolait can make it easier to write web pages. It does this by translating data types back and forth between python on the server and javascript on the client. For example, python dictionaries become javascript 'associative arrays'. Javascript associative arrays become python dictionaries. Python lists and javscript arrays are interchangable. So are Python objects and javascript objects to some degree.

How to use jsolait with CherryPy

Example 1

This uses jsolait1.0. A webpage retreives two objects from the server; the objects are inside of an array.

Here is the file layout:

etc etc etc the rest of the jsolait files. 

Here are the contents of 'index.html':

    <script type="text/javascript" src="./jsolait/init.js"></script>
    <script type="text/javascript" src="./jsolait/lib/urllib.js"></script>
    <script type="text/javascript" src="./jsolait/lib/xml.js"></script>
    <script type="text/javascript" src="./jsolait/lib/xmlrpc.js"></script>

<script language="javascript">

var xmlrpc = importModule("xmlrpc");
var url="http://localhost:8080";
var methods=["testRPC"];
try {
    var service = new xmlrpc.ServiceProxy(url,methods);
    document.write('first obj name: ' + response[0].name + '<br>');
    document.write('first obj number: ' + response[0].number + '<br>');
    document.write('second obj name: ' + response[1].name + '<br>');
    document.write('second obj number: ' + response[1].number + '<br>');
} catch(e) {

Here is

import cherrypy

class TestClass:
    def __init__(self, name='', number=''): = name
        self.number = number

class Root:
    _cp_config = {'tools.xmlrpc.on': True}

    jsolait = tools.staticfile.handler(section='/', file='jsolait')

    def index(self):
        return open('index.html').read() = True
    def testRPC(self):
        test2 = TestClass('hi', '5')
        test3 = TestClass('hi2', '52')
        return [test2, test3] = True


Run Open a browser to 'http://localhost:8080'. You should get this:

first obj name: hi
first obj number: 5
second obj name: hi2
second obj number: 52

Example 2

This is similar to the first example except for one thing. The two objects are contained in a python dictionary instead of an array. The python dictionary gets transformed into a javascript associative array by jsolait.

Here is the modification to index.html:

try {
    var service = new xmlrpc.ServiceProxy(url,methods);
    document.write(' ' + + '<br>');
    document.write('obja.number: ' + response.obja.number + '<br>');
    document.write(' ' + + '<br>');
    document.write('objb.number: ' + response.objb.number + '<br>');
} catch(e) {

Here is the new testRPC method for

    def testRPC(self):
        test2 = TestClass('hi', '5')
        test3 = TestClass('hi2', '52')
        return {'obja': test2, 'objb': test3} = True

If you edit index.html and with these changes, then stop and restart, and then open up your browser and again type http://localhost:8080, you should get the following: hi
obja.number: 5 hi2
objb.number: 52

Older versions

replace this with this

See StaticContent? for additional compatibility info.

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages