Main image of article 5 Top Python Web Frameworks: A Walkthrough
shutterstock_172084607 Today, we’re going to look at the top five Web frameworks available in Python. When it comes to languages and frameworks used in Web development, PHP and ASP.NET enjoy near-ubiquity across the Web. For example, WordPress, Joomla, Drupal, and a few hundred other open-source Web applications are built on PHP. But PHP and ASP.NET aren’t the only two options for the modern Web. Given how many free hosting Websites are Linux-based and include both PHP and Python, it’s surprising (at least to me) that there aren’t more Python-powered Websites around; if you ask me for a theory on this, I suppose that Python, while easy to learn, is still regarded as more of a general-purpose programming language than one optimized for Web development (Your own mileage with that theory may vary, of course.)

Full-Stack vs. Non-Full-Stack

The first three frameworks I've listed below are full stack. This means they provide all the code needed, from form generators to templating layouts and forms validation, and leave you to wire things according to your specific needs. With the non-stack frameworks, if you want to create a full-featured website, you will need to add a lot of code and extra bits yourself. This takes skill and time.

Web2py

The lead developer of Web2py is Massimo Di Pierro, a Professor of Computer Science who is based in Chicago. The platform does have excellent documentation, including a 600-page PDF, and that includes an introduction to Python. What I like most about web2py is its completeness. If you are new to Python Web development, you'll have many questions, and web2py seems to have answers to everything. Security is built-in, with generated forms that have field validation; sessions are stored server-side, so tampering with cookies gains you 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 you've defined your models, you get a complete administrative interface for free. I like web2py, even though it took a little time to move from 'just' supporting Python 2.7 to Python 3.x support. I only started recommending it once it finally came through in terms of supporting later versions, but keep an eye on it (the website currently lists 3.5/3.6/pypy). You can view a list of websites powered by web2py.

Django

Unlike web2py, this framework has been diligent about supporting the latest versions of Python in a timely manner (Django 2.2 supports through Python 3.7). 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; 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; you have to write code to make a working Website. Django is used in high-traffic sites such as Disqus, Pinterest, Instagram and Mozilla. A large list (259 pages!) of Django-powered Websites can be found at Djangosites.org.

TurboGears

Listed as one of three popular full-stack frameworks on the Python wiki web frameworks page, TurboGears is my least favorite of the three. All frameworks use other libraries and frameworks, but TurboGears does so more than Django and web2py. For example, database access is done through SQL Alchemy 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. Here's a list of websites using TurboGears.

Pyramid

Pyramid is the successor to the widely known Pylons. Here's the code for “Hello World” using Pyramid:
from wsgiref.simple_server import make_server

from pyramid.config import Configurator

from pyramid.response import Response

 

def hello_world(request):

return Response('Hello %(name)s!' % request.matchdict)

 

if __name__ == '__main__':

config = Configurator()

config.add_route('hello', '/hello/{name}')

config.add_view(hello_world, route_name='hello')

app = config.make_wsgi_app()

server = make_server('0.0.0.0', 8080, app)

server.serve_forever()

Admittedly, it doesn't do a lot as-is, but you can extend it. Building out a full-blown Website is documented at the following link, in 21 chapters.

Cubic Web

Rather than look at other non-full-stacks such as Flask, Bottle and CherryPy, I've included something of an oddity: 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.” In a 2001 article, Tim Berners-Lee suggested that the Web should evolve from HTML into something better understood by computers; it’s worth finding that paper, and learning more about the semantic Web, before trying to understand Cubic Web. 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, as well, but that's typical for all modern Web development.

Conclusion: Python Still Strong

Popularity-wise, I think Django wins, though my favorite (at least from a completeness and documentation standpoint) is web2py. If you have more time, then consider one of the non-full-stack options such as Pyramid or Cubic Web. And if you're just breaking into Python for the first time, take a gander at how the language has become vital for everything from machine learning to data analytics; in a sign of its continued strength (despite its advancing age), it continues to pay tech pros an extreme healthy average salary.