PYTHON IS BOTH A FUN TOY AND A FRIGHTENING FLAMETHROWER. SAME GOES WITH WHAT YOU CAN DO WITH PYTHON.
Python is loved by hobbyists, scientists and architects alike.
It’s damn easy to get started with, has higher-order abstractions and metaprogramming capabilities to build large and complex systems, and has truck-loads of libraries for doing pretty much anything. Sure, there are limitations when it comes to concurrency and strong typing, but you can work around them.
In this article, we’ll cast a look at some of the best Python frameworks when it comes to building web applications large and small.
- Django The Django framework has withstood the test of time the go-to web framework for the Python community. If you assaulted a Python developer in their sleep and forced them to build a web application at gunpoint, they’d automatically reach for Django the way a Rubyist will reach for Rails.
And there’s a good reason for that. Django is, as the tagline says, “a web framework for perfectionists with deadlines.” It’s what is called a “batteries included” framework (much like how Python is a batteries-included language), which provides all common functionality out of the box.
With these features baked in, Django massively cuts down on development time:
A handy and pleasant ORM, with migrations created and applied automatically by the framework.
Scaffolding for automatic generation of admin panel based on your models.
Support for cookies, sessions, middleware, templates, etc.
Security features like XSS prevention, CRSF prevention, etc., are applied automatically.
Works with practically all databases out there (it’s easy to find adapters where official support doesn’t exist)
First-class support for Geographical data and spatial queries though GeoDjango
And much, much more. Suffice it is to say Django is a full-blown, friendly web framework.
Is Django for you?
Absolutely yes.
Django makes excellent sense for all use cases, whether rapid prototyping or planning an enterprise application. The only rough edge you’ll come across is the framework’s structure. Since Django bends over backward to make development fast and easy for you, it imposes its structure (a concept called “convention over configuration”) on the developer, which you may not agree with. For instance, if you want to replace the Django ORM with something else (say, SQL Alchemy), be prepared for surprises.
Interested in becoming full stack developer with Django and Python? – Check out this fantastic online course.
- Flask While Django takes up almost all the mindshare in Python web development, Flask is a strong contender.
As opposed to Django, Flask is a “micro-framework,” which means it focuses on getting a few, bare minimum things right, and leaves the rest to you. This “the rest is up to you” can be a source of frustration or delight, depending on what your goals are. For those that know what they’re doing and want to lovingly craft their web applications by choosing components of their choice, Flask is a godsend.
Flask offers the following features:
Routing, templating, session management, and other useful features.
Full support for unit-testing
A minimal, pluggable architecture
First-class REST support
Support for Blueprints, Flask’s unique take on architecture for tiny web applications
Choose your packages for ORM, migrations, etc.
Flexible application structure — put your files where they make the most sense to you
Static file serving
WGSI compliant
Is Flask for you?
As already said, Flask is a minimal web framework, with everything broken up into individual components that you can swap out. If you’re in a hurry to build a prototype, you’ll spend a lot of time making trivial decisions on the database, folder structure, routing, etc., that can prove counter-productive. Flask works best when you’re on to a stable, serious project of medium- to large-scale, especially REST APIs.
- Bottle If you thought Flask’s approach wasn’t minimal enough and that you’d still like more control, say hello to Bottle.
Bottle strips out even more, to the point where the only dependency is the Python standard library. This means no pip install this or pip install that, though you’d most likely need to before long. Here’s why Bottle stands out for some people:
Single-file deployment. Yes, your entire application lives in a single “.py” file.
No external dependencies. If you have the right Python version installed, you’re good to go.
Supplies its templating engine, which can be swapped out with Jinja2, Mako, or Cheetah.
Support for forms, headers, cookies, and file uploads.
Built-in web server, which can be easily replaced.
Is Bottle for you?
If you’re making a really small app (say, less than 500 lines of code) with no special requirements, Bottle might make a lot of sense to you. It’s a complete no-nonsense approach to creating web apps, but in practice, you’ll find you’re more hindered than helped by Bottle. The reason is that the real world is always changing and before you know it. New requirements will be dropped on your head. At that point, putting everything in a single file would become a chore.
Also, if you think Bottle and Flask are almost alike, you’re right. Proposals of merging the two date back to 2012, and even Armin, the creator of Flask, agrees with that. However, Marcel, the creator of Bottle, maintains a strict ideological distinction because of the single-file approach and insists that the two remain separate.
- Zope Zope is complicated to explain, but I’ll attempt. Yes, it’s a web framework that can be used to build applications large or small, but that’s not all. Zope is more of a platform that consists of tools (web frameworks included) created under the Zope philosophy and maintained by the Zope Corporation.
Zope has several interesting components and features suitable for enterprise application development:
A component registering and discovery architecture to configure a large app.
ZODB — (the only) object database for Python for storing objects natively.
Full-fledged framework and standards for Content Management Systems
A set of web application frameworks — the canonical one is still called Zope, although several new frameworks (like Grok) have been built on top of it.
Strong standards for software development, release, and maintenance.
Is Zope for you?
If you’re after a highly structured environment for building really large apps, Zope is good. That said, you’ll run into your fair share of issues as well. While Zope continues to evolve, the community is really small, to the extent that many Python developers haven’t even heard of it. Finding tutorials and extensive documentation is hard, so be prepared to do a lot of digging around (though the community is really helpful!). Also, the Python developers you come across may not want to learn Zope and “dilute” their skill-set.
- TurboGears TurboGears is a highly flexible framework that has a genuinely composable architecture. It’s a framework that scales as per your needs, and you can use it to build a single-file application or a multi-tenancy behemoth.
TurboGears has some elegant features, some of which are either not present in popular frameworks (like Django) or are hard to build:
First-class support for multiple databases
Multi-database transactions
Highly modular — start with a single file and scale out as much as you need
A powerful ORM (SQLAlchemy, which is more mature and capable than Django’s ORM)
Pluggable architecture based on the WSGI specification
Built-in support for database sharding
A function-driven interface as opposed to deep, rigid object-oriented hierarchies.
Is TurboGears for you?
If you want to develop happily and want a tested, mature, and robust framework away from the media noise of “awesome, next-gen” and all that, TurboGears is a great fit. It’s highly respected in the community and has complete, extensive documentation. Sure, TurboGears isn’t opinionated, which means initial setup and configuration time can be more, but it’s the ideal framework for enterprise application development.
- Web2py Web2py started as a hobby project and was released in 2007. Its goals are to make web development easy and accessible to everyone.
As a result, Web2py takes the zero-dependency approach to the extreme — it has no requirements, nothing to install, and includes a full-featured Web-based editor for development, database management, as well as deployment.
You can almost think of it as Android Studio, which is more of a complete environment than just a framework. Some nice features that Web2py has, are:
Virtually no learning curve.
Minimal core (only 12 objects), which can even be memorized!
Pure-Python templating
Protection against XSS, CSRF, and other attacks
A pleasant and consistent API
Is Web2py for you?
Web2py is a stable and fun framework, but it’s hard to recommend it against other options like Django, Flask, etc. There are hardly any jobs, and the unit testing story is not great. That said, you might enjoy the code API and the overall experience the framework offers, especially if you’re building REST APIs.
- CherryPy CherryPy is another microframework that aims to provide basic framework functionality and quickly get out of the way.
While it’s comparable to other microframeworks like Flask, CherryPy boasts of some distinction:
It contains a built-in multi-threaded server (something that remains on the wishlist of Flask)
The (single) web server can host multiple applications!
Serve your application as a WSGI app (to interface with other WSGI apps) or a plain HTTP server (which performs better)
First-class support for profiling and unit-testing
Runs on PyPy (for the true performance junkies), Jython, and even Android
CherryPy does all this, and then the usual you’d expect from a web framework.
Is CherryPy for you?
If you’re building RESTful services mostly, CherryPy is a much more serious contender than Flask. It’s a decade-old framework that has matured nicely and is suitable for small and large applications alike.
- Sanic The rise of Node and its asynchronous programming model left many communities feeling behind, Python’s included. In response, a flurry of async web frameworks have appeared on the radar, Sanic being one of them.
Sanic is heavily inspired by Flask, to the extent that it borrowed the route decorators, Blueprints, and other fundamentals hook line and sinker. And they’re not ashamed to admit it. What Sanic brings to the table, if you’re a Flask fan, is true non-blocking I/O to meet the performance levels of a Node application. In other words, Sanic is Flask with async/await support!
When compared to CherryPy, Sanic has an incredible performance advantage (just think of how it would fare against Flask!). Check out the following results tested by DataWeave:
As you can see, once the concurrency numbers start exceeding 50 per second, CherryPy practically chokes and throws up a high failure rate.
Is Sanic for you?
While the performance characteristics of Sanic blow everything else out of the water, it may not be the best choice for your next project. The main reason is the lack of asynchronous libraries. The bulk of existing Python tools and libraries were written for the single-threaded CPython version, with no forethought for high concurrency or asynchronous operations. If, for example, your favorite ORM does not support asynchronous operations, the whole point of using Sanic gets defeated.
Because of these maturity and availability reasons, we won’t examine any more async frameworks in Python.
9.Masonite
I came across this framework a while ago and thought it was a step in the right direction. Since then, version 2.0 has been released, and I feel like the time has finally come to give Masonite some love.
Simply put, Masonite is the Python version of Laravel (a famous PHP framework, in case you didn’t know). Why does that matter? It matters because Laravel was built on the principles of Ruby on Rails, and together these two frameworks allow non-Ruby devs to experience the “Rails Way” of doing things.
Laravel (and to an extent, Rails) developers will feel right at home and would be up and running in literally no time. When I tried Masonite (and I did submit an issue or two, including a bug!), I was able to build REST APIs with exactly zero thinking because my Laravel muscle memory was doing everything.
As a batteries-included, full-stack framework, Masonite brings several interesting things to the table:
Active-record style ORM
Database migrations (which, unlike Django, need to be created by the developer)
A powerful IoC Container for dependency injection
Own CLI (called “craft”) for scaffolding and running tasks
First-class support for unit testing
The biggest “rival” for Masonite is Django, as the community is doing its best to market the framework as easy, delightful, and the next big thing. Whether it will surpass Django is something time will tell (if you ask me, it does have a decent shot), but for a discussion comparing the two, see here and here.
Is Masonite for you?
Masonite is still a baby when compared to Django, so there’s no way it can be recommended over Django. That said, if you’re into the Rails way (or the Laravel way) of doing things, you’d appreciate what Masonite has to offer. It’s ideal for rapidly building prototypes that need everything pre-configured and easy to switch.
Conclusion
There’s no shortage of Python web frameworks out there, large and small. While you can pick up pretty much anything for a small project, an enterprise application has demands that not many of these frameworks can fulfill. If you ask me, for enterprise development, Django (to an extent), Zope, and TurboGears are what comes to mind. And even among those, I’m inclined towards TurboGears.
That said, any architect worth his salt can pick up a microframework and roll out their architecture. And this is pretty much what happens in practice, which explains the success of Flask and similar ideas.
If you are a newbie, then this online course would be helpful to learn Python.
Top comments (1)
I used Django for years, is not the best at anything but is very good at everything, not fast running but very fast for developing and is easy to make a good application with it.
I don't think that it applies to Django tho, everything in Django is explicit, everything is laid plainly in the settings file and even tho it comes with many defaults, those are easily findable and changeable.
What I do agree is that it comes with the general architecture already figured out, you just need to fill it. The documentation is glorious and comes with a lot of out of the box safety features. And as you mentioned, you can mix with other tools like Jinja or SQLAlchemy, is not always smooth and to me kinda defeats the purpose of using Django.
PS: If what you want is to make a REST API, there is a great side-project to use Django for that django-rest-framework.org/