Skip to content

Cappuccino and CherryPy

Cappuccino is a JavaScript toolkit for building application like experiences on the web. It's developed (and used) by the folks who produced 280 Slides, a rather amazing presentation package for the web.

I've been doing a fair bit of of Mac development and have come to appreciate the syntax of Objective-C. One of the more interesting aspects of Cappuccino is that it has created a new dialect of JavaScript that they call Objective-J. This blog post explains some of the motivations they had for doing this. In any event, I decided that I'd like to give Cappuccino a spin.

My "go to" language for server back ends is Python. I also like CherryPy, a Python server framework that makes it easy to attach Python code to web requests. Since Cappuccino is designed to make it easy to build an application inside a browser window, the back end mostly needs to be able to serve up JSON data for the Cappuccino application to present. CherryPy makes this fairly easy.

To get started, I downloaded the the Cappuccino "Starter Package". Inside that zip file is a "NewApplication" directory. I copied that directory as a directory called "static" in the my Application directory. If you open the supplied index.html, you are presented with a very basic "Hello World" application written in Cappuccino. In order to to build the application I have in mind though, I want that index.html to come from my CherryPy server so that the Cappuccino application can easily request information.

To do this, I configured my CherryPy application to server the contents of my Application/static directory. This is readily accomplished by creating a CherryPy configuration file cherrycappuccino.ini:

 
[global]
tools.staticdir.root = "/Users/keith/Projects/CherryCappuccino/Application"
 
[/]
tools.staticdir.on = True
tools.staticdir.dir = "static"
 

This tells CherryPy to server the contents of my "static" directory statically.

What remains is the Python code that builds the CherryPy application. At this point it's quite simple. We create one object that's mounted on the root of the CherryPy server. It's index method simply returns the contents of the index.html file in the static directory (there might be other ways to accomplish this but this was expedient. Here's the Python code from cherrycappuccino.py in my Application directory:

 
import cherrypy
 
class CherryCappuccino:
    def __init__(self):
        self.indexText = None
 
    @cherrypy.expose
    def index(self):
        if not self.indexText:
            indexFileName = os.path.join(os.path.dirname(__file__),"static","index.html")
            indexFile = open(indexFileName)
            self.indexText = indexFile.read()
 
        return self.indexText
 
cherrypy.quickstart(CherryCappuccino(),'/',"cherrycappuccino.ini")
 

This reads the cherrycappuccino.ini file to configure the application and starts the server with our single application object. Starting the server with "python cherrycappuccino.py" sets up the server that can be browsed to at http://localhost:8080.

In a future post, I'll show how to configure the Cappuccino view based on data returned from the server.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*