Python began life in the late 1980s. It was developed by Guido van Rossum at Centrum Wiskunde & Informatica, a math and computer science research center in Amsterdam Science Park in the Netherlands. Van Rossum continues to be an influential figure in the development and guidance of Python. In fact, members of the Python community have bestowed upon him the regal title of Benevolent Dictator for Life (BDFL).
From those humble beginnings, Python has grown to become one of the most popular server-side languages on the Internet. According to W3Techs, it is used by more high-traffic sites than ColdFusion, PHP, and ASP.NET. More than 98 percent of those sites are running Python 2.0, and just over one percent are using 3.0.
Frameworks Makes Life Easier
Today, there is a wide variety of Python frameworks that make creating web applications much easier. These frameworks bring together the modules and packages that allow you to build applications quickly, without the need to take care of involved details, like sockets and protocols, all in one place.
In Part 1 of this two-part article series, we will examine some of the more popular Python frameworks. While most modern Web frameworks are used on the server, some are beginning to incorporate code that lets them run code on the client (e.g. Skulpt or Trinket). Python frameworks are often separated into full-stack and non-full-stack frameworks. Full-stack frameworks help developers create applications from the user experience right through to the database. Non-full-stack frameworks are those that handle anything less than complete development.
There are also micro-frameworks, which are non-full-stack frameworks less bloat. In some situations, it makes more sense to use a complement of micro-frameworks, while other applications demand a full stack. We will take a closer look at full-stack vs. micro-frameworks in Part 2 of this series.
Perhaps the Python framework with the highest profile is Django. It is an open-source framework that follows the architectural pattern of model-view-controller (MVC). It is named after Django Reinhardt, a French composer and guitarist who is widely considered one of the greatest guitar players in history. Two developers at the Lawrence Journal-World in Lawrence, Kansas, Adrian of Holovaty and Simon Willison, created Django in 2003 to build Web apps for the newspaper.
Django includes its own templating engine, as well as providing OOTB support for the widely popular Jinja2 engine. It also employs a regex based URL dispatcher, allowing complex mappings with very simple URLs.
One of Django’s strengths has been that it is a single package install. Other alternatives require developers to hunt down many components just to get started. Also, Django always had extensive documentation, historically a weakness for open-source projects. It is a robust, well-integrated framework with many plug-ins and add-ons from the user community. Also, the community behind the project seems much more organized, as is evident with its thorough documentation and tutorials.
TurboGears is a framework built on top of several reputable Python projects, such as SQLAlchemy, WebOb, Repoze, and Genshi. In a sense, TurboGears takes the approach of gluing together already established open platforms. Like Django, it utilizes the MVC architecture. It recently also incorporated a “minimal mode,” which allows it to act as a micro-framework.
TurboGears was developed by Kevin Dangoor in 2005. He distributed it as an open-source project in September of that year. In 2013, the project developers moved to support Python 3 and away from the Pylons code base they had been working with. Some of the advantages of TurboGears include:
Strong support for aggregates
Powerful object relational mapper
Transaction system that takes care of multi-database transactions automatically
Reusable snippets for templates
Lots of flexibility to incorporate non-standard components
Support for sharding
Template systems that make life easier for designers
A few of the drawbacks of TurboGears includes a very basic admin, fewer third-party apps, and rudimentary documentation. A major challenge with TurboGears is that it can be overwhelming. Because it is mash-up of other components that offer infinite flexibility, it can be overwhelming when trying to decide which path to take to complete a task.
One open-source framework, web2py, allows developers to create dynamic, interactive websites quickly. It is designed to eliminate many of the repetitive programming tasks that slow down development, like creating basic forms. In the beginning, it was created as a teaching tool. It was modeled after Django and Ruby on Rails, a framework for the Ruby language. Like TurboGears, it uses the MVC architecture.
The original source code was released in 2007 by Massimo DiPierro. At that time, it was known as the Enterprise Web Framework (EWF). Due to naming conflicts, it changed names several times and settled on web2py on Version 1.16. Several applications created using web2py include the Movuca Content Management System, music site NoobMusic, a diagnostic tool for networks called LinkFindr and the Instant Press blogging platform. In 2011, web2py captured a Bossie Award for the best open-source development software. The following year, its winning ways continued when it brought home InfoWorld’s Technology of the Year award.
Like Django, web2py has extensive documentation. New and advanced developers alike can download the Complete Reference Manual for free.
Some of web2py’s advantages include:
Easy to use. As a robust full-stack framework, it functions without any dependencies. This makes it easy to learn and deploy, as no configuration files are needed for installation. Once it is downloaded and installed, everything works. Developers get a database, a Web-based IDE, and a Web server as well as a powerful API with a number of core objects.
Excellent security. Web2py’s template language reduces the dangers of hackers using cross-site scripting. The abstraction layer prevents SQL injections while the forms created have field validation and stop cross-site request forgeries. Sessions are stored on the server to keep bad actors from messing with browser cookies, and every password is stored hashed.
Some drawbacks to web2py include:
Not easy to use the administrative interface on a regular basis
No permissions on the administrative interface
No built-in unit testing support
Rapid development. All functions have default behaviors. Forms are generated automatically, and high-level widgets and application grids are built in.
Flask is a Python micro-framework based on Jinja2 and Werkzeug. Similar to other frameworks, it is licensed under the Berkeley Software Distribution (BSD), a free software license that operates with few restrictions. Web sites that have used Flask include LinkedIn and Pinterest. It includes features such as:
Built-in unit testing support
Templating using Jinja2
Client-side sessions using secure cookies
Development server and debugger
RESTful request dispatching
Compliant with WSGI version 1.0
Flask is a young framework that came on the scene in 2010. The philosophical objective of Flask is to not impose conventions on the programmer, allowing you to use your choice of DB ORM, templating engine, session middleware, or other components that your project requires. This is generally, in my opinion, the purpose of a ‘microframework’.
I would say that a framework like Flask is better suited to a more experienced developer, not necessarily just to smaller applications. Of course, if you just want to make a simple REST API, then Flask is definitely good bet.
Like Flask, Bottle is a WSGI Web framework. Distributed as a single file, it does not have any dependencies outside of the Python Standard Library. Written by Marcel Hellkamp in 2009, it has minimal tools to start including templating, routing and a WSGI abstraction layer. This small, powerful framework is great for programmers looking for flexibility and basic functionality, building simple applications and websites and creating a Web API.
Its advantages include:
Fast template engine that comes built-in and supports Jinja2, Mako, and Cheetah
Many utilities that can access uploads, cookies, form data, headers, and other metadata
HTTP development server built-in that supports fapws3, Google App Engine, CherryPy and Paste
Support for dynamic URLs
Another advantage of Bottle is that its small profile makes it easy to embed it within a larger application without worrying about system-wide dependencies. Bottle is for you if you want to create small apps with a simple, clean and fast framework that is not verbose.
It is hard to believe, but these are just a few of the dozens of Python frameworks available to developers. Python.org reports that Django, TurboGears, and web2py are the most popular full-stack options. Here we added two of the higher-profile micro-frameworks to provide a thorough look at the framework landscape. Other frameworks with enthusiastic followers include Pyramid, web.py, Bobo, Albatross and CherryPy.
Finding the Right Fit
Choosing the right framework depends on the size of the project, its communication needs, whether it is a standalone application or not, the level of customization that is needed, overhead and a number of other factors. Just as important, it depends on which framework feels right for your personal working style. Review these projects and download their latest release. Take a test drive to make sure you are in the right vehicle before planning a major trip.