This is a work in progress, I will expand this document considerably (and tidy up the grammar) once ScratchPad is released to the world
Using PythonCard with XML RPC
by Simon Willison
My first experiment with PythonCard was to build an application for editing text stored on a web server. The application I created - ScratchPad - will be released as soon as I have built suitable authentication controls in to it. In the meantime here are some thoughs on how PythonCard can be used with XML RPC to create flexible applications for modifying website contents.
Content Management is a huge concept at the moment. From Radio Userland, Blogger and other web logging software up to full on commercial scale systems such as Zope and Vignette, everyone wants to get in on the simple idea of creating systems to manage content. More often than not these systems are used to manage web content (although a true CMS will manage anything you throw at it). The obvious track to take when implementing a web-orientated CMS is to create the administration tools as a web application, with web forms, authentication and server side solutions handling the administrative side of the system.
The problem with this approach is that, as Anil Dash points out so eloquently in this posting, HTML interfaces are miles behind those provided by a proper GUI application. Interaction is limited by the request-response nature of the web, and many simple interface systems such as drag and drop are virtually impossible to implement on the web.
Another buzz word doing the rounds at the moment is the concept of Web Services - applications that can be called by other applications over the web using a common protocol such as SOAP or XML-RPC. It can be hard to break through the hype surrounding these technologies, but the core principle is elegant and very powerful. By combining web services with a simple PythonCard application it is possible to create highly flexible administration tools for managing content on the web!
Out overall system consists of three main components:
- The Content Management System
- A Web Service server tied in to the CMS
- Our application
I chose to implement the CMS and Web Service server in PHP and the application in PythonCard. I used XMLRPC for the interaction between the two - both PHP and Python have excellent XMLRPC modules and I find the protocol easier to use than the more complicated SOAP.
Presuming you have a CMS already, the most important decision to make is to decide what methods will be made available to the application through XMLRPC. For ScratchPad (which only deals with loading and modifying simple text documents) I created the following methods:
- getDocumentlist() - returns a list of available documents
- getDocument(name) - returns the text of the specified document
- saveDocument(name, contents) - saves contents to the document called name
The third method will over-write a document if a currently in-use name is specified, and create a brand new document if the name provided is unique. For anything more complicated than a very basic system like ScratchPad it would make sense to seperate these functions out, however for my purposes it was perfectly adequate.
My first step was to put together the XMLRPC server. I used this PHP library which allows you to create a service simply by defining the functions you wish to make available and then "registering" them with the server class.
Once that was done I tested the server using the Python command line interpreter. By dynamically importing the xmlrpclib module it is possible to quickly try out the different server methods and confirm that they are returning the correct information.
Writing the application proved easy thanks PythonCard's powerful resource editor and elegant structure. I "drew" the application interface, saved it as a resouce file, defined the few methods I needed for the various buttons displayed by the interface and tested the program.