A Guide to Python Web Frameworks

Python is a valuable and highly flexible programming language for teams trying to get a web app up and running quickly. To accelerate the development and prototyping process even more, many developers turn to Python’s robust collection of frameworks to handle the details of implementation.

There are a number of popular Python web frameworks out there, from robust full-stack options designed for enterprise-scale applications, to ultralightweight microframeworks optimized for building small, specialized tools.

In this article, we’ll look at some of the most popular Python frameworks and explore some of their advantages and idiosyncrasies.

Full-Stack or Microframework?

One of your main considerations in choosing a framework should be the size and complexity of your project. If you’re trying to get a feature-rich web app that will integrate with the rest of your organization’s systems, then a full-stack option could potentially save your team lots of time and effort. If, on the other hand, you’re working on a small, relatively simple project with few requirements, then a microframework might have everything you need.

A related question is how much “scaffolding” you need from a framework. A full-stack option should have just about everything you need to actually build a functioning web app–form generators, templating, access control lists, etc–so your team can assemble the pieces as needed. This is an attractive option for teams that are trying to get a product together quickly because it allows them to focus on actually building the app and not the stuff that goes around it. But if you have complex custom requirements or are working with a lot of customized software already, you may not be able to take advantage of all those libraries in the first place.

Full-Stack Options

There are a bunch of full-stack options when it comes to Python frameworks. Some prominent examples include TurboGears 2, Pylons, and Web2py. That said, the most popular option, by far, is Django.

Django is the most popular Python framework around, and it’s easy to understand why. Thousands of websites are currently using Django, from daily newspapers to social media and sharing sites to major foundations and nonprofits. Since Django was originally developed for use in the newsroom, it’s no surprise that major daily publications like the Washington Post and The Guardian rely on it. Startups like Eventbrite and Disqus have turned to Django to scale quickly, while social media behemoths like Instagram and Pinterest have used it to power their dynamic web apps.

As a framework, Django is known for being fast to build and friendly to beginning programmers. It’s a “batteries included” framework, meaning it supplies all the critical components you need–template rendering, ORM, authentication, routing, etc. It’s also exceedingly well-documented, which isn’t necessarily the case with some other popular frameworks.

By taking care of a lot of decisions, Django can dramatically speed up the time it takes to bootstrap a new project. What you gain in speed, though, you may lose in long-term flexibility. For example, Django’s built-in ORM is considered fine for a majority of cases, but it’s not as powerful an ORM as SQLAlchemy, which is widely viewed as the best Python database abstraction tool. While you can theoretically use SQLAlchemy with Django, you’ll lose a lot of the functionality that makes Django so appealing to begin with.

Web2py is another popular full-stack framework. One thing to keep in mind about Web2py is that it doesn’t support Python 3. The original developer of Web2py has promised a Python 3-compatible successor, but as of 2017 it hasn’t appeared.

Despite now being nearly a decade behind the latest version of Python, Web2py is still used by many major organizations, including a number of international banks. What makes this older framework still appealing for many developers? For one, it’s as easy to learn as Django, but also more flexible and extremely portable. The same code can run on just about any VPS with a SQL database or MongoDB, whether AWS or Google App Engine.

Web2py is extremely well supported with book-length documentation and a passionate community behind it. Another neat feature is that Web2py comes with its own IDE that includes a code editor, debugger, bug ticketing system, one-click deployment, and more. If your organization is committed to Python 2 for the foreseeable future or you plan to make use of some older Python libraries and software, then Web2py could suit your needs just fine.

Pyramid isn’t technically a full-stack framework. It instead bills itself as the “Goldilocks” framework, feature-rich without enforcing one way of doing things, lightweight without leaving you on your own as your app grows. It’s a favorite framework among many experienced Python developers thanks to its modularity and transparency, and has been used by small teams as well as tech giants like Dropbox, Yelp, SurveyMonkey, and Mozilla.

Practically speaking, nearly all of Pyramid’s components can be swapped out. You can choose how you connect to a database, or even what type (or types) of databases you want to be able to connect to. It doesn’t enforce certain choices for you the way Django does, and it also discourages the sort of “magic” features that handle certain tasks automatically but don’t always behave in a predictable or desirable manner.

Pyramid is often celebrated for its security system, which makes it easy to set up and check access control lists. Another point worth calling out is Pyramid’s Traversal system for mapping URLs to code, which can make building RESTful APIs much easier.

Microframeworks

What if you don’t need the hand-holding or complexity of a full-stack framework? Many modern web apps require lots of moving parts, including database abstraction, form validation, and customized access control lists. But there are also plenty of web apps that don’t need any of that. For those kinds of projects, a microframework may be just what’s needed.

These ultra-lightweight frameworks are designed to get dead simple web apps up and running as quickly as possible. Their feature sets are minimal by design–anything functionality you could get by installing another library is deliberately left out. The plus side of this minimalist approach is that your code can be cleaner and your website faster. This is at least partly because microframeworks are generally less abstracted than full-stack frameworks. The code you write will be much closer to actual HTTP functions than with a more beginner-friendly framework.

Flask is probably the most popular microframework for Python, and one of the most popular Python frameworks, period. Like Django, it’s designed to get web apps up and running as quickly and painlessly as possible. Despite its small size, Flask has been used by big companies, including LinkedIn and Pinterest.

However, there’s a twist, which is that Flask is definitely suited to smaller, simpler projects than Django. Out of the box, you can expect a development web server, built-in unit testing, and support for Google App Engine. Now here’s a taste of what you won’t find in vanilla Flask: There’s no database abstraction layer, form validation, authentication, or upload handling, though all these features can be added through extensions. Some of the more common setups include Jinja2 for templating and CouchDB or SQLAlchemy for the database.

Some Python developers prefer Flask to Django because Flask is more “pythonic,” in that it mostly sticks to the Python mantra of there being one way to do something and that code should be explicit about what it’s doing. If you value clean code and don’t mind coding closer to the metal, then Flask may be a good option.

Bottle is another popular microframework. It was originally designed for building web APIs, and that’s still a good use case for it. What’s novel about Bottle is it tries to implement everything in one single file, which should give you an idea of how “micro” it’s designed to be. Out of the box it comes with routing, templating, utilities, and some basic abstraction over the WSGI standard. As with Flask, you’ll be coding much closer to the metal than with a full-stack framework. Despite its small size, Bottle has been used by the Netflix team to build web interfaces.

Other Frameworks

We’ve covered some of the most popular Python frameworks for general use, but there are a ton of other options out there. Below, we’ll briefly look at a few others that may be good candidates in specific situations.

Tornado is a Python framework specifically designed to solve the C10k problem (ie, it can handle more than 10,000 concurrent connections if configured properly) using asynchronous I/O. This gives it a strong appeal for projects that require high performance and tens of thousands of concurrent users.

CubicWeb is an interesting framework that’s totally different from every other one in this article. It’s part of the semantic web, which the W3C describes as “a common framework that allows data to be shared and reused across application, enterprise, and community boundaries.” In layman’s terms, it’s a framework that strives to represent data in a way that’s easier for computers to understand. It replaces the Model and View part of MVC with a single concept–the data cube which can be used to build other components, sort of like Docker for the web.

Falcon is a microframework that’s all about building cloud APIs. Like other microframeworks, it keeps dependencies to a minimum and avoids magical features. Unlike other frameworks, however, it’s not really designed for serving HTML pages. Instead, it’s for building fast, RESTful APIs.

If you really want to get into the guts of these frameworks and see how they compare for yourself, there’s a great GitHub repo that contains the same app written in a host of different frameworks. A Python developer can also help you pick which framework may be best suited to your project.

Related Post