<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rexmere.com &#187; development python cherrypy cappuccino</title>
	<atom:link href="http://rexmere.com/tag/development-python-cherrypy-cappuccino/feed/" rel="self" type="application/rss+xml" />
	<link>http://rexmere.com</link>
	<description>Technical Arcana, Software Ephemera and Miscellaneous Bits. Keith R. Fieldhouse proprietor.</description>
	<lastBuildDate>Wed, 18 Mar 2009 00:36:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cappuccino and CherryPy, Part II</title>
		<link>http://rexmere.com/2008/12/21/cappuccino-and-cherrypy-part-ii/</link>
		<comments>http://rexmere.com/2008/12/21/cappuccino-and-cherrypy-part-ii/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 18:32:59 +0000</pubDate>
		<dc:creator>Keith Fieldhouse</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[development python cherrypy cappuccino]]></category>

		<guid isPermaLink="false">http://rexmere.com/?p=80</guid>
		<description><![CDATA[I'm in the process of blogging my experiences using the Cappuccino JavaScript framework and CherryPy, a Python web application project.  The first post can be found here.
Cappuccino is designed to provide an application-like interface inside a web browser.   As a result, when you're building a Cappuccino application, instead of the server providing [...]]]></description>
			<content:encoded><![CDATA[<p><em>I'm in the process of blogging my experiences using the Cappuccino JavaScript framework and CherryPy, a Python web application project.  <a href="http://rexmere.com/2008/12/20/cappuccino-and-cherrypy/">The first post</a> can be found here.</em></p>
<p>Cappuccino is designed to provide an application-like interface inside a web browser.   As a result, when you're building a Cappuccino application, instead of the server providing a sequence of pages to view, the server is providing data (usually using JSON) that the Cappuccino application running in the browser retrieves and displays.   For all practical purposes, using Cappuccino is very like old-fashioned client server programming with the client written in Objective-J and the server, in this case, written in Python.</p>
<p>In this post we'll focus on changing the Cappuccino "Hello World" application so that it retrieves it's message from the server using JSON.  No, it's not a particularly exiting application but it has the virtue of being simple and fundamental:  Once it's possible to act on JSON data from the server, the rest is just details.</p>
<p>First, we'll change the client side Objective-J code.  We need to add the following two lines to the end applicationDidFinishLauching: method of our AppController in AppController.j</p>
<pre class="objc">&nbsp;
    var request = <span style="color: #002200;">&#91;</span>CPURLRequest requestWithURL:<span style="color: #666666;">&quot;getLabel&quot;</span><span style="color: #002200;">&#93;</span>;
    var connection = <span style="color: #002200;">&#91;</span>CPURLConnection connectionWithRequest:request delegate:self<span style="color: #002200;">&#93;</span>;
&nbsp;</pre>
<p>The first line creates a <a href="http://cappuccino.org/learn/documentation/class_c_p_u_r_l_request.html">CPURLRequest</a> for the "getLabel" URL from the server.   The second line actually makes the connection to the server using that request object.  Like it's inspiration, Cocoa, Cappuccino makes heavy use of the delegate pattern.  In this case we pass our AppController object as the delegate for the <a href="http://cappuccino.org/learn/documentation/class_c_p_u_r_l_connection.html">CPURLConnection</a>.  There are two methods that the CPURLConnection may call on the delegate.   We'll add those next:</p>
<pre class="objc">&nbsp;
- <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>connection:<span style="color: #002200;">&#40;</span>CPURLConnection<span style="color: #002200;">&#41;</span> connection didReceiveData:<span style="color: #002200;">&#40;</span>CPString<span style="color: #002200;">&#41;</span>data
<span style="color: #002200;">&#123;</span>
    var result = CPJSObjectCreateWithJSON<span style="color: #002200;">&#40;</span>data<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>label setStringValue:result.label<span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
- <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>connection:<span style="color: #002200;">&#40;</span>CPURLConnection<span style="color: #002200;">&#41;</span>connection didFailWithError:<span style="color: #002200;">&#40;</span>CPString<span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>label setStringValue:@<span style="color: #666666;">&quot;Label Did Fail With Error&quot;</span><span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;</pre>
<p>The method "connection:didReceiveData:" is called with the original connection object (useful to distinguish between multiple pending connections) and the data that the server provided.  The "connection:didFailWithError" is called if the connection failed for some reason.</p>
<p>For now, we'll simply change the label when the connection completes.  If the connection fails, we'll note that.   If the connection does receive data, we'll use that data to change the label.  To do this, we use a CPJSObjectCreateWithJSON(data) object.  Once we've done that, we can retrieve the "label" attribute of the result and display it.</p>
<p>There's one other thing that we need to do on the client side for all of this to work.  In the original AppController.j, the label object is a local variable.  Since other methods of our AppController object need to access that object, we need to make it an instance object.   We do this by adding the following line to the "@implementation AppController" stanza in AppController.j:</p>
<pre lange="objc">
CPTextField label;
</pre>
<p>We also need to remove the "var" qualifier from the first appearance of the label variable in "applicationDidFinishLaunching:"</p>
<p>The server side is even simpler.  We simply need to add a method that will respond to the "getLabel" request that the client will make.  We do this by adding a "getLabel"<br />
method to the root CherryPy object:</p>
<pre class="python">&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> getLabel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        data = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;label&quot;</span>:<span style="color: #483d8b;">&quot;Server Hello&quot;</span> <span style="color: black;">&#125;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> simplejson.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;</pre>
<p>In that method, we'll create the Python dictionary that we'll return.  It has one attribute/key "label" which we'll set to the string "Server Hello".  Then we use the Python simplejson to convert that object to a JSON string which we return.  </p>
<p>We've seen that it's fairly easy to communicate between the Cappuccino client and the CherryPy server.  Next we'll start looking at building a somewhat more elaborate client application based on data returned from the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://rexmere.com/2008/12/21/cappuccino-and-cherrypy-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cappuccino and CherryPy</title>
		<link>http://rexmere.com/2008/12/20/cappuccino-and-cherrypy/</link>
		<comments>http://rexmere.com/2008/12/20/cappuccino-and-cherrypy/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 23:54:10 +0000</pubDate>
		<dc:creator>Keith Fieldhouse</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[development python cherrypy cappuccino]]></category>

		<guid isPermaLink="false">http://rexmere.com/?p=72</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cappuccino.org/">Cappuccino</a> is a JavaScript toolkit for building application like experiences on the web.  It's developed (and used) by the folks who produced <a href="http://280slides.com/">280 Slides</a>, a rather amazing presentation package for the web.</p>
<p>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.  <a href="http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/">This blog post</a> explains some of the motivations they had for doing this.  In any event, I decided that I'd like to give Cappuccino a spin.</p>
<p>My "go to" language for server back ends is <a href="http://www.python.org">Python</a>.   I also like <a href="http://www.cherrypy.org/">CherryPy</a>, 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 <a href="http://www.json.org/">JSON</a> data for the Cappuccino application to present.  CherryPy makes this fairly easy.</p>
<p>To get started, I downloaded the the Cappuccino "<a href="http://cappuccino.org/download/">Starter Package</a>".  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.  </p>
<p>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:</p>
<pre class="ini">&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>global<span style="">&#93;</span></span>
tools.staticdir.<span style="color: #000099;">root </span>= <span style="color: #933;">&quot;/Users/keith/Projects/CherryCappuccino/Application&quot;</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>/<span style="">&#93;</span></span>
tools.staticdir.<span style="color: #000099;">on </span>=<span style="color: #660066;"> True</span>
tools.staticdir.<span style="color: #000099;">dir </span>= <span style="color: #933;">&quot;static&quot;</span>
&nbsp;</pre>
<p>This tells CherryPy to server the contents of my "static" directory statically.  </p>
<p>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:</p>
<pre class="python">&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CherryCappuccino:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">indexText</span> = <span style="color: #008000;">None</span>
&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">indexText</span>:
            indexFileName = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>,<span style="color: #483d8b;">&quot;static&quot;</span>,<span style="color: #483d8b;">&quot;index.html&quot;</span><span style="color: black;">&#41;</span>
            indexFile = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>indexFileName<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">indexText</span> = indexFile.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">indexText</span>
&nbsp;
cherrypy.<span style="color: black;">quickstart</span><span style="color: black;">&#40;</span>CherryCappuccino<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">'/'</span>,<span style="color: #483d8b;">&quot;cherrycappuccino.ini&quot;</span><span style="color: black;">&#41;</span>
&nbsp;</pre>
<p>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.</p>
<p>In a future post, I'll show how to configure the Cappuccino view based on data returned from the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://rexmere.com/2008/12/20/cappuccino-and-cherrypy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
