DEV Community

Cover image for 2 Questions to Ask Before Choosing a Python Framework
Profil Software
Profil Software

Posted on • Edited on • Originally published at profil-software.com

2 Questions to Ask Before Choosing a Python Framework


description: How to choose Python web development framework. Examples, Advantages, Disadvantages of Popular Python frameworks. Choose best framework for web development.

Hello! My name is Łukasz! I’m a Software Developer at Python Software Development Company. Like many Python developers who start new projects, I had to make a decision which back-end framework I should use, and it wasn’t easy to choose the best one. In making the decision, I asked myself two questions that helped point me in the right direction.

How much time do I have to deliver a production-ready solution?

I am sure that none of us would like to delay delivery time and become unreliable business partners or pay a fine because of contract terms. Having short time to deliver a production-ready app, I would choose Django as a back-end framework.
Alt Text
Django is an advanced open source web framework whose main goal is to simplify developers’ work. It uses the DRY philosophy (Don’t Repeat Yourself). Django allows developers to reuse existing code and focus on really unique ones. It is maintained by the Django Software Foundation, an independent American organisation. It was first released in 2005 and has many built-in features like ORM, authentication, and admin panel, which are easy to set up. Django is a secure framework — developers don’t have to worry about most common security issues like SQL injections or cross-site scripting. We have security out-of-the-box. Django also has very good documentation for real-world applications. Because of the big community we can find many production-ready solutions and save development time.

Here’s an example of Django REST view:
Alt Text
A big advantage of the Django framework is its built-in admin panel. It allows users or developers to make changes to a database through a user-friendly interface. It is generated automatically with minimal configuration.

Here’s an example of what the admin looks like:
Alt Text
Django has a less-flexible structure than other frameworks. This is another big advantage especially if the development team is big. It helps keep back-end code well organised. Like any back-end application framework, Django isn’t perfect. As I mentioned above, it’s huge and includes many built-in features. In many cases we don’t need everything, but using Django we have to install all dependencies because it’s one big monolith. We can turn off the features we don’t need, but we still have to install them.

The big minus of Django is that we don’t have alternatives for many built-in libraries. The general rule is that Django works fine as long as you do things in the Django way. A good example is ORM. There’s no easy way to change an object-relational mapping library to another one (e.g. SQL Alchemy).

How customized is the solution I would like to build? (or How customizable is the solution I would like to build?)

Delivery time is very important but it’s also good to think about flexibility. Let’s imagine that we want to build some features and it’s not possible to implement some solution using a built-in library, or that it’s possible but we will have problems with performance. Or what if we would like to split our application into smaller microservices? Using a big Python web framework like Django requires installing all features, which makes our app big right from the start.

To help with the above two cases I would consider using a microframework, and the Python community provides many solutions. In my professional career I’ve had the opportunity to work with three very good alternatives to Django: Flask, Falcon, and Fast API.
Alt Text
Flask is designed to be easy to use and extend. It follows the principles of minimalism and gives more control over the app. Choosing it, developers can use multiple types of databases, which is not easy to do in Django. We can also plug in our favorite ORM and use it without any risk of unpredictable app behavior. In contrast to Django, it’s easy to integrate NoSQL databases with Flask.

Here’s an example Flask application:
Alt Text
The big advantage of using Flask is how easy it is to separate database logic from application views. It looks cleaner, and it’s easier to extend or replace.

In Django, database logic is mixed with application views that might be confusing and sometimes hard to understand when we debug the code.

Flask developers can also follow REST application patterns because it offers RESTful request dispatching. It also has API support, which Django does not offer.

Alt Text
A second good alternative to the Django framework is Falcon. It’s another light-weight microframework for building web APIs and app back-ends. The main focus of Falcon is REST APIs. It has a clean design and REST architectural style, but it’s not suitable for serving HTML pages at all.

The big advantage of using Falcon is performance. Falcon turns around requests significantly faster than Flask or Django. It compiles itself with Cython when available and also works well with PyPy.

Here’s an example of a Falcon resource class:
Alt Text
Falcon is a flexible microframework. Its authors leave a lot of decisions and implementation details to the developer. Like with Flask, we can choose ORM and a database engine that fits our needs and build the logic in the most convenient way. It also allows you to use NoSQL databases.

Alt Text
The last microframework I’ve had the chance to use is Fast API. It’s new, modern, and was inspired by its predecessors. It’s very fast because it’s built over ASGI (Asynchronous Server Gateway) instead of WSGI. It also provides built-in async support, which is not available in Flask or Django.

Here’s an example FastAPI view:
Alt Text
Fast API has a validation system which can detect invalid types at runtime and return a well formatted JSON response containing the reason for the bad input. It also generates documentation on the go, which is really helpful for developers. Fast API also allows users to choose the best ORM package and use it in a convenient way.

Another big Fast API feature is schema validation. It uses pydantic — a fast and intuitive package which relies on Python typing. It makes our code easy to understand.

Here’s an example of a pydantic model:
Alt Text
Fast API also has built-in security features and makes it easy to implement both HTTP Basic Auth and Oauth2. It is well documented and contains many practical examples.

Conclusion

It’s not easy to choose a proper Python back-end framework. I’ve had a chance to work with all of the above back-end frameworks and currently the best option for me is FastAPI. It is developed in a modern way and takes the best features from the other ones. Its flexible structure allows us to easily integrate it with other Python packages. If you have a tight deadline, I would consider using Django because of its many built-in, easy-to-setup features. The auto-generated Django admin panel for simple CRUD operations is another really big advantage for people who don’t need an advanced UI.

Top comments (1)

Collapse
 
mccurcio profile image
Matt Curcio

Good article
Thank you Lukasz,