Python / Technology / Web Development

A Quick Guide To Python Web Frameworks

SHARES
ShareTweet

What is a Web Framework?
Web frameworks allow developers to write web applications or services without needing to deal with low-level details, including protocols, sockets and process/thread management. Common uses of web frameworks allow you to get form parameters, handle cookies, deal with sessions and so on.  There are a wide variety of frameworks available, including:

Full-Stack vs. Non-Full-Stack
There are two main types of frameworks — full-stack, which provides all the code you need and non-full-stack, which leaves the extra code and other details in your hands, but gives you more control. With full-stack frameworks, you get common things that save you time, such as templating layouts, form generators and validators and so on.

Frameworks also include functionality built into their code or through extensions to let you take the common steps needed to run web applications.

These common operations include:

  1. URL routing
  2. HTML, XML, JSON, and other output format templating
  3. Database manipulation
  4. Security against Cross-site request forgery (CSRF) and other attacks
  5. Session storage and retrieval

Let’s take a closer look at some popular full stack frameworks:

Web2py
The lead developer of Web2py is Massimo Di Pierro, a Professor of Computer Science in Chicago.. The platform has excellent documentation, including a 600-page PDF that includes an introduction to the Python programming language..

Even if you’re new to Python web development, you’ll find Web2py invaluable.   Security is built-in, with generated forms that have field validation; sessions are stored server-side, so tampering with cookies gains users nothing. Database support includes built-in SQL generation for the ten most popular databases and Google App Engine.

Architecturally, Web2py follows the Model-View-Controller (MVC). It doesn’t go down the PHP embedded tags route, but instead generates html from code. Once the user has defined models, they get a complete administrative interface for free.

With all the good that Web2py brings, there’s one downside — currently it only supports Python 2.7, and there’s no Python 3.x. support. Supposedly the latter is in the works; if or when that happens, I would wholeheartedly recommend web2py. If your users are happy with Python 2.7, then web2py is going to be the framework worth using.

Django
Unlike web2py, this framework does include Python 3.4 support. It’s been designed from the start to leverage a lot of native Python, and make less use of third-party frameworks or libraries.

Although it’s another MVC framework, the controllers are called Views and they encapsulate the logic responsible for processing a user’s request and returning the response.

Technically, Django uses an ORM (Object Relational Mapper) to map its objects to database tables, in contrast to web2py’s Database Abstraction Layer (DAL), which maps objects to queries, tables and records.

The same code works with different databases and makes portability between different database types easier. Django works with PostgreSQL, MySQL, SQLite and Oracle. Other databases can be used with third-party drivers.

Like Web2py and the other frameworks, Django is not a content management system (CMS) such as Joomla, Drupal or WordPress.  The user will have to write code in order to make a working website. Django is often used in high-traffic sites such as Disqus, Pinterest, Instagram and Mozilla to help handle their operations. A large list (259 pages!) of Django-powered Websites can be found at Djangosites.org.

Django is by far the largest Python-based web framework. It is supported by a large and active community. It comes with a powerful admin interface as well as many other features out of the box. Django offers model-based forms, has its own templating language, and has excellent documentation available.

Django is great for:

  • Developers    who like to share ideas with each other via online forums   
  • Developers     who want to build something quickly with powerful built-in tools

Useful Django apps:   

  • South     (for schema and data migrations)   
  • Django Celery
  • Django Rest Framework or TastyPie
  • Django Extensions

TurboGears
Listed as one of three popular full-stack frameworks on the Python wiki web frameworks page, TurboGears is worth noting, but may not be the right solution for every developer.   All frameworks use other libraries and frameworks, but TurboGears does so more than Django and web2py. For example, database access is done through SQLAlchemy or Ming for NoSQL.

Getting started is relatively painless, but then there’s quite a steep learning curve to understand how to actually use those features.

Most notably, TurboGears offers:

  • The functionality to start as a microframework and scale up to a full stack solution
  • Code that is as natural as writing a function
  • A powerful and flexible Object Relational Mapper (ORM) with real multi-database support
  • Support for Horizontal data partitioning (aka, sharding)
  • A new widget system to make building AJAX-heavy apps easier
  • Support for multiple data-exchange formats
  • Built in extensibility Pluggable Applications and standard WSGI components
  • Designer friendly template system

Like Django, TurboGears is used by a wide range of well-known sites, including:

SourceForge – Allura, the SourceForge project management suite

  • Fedora Community – Provides a window into the Fedora distribution
  • Kamisons – South Indian and Arab eCommerce
  • PyF – Flow based Python programming framework with web gui for drag’n’drop graphic development
  • Moksha – Web framework that aims to simplify the creation of highly-interactive real time web applications

Pyramid
Pyramid, the successor to the widely-known and highly popular Pylons, doesn’t do a lot out of the box, but it is extendable by the user.  

A framework for large applications. It aims to be flexible, unlike “everything-in-the-box” Django. For example, templating and database administration require external libraries. Pyramid web applications start from a single-file module and can evolve into ambitious projects.

Pyramid is great for:

  • Getting started quickly on development projects
  • Developers working on API projects
  • Prototyping a concept
  • Developing large web applications, such as a CMS or a KMS

Pyramid is used by: Hypothesis, DiscNW,ZOPYX,Majerti,ITCase, Klein & Partner KG


Cubic Web
Rather than look at other non-full-stacks such as Flask, Bottle and CherryPy, I’ve included something of an oddity in this list: Cubic Web.

This is a semantic Web application framework. The official W3C definition of semantic Web is “a common framework that allows data to be shared and reused across application, enterprise, and community boundaries.”

Instead of separate views and models, a cube includes both—it’s a component that can be built from other components. One or more cubes are assembled into an instance along with configuration files, a Web server and a database (i.e., it sounds a bit like Docker, but it’s for the Web).

There’s something of a learning curve with this approach, but that’s typical for all modern Web development.


Cubic Web’s main features are:

  • an engine driven by the explicit data model of the application,
  • a query language named RQL similar to W3C’s SPARQL,
  • a selection+view mechanism for semi-automatic XHTML/XML/JSON/text generation,
  • a library of reusable components (data model and views) that fulfill common needs,
  • the power and flexibility of the Python programming language,
  • the reliability of SQL databases, LDAP directories, Subversion and Mercurial for storage backends.

Flask
Flask is a micro framework that, if you can believe it, was originally created as an April Fool’s joke that proved a single file framework could exist. It strives to be simple and small; the entire framework consists of only a handful of modules. There is no skeleton to start from; instead, the user starts with a blank page. While Flask doesn’t provide a lot out of the box, there are Flask extensions available to add in ORM, form validation, upload handling, etc.

Despite its simplicity, Flask is used by popular websites including LinkedIn and Pinterest.

Flask is great for:

  • Learning programming
  • Developers who care about best practices and “tasteful” code   
  • Developers who want to prototype something quickly   
  • Developers who need a standalone app

Popular combinations of framework templating and ORM include the following:   

  •     Flask     + Jinja2 + SQLAlchemy
  •     Flask     + Mako + SQLAlchemy
  •     Flask     + Jinja2 + Peewee

    Flask     + CouchDB

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *