Fandom

Stanford University Wiki

Pylons: A Flexible Python Framework

229pages on
this wiki
Add New Page
Talk0 Share

OverviewEdit

Pylons is a lightweight Python-based web framework for rapidly building and deploying web applications. It is based on the popular Model-View-Controller paradigm established by other frameworks such as Rails and Django. While many users will find Pylons' development paradigm similar to that of other web frameworks, Pylons' developers benefit greatly from the ability to inter-exchange their favorite Model, Template, URL Dispatching, or even Javascript libraries with Pylon's default classes. Pylons not only makes this easy to implement but also highly encourages developers to work with whatever components their most familiar with.

Framework StructureEdit

Al Pylons applications are encapsulated in easily distributable Pylon project packages, called eggs. Each egg includes the following breakdown:

- helloworld
    - README.txt
    - data
    - docs
    - development.ini
    - helloworld
    - helloworld.egg-info
        - Various files including paste_deploy_config.ini_tmpl
    - setup.cfg
    - setup.py
    - test.ini

and can be easily distributed to others by registering with the third-party python CheeseShop.

Paste and WSGIEdit

The primary difference between Pylons and other frameworks is its dependence and internal structuring based on the popular WSGI (Web Server Gateway Interface) middleware: Paste. Because Paste is built on the emerging python WSGI server interface, ideally, all Pylon applications should be able to interact with each other with minimal need for documentation between two development teams.

Using Paste, Pylons can interact with URL requests in more detailed and powerful ways because actual Python code is facilitating the server-client interaction. This deep integration with the emerging WSGI gives Pylons the ability to process more detailed HTTP requests from the client's application, allowing developers to pass higher level context information within the dispatched URL, such as callback functions, extra information, arbitrary Python objects and other developer-defined elements that get evaluated at the actual HTTP interface level.

Models and ORM'sEdit

As with any database driven application framework that attempts to abstract the application data from the document presentation layer, Pylons attempts to track all necessary data objects in one pre-defined model component. The central models file allows developers to architect the data and objects that will be manipulated throughout the application from one, easily accessible location. In Pylons, this model file will vary depending on the particular ORM you have chosen to use (SQLAlchemy, SQLObject, or simply DB-API); however, current versions of Pylons default to SQLAlchemy which allows for 3 levels of SQL database interaction:

  1. The Object-Relational Mapper (ORM) lets you interact with the database using your own object classes specific to the domain of the application as opposed to writing direct SQL code
  2. The SQL expression language allows more experience database engineers to write code directly modifying the database in its native dialect
  3. Low-level executive methods accept literal SQL strings for modifying specific table or column entries and types

ControllersEdit

About the only component of Pylons that can't be substituted by another user defined library or component is the controller definitions. As expected under typical MVC architecture, controllers are called upon by specific URL's to process input data, format results, and render the output to pre-defined templates for display. The controllers are called based upon a pre-set routing method defined in the Pylon's file: helloworld/config/routing.py (where "helloworld" is the home application directory. Here's some sample controller code:

import logging

from helloworld.lib.base import *

log = logging.getLogger(__name__)

class HelloController(BaseController):

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

Templates and Presenting InformationEdit

Perhaps one of the strongest selling points for Pylons over other Python frameworks is the ability to import your own preferred Template markup language. This allows a web designer or developer to build the visible components of an application not only independent of the application logic and data objects (thanks to the abstraction of the web framework paradigm) but also using the markup language most comfortable to the designer. Pylons has support for all kinds of templating languages such as Mako, Genshi, Jinja, Kid, Cheetah all powered by the templating API Buffet. By default, Pylons uses the Mako template language, which looks like this:

<%inherit file="base.html"/>
<%
    rows = [[v for v in range(0,10)] for row in range(0,10)]
%>
<table>
    % for row in rows:
        ${makerow(row)}
    % endfor
</table>
   
<%def name="makerow(row)">
    <tr>
    % for name in row:
        <td>${name}</td>\
    % endfor
    </tr>
</%def>

but other languages such as Genshi (which probably feels more comfortable to traditional HTML coders) can be used as well:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/">
  <head>
    <title>Geddit: News</title>
  </head>
  <body class="index">
    <div id="header">
      <h1>News</h1>
    </div>

    <ol py:if="links">
      <li py:for="link in reversed(links)">
        <a href="${link.url}">${link.title}</a>
        posted by ${link.username} at ${link.time.strftime('%x %X')}
      </li>
    </ol>

    <p><a class="action" href="/submit/">Submit new link</a></p>

    <div id="footer">
      <hr />
      <p class="legalese">© 2007 Edgewall Software</p>
    </div>
  </body>
</html>

WebHelpers and AJAXEdit

Pylons borrows directly from Ruby on Rails for inspiration regarding some of its built in AJAX tools, even going as far as directly porting over some of the most common Rails functions such as: start_form, number_to_phone, and link_to_function that has provided some of the more useful AJAX interactions in the most common Rails applications. Pylons WebHelper libraries also build upon the successful JavaScript libraries Prototype and Scriptaculous.

UniquenessEdit

Pylons, as a general Python web framework, competes for attention from developers amongst a fairly crowded market of other contenders, including Django and TurboGears. That being said, however, Pylons has some very interesting core "Do-It-Yourself" components and philosophies that can make it quite appealing to developers looking to integrate a framework around existing applications or even for porting applications built in one framework / language to the otehr.

BYOL: Bring Your Own LibrariesEdit

While Pylons has default library packages for templating (Mako), ORM modeling (SQLAlchemy), and request dispatching (Routes), its creators intentionally built the framework such that it is easy to plug-in your own libraries, whether they are other popular alternatives or even homegrown or internal packages. All in all, Pylons' creators intended developers to pick and choose what parts of their framework they'd like to use that best met the needs of their user's projects.

WSGI InteractionEdit

While still a fairly new initiative, the Python Web Sever Gateway Interface project intends to create more dynamic methods for server and client browsers to interact while also creating a standard interface language for which all WSGI python applications can communicate by. While the benefits of more rich client-side input data provides more interactive applications, the WSGI standard hopes to provide a way for ALL python web applications to easily communicate and leverage each-other's resources. Ideally, Pylons would allow for powerful future collaboration efforts with other teams, companies, and application domains.

Why Use Pylons?Edit

Overall, Pylons presents a highly adaptable alternative to more "All-in-One" Python web frameworks like Django or TurboGears. At its core, Pylons intends to be a simple glue that can be applied to various existing or future application components that would otherwise find it difficult to interact. The best use cases for Pylons are perhaps:

  1. Porting existing applications to a new language or framework during a product development shift, server architecture upgrade, or new hires.
  2. Fleshing out a more light-weight prototype that was used to test a proof-of-concept but lacks the polish of a background framework to piece some of the more meatier future components together
  3. Highly complicated model and database architectures that require low-level access to the database which can be facilitated by powerful ORM frameworks like SQLAlchemy or can even be custom built and plugged directly into Pylons

Relevant LinksEdit

  1. Pylons HQ - Development Headquarters
  2. Python Paste - WSGI Interface Framework
  3. SQLAlchemy Development Center
  4. WSGI Project Information

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.