<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>Olifante's Lair</title>
    
    
    <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/" />
    <id>tag:typepad.com,2003:weblog-16557</id>
    <updated>2010-04-24T21:58:56+01:00</updated>
    <subtitle>Bits and snacks for hungry minds</subtitle>
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/olifanteslair" /><feedburner:info uri="olifanteslair" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://hubbub.api.typepad.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry>
        <title>Minimal Django</title>
        <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/2010/04/minimal-django.html" />
        <link rel="replies" type="text/html" href="http://olifante.blogs.com/covil/2010/04/minimal-django.html" thr:count="6" thr:updated="2010-04-30T05:02:44+01:00" />
        <id>tag:typepad.com,2003:post-6a00d834528cff69e20133ecec3ad8970b</id>
        <published>2010-04-24T21:58:56+01:00</published>
        <updated>2010-04-24T22:31:42+01:00</updated>
        <summary>Flask is a new microframework for web development. Created by Armin Ronacher, it grew from an April Fools’ prank into an elegant, usable tool in a very short time, mostly thanks to its clever leveraging of Armin’s two other babies,...</summary>
        <author>
            <name>olifante</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Python" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Web/Tech" />
        
        <category scheme="http://sixapart.com/ns/types#tag" term="django python webdev flask microframework minimalism beginners" />
        
<content type="xhtml" xml:lang="en-US" xml:base="http://olifante.blogs.com/covil/">
<div xmlns="http://www.w3.org/1999/xhtml"><p><a href="http://flask.pocoo.org/docs/quickstart/">Flask</a> is a new microframework for web development. Created by Armin Ronacher, it grew from an April Fools’ prank into an elegant, usable tool in a very short time, mostly thanks to its clever leveraging of Armin’s two other babies, the <a href="http://werkzeug.pocoo.org/documentation/">Werkzeug</a> webserver and the <a href="http://jinja.pocoo.org/2/documentation/">Jinja2</a> templating language.</p>

<p>I work daily with <a href="http://www.djangoproject.com/">Django</a> and love it, but after reading about Flask I got a small case of microframework envy. Why can’t Django development scale down as well as it scales up, and let a beginner have fun with single-file web development without becoming swamped in the full complexity of modern web development? It turns out Django <em>can</em> easily scale down.</p>

<h2 id="single_file_django">Single-file Django</h2>

<pre><code>import os
from django.conf.urls.defaults import patterns
from django.http import HttpResponse
filepath, extension = os.path.splitext(__file__)
ROOT_URLCONF = os.path.basename(filepath)

def yoohoo(request):
    return HttpResponse('Yoohoo!')

urlpatterns = patterns('', (r'^hello/$', yoohoo))
</code></pre>

<p>If you’re not familiar with Django, this code simply defines a 2-line function (<code>yoohoo</code>) that accepts an HTTP Request and returns a webpage to the browser. In Django this is called a view. After we create the view, we have to associate it with a URL. The last line of code simply means <em>“when someone asks for the page <code>http://your.server.com/hello/</code>, run the <code>yoohoo</code> view and display the result”</em>.</p>

<p>To try this code out in Mac OSX or Linux, all you need to do is <a href="http://docs.djangoproject.com/en/dev/intro/install/">install Django</a>, copy the above code into <code>/some/path/to/somefile.py</code> and type the following incantation into a terminal: <code>PYTHONPATH=/some/path/to DJANGO_SETTINGS_MODULE=somefile django-admin.py runserver</code></p>

<p>This will run a local webserver in your machine, which you can access by opening <code>http://localhost:8000/yoohoo/</code> in your browser. Not too bad for a single file with 10 lines of code.</p>

<h2 id="look_ma_no_project">Look Ma, no project!</h2>

<p>If you’re familiar with Django, you might have noticed what’s missing: we didn’t have to run the usual <code>django-admin.py startproject someproject</code> to create the initial Django project skeleton. This means there is no <code>manage.py</code> file, no <code>settings.py</code>, no <code>urls.py</code>, no <code>views.py</code>, and no Django app in sight. Projects are really a superfluous convenience, as James Bennett argues in <a href="http://apress.com/book/view/1430219386">Practical Django Projects</a>. The file <code>manage.py</code> is simply a shallow wrapper around <code>django-admin.py</code> that adds its directory to <code>PYTHONPATH</code> and points the <code>DJANGO_SETTINGS_MODULE</code> to the <code>settings.py</code> in its directory. By explicitly passing these two parameters to <code>django-admin.py</code> as we did in the above incantation, we eliminate the need for <code>manage.py</code>.</p>

<p>Actually <code>settings.py</code> hasn’t disappeared, we’re just calling it <code>somefile.py</code> or whatever you prefer. The thing is, it probably doesn’t look like a <code>settings.py</code> to you if you’re used to Django. What happened?</p>

<p>It turns out that Django has sensible defaults for much of the stuff that usually goes into <code>settings.py</code>. The absolute minimal <code>settings.py</code> only has to define the <code>ROOT_URLCONF</code> variable and point it to the <code>urls.py</code> module that usually contains the URLs for you Django project. In our single-file Django example, we’re actually pointing the <code>ROOT_URLCONF</code> to the <code>somefile.py</code> file itself.</p>

<p>As for the <code>urls.py</code> file, it hasn’t so much disappeared as merged with <code>settings.py</code> into <code>somefile.py</code>. The same goes for what would usually be the <code>views.py</code> file. We have a single view merged into the <code>somefile.py</code> file. The only restriction is that we must make sure our views are defined before we refer to them in our <code>urlpatterns</code>.</p>

<h2 id="look_ma_no_django_adminpy">Look Ma, no django-admin.py!</h2>

<p>The problem is, typing <code>PYTHONPATH=/some/path/to DJANGO_SETTINGS_MODULE=somefile django-admin.py runserver</code> seems too much of a mouthful to be typing all the time. It almost makes you want to create a project just so you can type <code>python manage.py runserver</code>. Repent, O ye of shallow faith! Copy this code into a file named <code>django</code> and give it execution permissions with <code>chmod +x django</code>:</p>

<pre><code>#! /usr/bin/env sh
USAGE="Usage: `basename $0` path/to/module.py django_command [arg1 arg2 ...]"
if [ "$#" == "0" ]; then
    echo "$USAGE"
    exit 1
fi
module=`basename $1 .py`
dir=`dirname $1`
if [ -n "${PYTHONPATH}" ]; then
    NEWPATH=$PYTHONPATH:$dir
else
    NEWPATH=$dir
fi
shift
echo PYTHONPATH=$NEWPATH DJANGO_SETTINGS_MODULE=$module django-admin.py $@
PYTHONPATH=$NEWPATH DJANGO_SETTINGS_MODULE=$module django-admin.py $@
</code></pre>

<p>Using this, you can now simply type <code>./django ./somefile.py runserver</code> and run your projectless Django code as easily as within a project.</p>

<h2 id="django_templates">Django Templates</h2>

<p>As so often happens with this kind of minimalistic example of microframework usage, our 12-line example looks enticingly simple but is not very realistic. In real life, the <code>HttpResponse</code> returned by the <code>yoohoo</code> view expects to be fed not a simple string such as <code>"Yoohoo!"</code> but a long and complex string containing your HTML code. Now, we could build that string incrementally inside the view function as people did in the good old ASP times, but by now we all should know that this leads to very brittle code and should never be taught to beginners, lest they stray from the righteous path. Instead of embedding HTML into our Python code, we’re going to embed a simplified kind of code into our HTML, using <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/">Django Templates</a>. Fortunately, adding templates to our minimal django example is easy:</p>

<pre><code>import os
from django.conf.urls.defaults import patterns
from django.http import HttpResponse
filepath, extension = os.path.splitext(__file__)
ROOT_URLCONF = os.path.basename(filepath)
DEBUG=TEMPLATE_DEBUG=True
TEMPLATE_DIRS = (os.path.dirname(filepath),)
from django.views.generic.simple import direct_to_template

def yoohoo(request):
    return HttpResponse('Yoohoo!')

urlpatterns = patterns('',
    (r'^hello/$', yoohoo),
    (r'^howdy/$', direct_to_template, dict(
        template='howdy.html',
        extra_context=dict(
            name='Stranger',
        ),
    )),
)
</code></pre>

<p>In this extended example, we’ve added the URL <code>your.server.com/howdy/</code> to our <code>urlpatterns</code> variable, but instead of pointing it to a previously defined view such as <code>yoohoo</code>, we’re pointing it to a built-in Django view called <code>direct_to_template</code>. This view accepts a few additional parameters such as the template file used to render the HTML page and a dictionary of variables that should be available inside the template, in our case <code>name</code>.</p>

<p>We’ve also activated debugging, which makes Django display more user-friendly messages in the browser when an error occurrs.</p>

<p>The template could be something as simple as this or as complex as your wildest Dreamweaver fantasies:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;
            Hi, {{ name }}!
        &lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;
            Hi, {{ name }}!
        &lt;/h1&gt;
        &lt;p&gt;
            My time is &lt;strong&gt;{% now "H:i:s" %}&lt;/strong&gt;
        &lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>This now looks less like a toy and more like something you could actually use to build something useful. We could add decorators à la Bottle or Flask to decorate views with their URLs instead of writing them separately at the bottom, and we could define a nicer syntax to point URLs to templates, but the basics are there and can do much of what Flask does. The difference is, the whole remaining paraphernalia of Django is hidden behind the curtains, ready for you to use it if or when you decide that you need to use an ORM to talk to a database, use Form helpers, build an administrative back-end, internationalize your webapp, etc.</p>
</div>
</content>



    </entry>
    <entry>
        <title>Gaza Gazebo</title>
        <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/2009/01/gaza-gazebo.html" />
        <link rel="replies" type="text/html" href="http://olifante.blogs.com/covil/2009/01/gaza-gazebo.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-61902532</id>
        <published>2009-01-26T04:00:08+00:00</published>
        <updated>2011-07-08T01:06:23+01:00</updated>
        <summary>Uploaded a new (very short) piano "Gaza Gazebo". This piece is dedicated to the victims of the 23 day assault on Gaza. (photo "Gaza Funeral Procession Sylvester Park Gazebo" © Robert F W Whitlock, used with permission.) Gaza Gazebo by...</summary>
        <author>
            <name>olifante</name>
        </author>
        
        
<content type="html" xml:lang="en-US" xml:base="http://olifante.blogs.com/covil/">
&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;p&gt;
&lt;a href="http://www.flickr.com/photos/rwhitlock/3207753926/" title="Gaza Funeral Procession Sylvester Park Gazebo by ˇBerd, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3125/3207753926_0fd27ddac7.jpg" width="500" height="185" alt="Gaza Funeral Procession Sylvester Park Gazebo"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Uploaded a new (very short) piano "Gaza Gazebo". This piece is dedicated to the victims of the 23 day assault on Gaza.
&lt;/p&gt;
&lt;p&gt;
(photo "Gaza Funeral Procession Sylvester Park Gazebo" © Robert F W Whitlock, used with permission.)
&lt;/p&gt;
&lt;p&gt;
&lt;object height="81" width="100%"&gt; &lt;param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18511258"&gt;&lt;/param&gt; &lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt; &lt;embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18511258" type="application/x-shockwave-flash" width="100%"&gt;&lt;/embed&gt; &lt;/object&gt;  &lt;span&gt;&lt;a href="http://soundcloud.com/olifante/gaza-gazebo"&gt;Gaza Gazebo&lt;/a&gt; by &lt;a href="http://soundcloud.com/olifante"&gt;olifante&lt;/a&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>Teatime at the Jockey Club</title>
        <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/2009/01/teatime-at-the-jockey-club.html" />
        <link rel="replies" type="text/html" href="http://olifante.blogs.com/covil/2009/01/teatime-at-the-jockey-club.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-61562810</id>
        <published>2009-01-19T01:43:09+00:00</published>
        <updated>2011-07-08T00:57:42+01:00</updated>
        <summary>Uploaded a freshly composed piano piece. Teatime at the Jockey Club by olifante</summary>
        <author>
            <name>olifante</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Music" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://olifante.blogs.com/covil/">
&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.flickr.com/photos/armadilo60/3930041707/" title="The Jockey by armadilo60, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2623/3930041707_87c0b039af.jpg" width="500" height="451" alt="The Jockey"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Uploaded a freshly composed piano piece.
&lt;/p&gt;
&lt;p&gt;
&lt;object height="81" width="100%"&gt; &lt;param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18510086"&gt;&lt;/param&gt; &lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt; &lt;embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18510086" type="application/x-shockwave-flash" width="100%"&gt;&lt;/embed&gt; &lt;/object&gt;  &lt;span&gt;&lt;a href="http://soundcloud.com/olifante/teatime-at-the-jockey-club"&gt;Teatime at the Jockey Club&lt;/a&gt; by &lt;a href="http://soundcloud.com/olifante"&gt;olifante&lt;/a&gt;&lt;/span&gt; 
&lt;/p&gt;&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>Support Wikipedia</title>
        <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/2008/12/support-wikipedia.html" />
        <link rel="replies" type="text/html" href="http://olifante.blogs.com/covil/2008/12/support-wikipedia.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-60408942</id>
        <published>2008-12-24T17:34:52+00:00</published>
        <updated>2008-12-24T17:34:52+00:00</updated>
        <summary>Donated $30 to Wikipedia, a pitiful sum compared to how much I get out of it. "Imagine a world in which every single person on the planet is given free access to the sum of all human knowledge." — Jimmy...</summary>
        <author>
            <name>olifante</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Web/Tech" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://olifante.blogs.com/covil/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Donated $30 to Wikipedia, a pitiful sum compared to how much I get out of it.</p>
<p>
<a href="http://wikimediafoundation.org/wiki/Donate/en"><img border="0" alt="Wikipedia Affiliate Button" src="http://upload.wikimedia.org/wikipedia/foundation/3/36/2008_fundraiser_square_button-en.png" /></a>
</p>
<p>"Imagine a world in which every single person on the planet is given free access to the sum of all human knowledge."<br />— Jimmy Wales, Founder of Wikipedia</p>


</div>
</content>



    </entry>
    <entry>
        <title>Fidec</title>
        <link rel="alternate" type="text/html" href="http://olifante.blogs.com/covil/2008/09/fidec.html" />
        <link rel="replies" type="text/html" href="http://olifante.blogs.com/covil/2008/09/fidec.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-56051728</id>
        <published>2008-09-24T01:03:55+01:00</published>
        <updated>2011-07-08T00:42:02+01:00</updated>
        <summary>Fidec, a piano piece by Olifante</summary>
        <author>
            <name>olifante</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Music" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://olifante.blogs.com/covil/">
&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;p&gt;
&lt;a href="http://www.flickr.com/photos/olifante/2883776450/" title="Fidec by Olifante, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3241/2883776450_52a44ed0c8.jpg" width="400" height="400" alt="Fidec"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
Here's a new piano piece, dedicated to no one in particular.
&lt;/p&gt;

&lt;p&gt;
&lt;object height="81" width="100%"&gt; &lt;param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18509432"&gt;&lt;/param&gt; &lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt; &lt;embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F18509432" type="application/x-shockwave-flash" width="100%"&gt;&lt;/embed&gt; &lt;/object&gt;  &lt;span&gt;&lt;a href="http://soundcloud.com/olifante/fidec"&gt;Fidec&lt;/a&gt; by &lt;a href="http://soundcloud.com/olifante"&gt;olifante&lt;/a&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;/div&gt;
</content>



    </entry>
 
</feed><!-- ph=1 -->

