Django, Flask, and Pyramid are frameworks for web development in Python. A web framework includes a collection of packages or modules that allow developers to write web applications without having to handle low-level details like sockets, process/thread management, or protocols.
Whenever we start a new project, we begin by thinking about its architecture and technology stack. Most of the time, we pick the stack that we're used to. But sometimes it's worth to think whether our habits and preferences are enough to argue for choosing that particular technology.
In this article I want to take a closer look at three Python frameworks – Django, Flask, and Pyramid – and show you their pros & cons together with tips on how they can be used to match different types of project requirements and assumptions.
Django was built with ‘batteries included’ idea in mind. That’s why it allows to quickly create a functional application we can gradually improve and develop. Developers get an ORM that supports the most essential database systems (PostgreSQL, MySQL, MSSQL, Oracle, SQLite3) and migrations.
Other advantages of Django are its advanced templates language, forms services, auto-generated admin panel, straightforward user and permissions system, and decent protection against attacks such as CSRF, clickjacking, SQL Injection, and XSS. Another significant advantage of Django is its comprehensive documentation.
Thanks to all these advantages, Django is today surrounded by a growing community of developers who have enriched it with amazing extensions. Many people consider Django a perfect framework for creating a CMS. No wonder that it's being used for that purpose by giants like Instagram, IKEA, Disqus, Washington Post, and NASA.
Are there any cons to using Django?
Many advanced developers complain about the ORM that has a particular syntax and doesn't allow taking full advantage of opportunities offered by the SQL language. Another thing is that Django is a megaframework. If we need higher responsiveness in our project, it might turn out that Django mechanisms are just too heavy and don't allow for the quick response we're aiming for.
Flask is almost a direct opposite of Django. It's a microframework that delivers only minimal tools and requires everything else to be manually configured. That means developers have full flexibility in choosing their tools.
If you'd like to use a different ORM than the popular SQLAlchemy, it's not a problem! (Btw. I recommend testing PonyORM though it’s not production-ready yet as it lacks migration support). Want a different template language than Jinja2, be my guest! Thanks to this type of approach, Flask works great whenever developers want to have full control over every single element in their application.
Some of the most popular portals based on Flask are Pinterest and LinkedIn. The former was originally based on Django, but the high demands it put on the organization's database finally forced Pinterest to migrate to Flask.
Flask works best in projects where the architecture is based on microservices. On the other hand, launching a project with Flask might become more challenging because of the time and effort required to write up and configure everything manually – while all these elements are simply provided in Django. So by picking Flask, you're basically trading comfort for greater flexibility.
The last framework I wanted to talk about here is Pyramid. The framework is often considered a viable alternative to both Django and Flask. Some people say that it's a framework designed for creating frameworks, other say that it’s Flask on steroids.
At its core, Pyramid contains basic mechanisms for URL routing with views, useful authorization functionalities and quite large set of plugins and third party apps that can provide for example a scaffold that serves as the equivalent to the admin panel we get in Django. You'll find that its toolset is slightly larger than the one provided in Flask, however, you still need to choose the ORM, template language, forms services, and others.
All in all, Pyramid can become a solid foundation for a project. Granted, it can be slightly overwhelming at the beginning, especially if you're coming from Django. Pyramid is used today by Mozilla and Yelp.
Whenever you begin a project, it's a good idea to write down the architecture properly and define your assumptions about its development.
If you're building an application based on a robust CMS, Django might be the best pick because it will allow you to create a prototype quickly and develop it into a fully functional service.
But if your project requires top efficiency or you don't want a framework's assumptions to constrain you in realizing the specific requirements of your project, it's a good idea to choose Flask or Pyramid.
Have you got any questions about these frameworks? Drop me a line or leave a comment below, I'm always happy to share my knowledge and help developers choose the right technology for their project.