Rate this page del.icio.us  Digg slashdot StumbleUpon

Introducing Pylons: A hacker’s web framework

by

Python has a good reputation for tasks like systems programming, network programming, and scripting, but Python for the web is becoming red hot. Part of this has to do with the very popular web framework Django, that was developed at a newspaper to help quickly create Content Management Sites. . Another reason is that Google App Engine–Google’s Cloud Computing offering for developers–only exposes a Python API.

If you are new to Python Web Development, then I’d recommend Django, as it is ideal for building CMS-type applications, social networking websites, and blogs. On the other hand, If you want a hacker’s framework, you might want to give Pylons a look.

Please note: By hacker, I am referring to the kind of hacker Eric Raymond refers to when he writes, “Becoming a hacker will take intelligence, practice, dedication, and hard work. Therefore, you have to learn to distrust attitude and respect competence of every kind. Hackers won’t let posers waste their time, but they worship competence — especially competence at hacking, but competence at anything is valued.”

Ok, so what problem does a hacker’s framework solve that a framework like Django doesn’t? According to some of the Pylons developers, their framework is geared to solve 80/20 problems. Most people—80% of people–want to build blogs, and CMS-type applications. And for that 80%, Django works just great. Of course, the other 20% is where Pylons comes in to play as a “hacker’s framework.”

Philosophically, Pylons is quite different. Pylons abstracts third-party libraries, such as WebOb, Mako, SQLAlchemy, Routes, and Beaker, to make a “hacker’s brew.” These libraries are loosely coupled–not in a internet marketing sense, but in a computer science sense. This means that it is quite easy to swap out the ORM, or templates, or URL routing, and create some alternate development stack. Note, that another hacker’s framework, Werkzeug, also follows a similar philosophy (See the references for more details).

What does a hacker’s framework buy you? Well, it allows you to change your web development paradigm. You no longer need to think in terms of what you can do with the choices defined by a framework. It allows the experienced developer to transcend this potential trap, and think about the actual problem at hand. For example, it may be more productive to start by using SQLAlchemy (the object-relational mapper) by itself.

Once the data model is working as expected, then it could turn into a command line tool, then potentially a WXPython application instead of a web application. Hacker’s frameworks let the developer decide what is best for them at any given situation. Additionally, by focusing on loosely coupling the “best of breed” components, it allows the user of a “hacker” framework, to use literally, the best component for the job. This extra power can come in handy with more complex problems.

Setup

In this article we dive into building an AJAX blog using Pylons. We cheat by using the bookmark tagging site, Del.icio.us as an admin interface that we don’t have to write. When a user creates content on Delicious, the Google AJAX feed API allows that content to be displayed locally on the web page. This is one of the most efficient type of blog that someone can create, as it reuses existing code, APIs, and services.

Building a Pylons AJAX blog

To get started you can download the whole example and run it, or you can follow the steps below. Note that in version control, each step is a separate Pylons project.

[ Step 1] http://code.google.com/p/pyatl-pylons/source/browse/#svn/trunk/1

1: Download http://www.pylonshq.com/download/0.9.7/go-pylons.py

ngift@noah][H:10471][J:0]# python gopylons.py --no-site-packages pylonsblog
New python executable in pylonsblog/bin/python
Installing setuptools..........................done.
Searching for Pylons
[snip]

Note: You will need to meet all requirements of the package.

2:

cd pylonsblog

3: Active environ

source bin/activate

4: Make project

mkdir -p src
paster create --template=pylons ajaxblog

5: Get Pylons running

cd ajaxblog

paster serve development.ini

6: Make a controller

paster controller blog

cd ajaxblog/controller to verify

go to page:

http://localhost:5000/blog

See Hello World, change it.

"Hello Red Hat Magazine"

[Step 2] http://code.google.com/p/pyatl-pylons/source/browse/#svn/trunk/step2

7: Change front page

Add your own content to public/index.html

8: Add a couple of templates, and hook up to blog controller

Create base.html
Create blog.html

Add this line to blog.html:

Hello from blog.html template: Red Hat Magazine

9: Change controller to render template

Edit controller/blog.py to this:

class BlogController(BaseController):

    def index(self):
        # Return a rendered template
        #   return render('/some/template.mako')
        # or, Return a response
        return render('/blog.html')

[Step 3] http://code.google.com/p/pyatl-pylons/source/browse/#svn/trunk/step3

10: Get Buzzword compliant: Adding AJAX, RSS, Mashup and Google

A. Sign up for AJAX RSS Developer Key:

http://code.google.com/apis/ajaxfeeds/

B. Add javascript Code to base.html
C. Call feed div in blog.html

11: See RSS Feed appear

If you go to localhost:5000/blog you will see my last RSS feeds

[Step 4] http://code.google.com/p/pyatl-pylons/source/browse/#svn/trunk/step4

Bonus Points: Try to add persistent comments using the SQLAlchemy ORM on your own!

Summary

In this article we explored some of the ideas behind Pylons, a hacker’s framework, and how it differs from a philosophy framework, like Django or Ruby on Rails. Note by philosophy framework, I am referring to how a developer must abide by the opinions of the developer of the framework, such as in the case of Django templates where a developer is handcuffed against running code in the template. For example, their idea of perfection might be different then yours. Some people refer to this as being an “opinionated” framework. Each one has its purpose, and place. If you just want to make a Content Management website MS website for your American Literature class project, then maybe a hacker’s framework isn’t suited for you or the project, as it is overkill. On the other hand, if you want maximum flexibility, power, (and, of course, “street cred”) you might give a hacker’s framework a try.

Finally, we got into making an actual Pylons AJAX blog that used Delicious to suck in feeds. This was accomplished by reusing code via the Google AJAX Feed API. Ok, enough talk, get to hacking….

Extra Credit: If you are interested diving into a more complex Pylons project template on your own take a look at this source code url. Jonathan Ellis, a Python hacker known for his work with SQLAlchemy, has donated a do-it-yourself tutorial on using FormAlchemy to create a simple blog in Pylons. I have included a link to his original article on the topic in the reference section.

References

Pylons Book: http://pylonsbook.com/alpha1/toc
Google AJAX Feed API: http://code.google.com/apis/ajaxfeeds/
SQLAlchemy: http://www.sqlalchemy.org/
Using SQLAlchemy: http://www.ibm.com/developerworks/aix/library/au-sqlalchemy/
How To Become a Hacker: http://www.catb.org/~esr/faqs/hacker-howto.html
Google App Engine: http://code.google.com/appengine/
WebOb: http://pythonpaste.org/webob/
Source Code For Example: http://pyatl-pylons.googlecode.com/svn/trunk/
Werkzeug (An alternate hacker’s framework): http://werkzeug.pocoo.org
Loose Coupling Computer Science Definition: http://en.wikipedia.org/wiki/Loose_coupling#Definition
Pylons FormAlchemy How To: http://spyced.blogspot.com/2008/10/formalchemy-10.html
Extra Credit Project: http://code.google.com/p/pyatl-pylons/source/browse/#svn/trunk/bonus_project_form_validation_jonathan_ellis

About the author

Noah Gift is the co-author of “Python For Unix and Linux” by O’Reilly, and “Google App Engine in Action” by Manning. He is an author, speaker, consultant, and community leader, writing for publications such as IBM Developerworks, Red Hat Magazine, O’Reilly, Manning and MacTech. He has a Master’s degree in CIS from Cal State Los Angeles, B.S. in Nutritional Science from Cal Poly San Luis Obispo, is an Apple and LPI certified sysadmin, and has worked at companies such as, Caltech, Disney Feature Animation, Sony Imageworks, Turner Studios, and–most recently–WetaDigital.

6 responses to “Introducing Pylons: A hacker’s web framework”

  1. Eko pranoto - kopra says:

    This post is very great introduction
    using pylons and it position on the
    web arena to anybody, even for
    dummies.
    I wonder, can I use python to create
    web application, like healthcare
    infomation system ?

  2. hacker.com.br » Blog Archive » Pylons: A hacker’s Web Framework says:

    […] Segue link: http://www.redhatmagazine.com/2008/11/05/introducing-pylons-a-hackers-web-framework/ […]

  3. Noah Gift says:

    Eko,

    Take a look at this API, it is a part of the GData API’s that Google Exposes:

    http://code.google.com/apis/health/

    Mixing this into a Pylons project wouldn’t be too tough.

  4. Rajul Patel says:

    ITG Digital Info is a modern organization providing world class innovative solutions to small, mid-size and corporate organizations and businesses. We provide global customers the optimum advantages of outsourcing in terms of cost, quality and quick turnaround time. Our offshore outsourcing services providing customized business propositions offer immense value to customers thereby building enduring customer partnerships. Our core value constitutes employing integrity at all times and being fair to our employees, customers, vendors, media, community and all other stakeholders. http://www.itginfo.com

  5. ldso says:

    …”get buzzword compliant”…
    I’m a grumpy old fuddyduddy. I don’t think real hackers ever get buzzword compliant. Just ask Dennis Ritchie. I think he would see this “mashup” of passing objects around between environments as being equivalent to touching yourself behind the curtains.

    Bah humbug!

  6. Curious says:

    Just curious, what’s happening with RHM? The number of articles have declined noticeably.