Python Web Frameworks

Senthil Kumaran
- CPython Core Developer
- Engineer at Lucasfilm, Singapore.

Web Frameworks

  • Django

  • Pyramid/ Pylons

  • Web2Py

  • Flask

Pylons

A pylon is a monumental portal to an ancient Egyptian temple.

pylons_ramesesseum.jpg

Pylons Web Framework

  • Is a lightweight web framework.

  • Emphasizes flexiblity

  • Rapid development of web applications

  • Uses standard tools from the Python community.

  • It does not suffer from Not-Invented-Here Syndrome.

  • It uses specific tools and the project has spawned off self-contained tools

Motivation for you

Web Development

  • The first shot in dynamic website designs was CGI.

  • Easy to write, but difficult to maintain.

  • Memory hungry because it loads up the entire interpreter.

  • They were using CGI

handout

Web frameworks such as Django and Ruby on Rails have become extremely popular in recent years because they provide a structure that allows you to quickly create good-looking web sites by defining the way the data is structured. The tools they provide then work on that data either to automatically generate code (scaffold in the case of Ruby on Rails) or to create form interfaces at runtime (as is the case with Django).

Pylons provides sensible low-level APIs and methodologies that allow you to quickly and easily glue together the component parts you choose to use for yourself.

Pylons is a collection of very carefully chosen 3rd party software.

Design Patterns

At their heart, web applications

  • Stored Data (Model)

  • Represent Data in various ways (View)

  • Execute Logic code for manipulation and control interaction. (Controller)

That's MVC for you.

Pylons Environment

  • Get go-pylons.py script and create the virtual environment myenv

  • cd myenv

  • Activate it source bin/activate

  • find | less

handout

There are other ways to setup like compiling the code directly from the mercurial repository. But having a dedicated environment setup is probably good and best during development.

Project Template

  • paster utility to create project template and serve the project.

(myenv)$paster create -t pylons helloworld
  • Choose the components to use.

(myenv)$cd helloworld
(myenv)$paster serve development.ini
  • paster utility to serve the project welcome page.

Contoller

  • Create a contoller in the project to handle request.

  • Use paster again.

(myenv)$paster controller hello

Template

  • templates/hello.mako

Hello World!<br>
Your environ looks like ${request.environ}
  • Update Controller to render template

Specify Route

  • Edit config/routing.py

# CUSTOM ROUTES HERE
map.connect('/', controller='hello', action='index')

Underlying stuff

  • Requests are dispatched to controller.

  • Controller is an ordinary Python class with methods called actions to handle application logic.

  • Controller interacts with Model to deal with data.

  • Controller passes the data to view template which displays the HTML.

  • User interacts with view.

Routes

  • Default URL Dispatcher used by Pylons.

  • It's RoR url dispatching reimplementation in Python.

  • map url to controller/action

  • It's located at config/routing.py

Templates

  • Default is Mako.

  • It is Text based.

  • Supports Includes, inheritance, embedding arbitrary python code.

  • Arbitrary Expressions, defs, template inheritance.

Database

  • No Default Database.

  • Both SQLAlchemy and SQLObject can be used.

  • So is Oracle (with db connector) and MySQL etc.

WSGI Standards

  • A way to develop Python web applications.

  • WSGI application is passed a Python representation of HTTP Request.

  • WSGI Middleware

  • Like Pipes in Unix.

  • Authentication, Logging, URL Redirection, Session, Compression.

Paste

Paste helps in developing such WSGI Middleware.

Pylons

Pylons glues together those Routes to Paste based Request Handlers, WSGI Middleware which provide Session, Caching with Database layer and passes the information to templates such as Mako.

paster

This is a very useful script that uses the Paste Script package and has a number of subcommands including paster create and paster serve,that are for creating a new Pylons project and serving a Pylons application, respectively. You will also see paster make-config and paster setup-app, which are for handling the creation of a config file from a distributed Pylons project and for setting it up.

Pylons

This is where everything needed to glue together the other components of Pylons is found. Pylons itself is relatively small, so if you are the curious type, feel free to look at its code to get a feel for how everything works.

Setuptools and Eggs

This contains the methods used by the easy_install script to provide all of its features and allow the use of egg files. Eggs, package format, but is simply a zip file with some meta-data such as dependency used distutils packing system. It is commonly used way for packaging in Python.

simplejson

This package converts data back and forth between JSON and Python formats and is used by the @jsonify decorator mentioned earlier. Pylons application developers also occasionally use simplejson directly in their controllers.

decorator

This is a simple tool used by Pylons to create the @validate and @jsonify decorators.

Beaker

Beaker is a piece of software used internally by Pylons to implement its Caching and session functionality but you would never normally interact with Beaker yourself directly.

Tempita

Tempita is a small template language that is a dependency of Paste. It is used only behind the scenes for simple variable substitutions when you create a new Pylons project directory with the paster create command described later in this chapter.

Mako

Mako is one of the three template languages that Pylons 0.9.7 supports out of the box. The others are Genshi (an XML template language) and Jinja (based on Django’s template system). You have to install Genshi and Jinja separately if you want to use them, whereas Mako is included in the default Pylons installation because it is the recommended template language to use.

WebOb

This provides the new pylons.request and pylons.response objects in Pylons 0.9.7.

WebError

WebError provides Pylons’ powerful interactive debugging and traceback functionality described in Chapter 4.

WebHelpers

WebHelpers is a collection of stand-alone functions and classes that provide useful functionality such as generating common HTML tags and form fields, handling multiple pages of results, and doing much more.

FormEncode

FormEncode is a library for validating form submissions from web sites. Although Pylons doesn’t use it internally, Pylons users work with it so often that it is considered an essential part of Pylons. The FormEncode package also includes a module named formencode.htmlfill that can be used to populate a string containing HTML fields with values and error messages. Together FormEncode and HTML Fill make an ideal tool set for handling forms in a Pylons application.

nose

This provides tools to help you write and run automated unit tests.

Pylons Resources

Pylons Book

https://dl.dropboxusercontent.com/s/c7lhtpqt1i91jll/pylons_cover.jpg

Python Web Frameworks

By Senthil Kumaran

Python Web Frameworks

Python Web Frameworks

  • 1,244