Why Learn to Develop in Flask and/or FastApi this 2024

- Andrés Cruz

En español

In this post, we are going to continue talking about why you should learn Python, web, this time, not focused on Django:

Why you should Learn to Develop in Django and Python in general

If not, what would be two frameworks with very similar structures such as Flask and FastAPI, which have more in common than if we compare them with Django, being a microframework and not a framework with “batteries included” as in the case of Django.

As we mentioned before, if you want to learn Python web, these frameworks are your options in web development with Python.

Before talking to you about the characteristics of these frameworks, let's see what they consist of and what they bring to us.

What is Flask?

Flask is a micro framework that allows you to create all types of web apps in a simple and scalable way. It is a web framework based on Python and has typical functionalities or that can be added through dependencies such as a template engine, ORM, packages to manage the user, etc.

Advantages

Flask is a general-purpose web framework, so we can use it to develop any type of web application. Based on this, let's look at its main characteristics.

  • Simplicity and flexibility: the greatest advantage we have in Flask is that being a micro-framework for general purposes like Django, it allows high customization and adaptability to any development you want to carry out, we can also develop any application little by little, at a time. Unlike Django, we can create the app little by little and with this, we have 100% control of what we want the application to do or not do.
  • Ideal for small projects: Since Flask is again a microframework, it is perfect for small web applications or even prototypes.
  • Large community: There is a large number of resources and extensions available for Flask. Being a framework that has been on the market for a long time, you will always find even some tip of the problem you want to solve.

What is FastAPI?

FastAPI is a more modern framework designed to create and design APIs with web Python and there is no faster framework to make APIs and be able to consume them with other technologies. Of course, we can also convert it into a more general-purpose framework like Flask in case that it is necessary.

Although, ultimately, it is a framework designed to create APIs, so here you can see the difference between both frameworks.

Advantages

  • FastAPI is a very fast framework due to its asynchronous nature; easily, we can define functions of this type.
  • Automatic documentation: Automatically generate documentation for your APIs, therefore, we can see and test the resources we create directly from the browser.
  • Data validation: It offers automatic validation of types and data in the API, again, it is a framework created to create APIs mainly.

Ideal Scenarios to Learn Flask/FastAPI

In this blog and my YouTube channel you will find many free resources to learn about Flask and of course FastAPI up to complete publications, I also have courses in Flask/FastAPI and also the most complete book; Of course, on the Internet you will find a lot of material about both technologies.

 

How long does it take to learn Flask and FastAPI?

Both frameworks are easier to start than the all-powerful Django, they are microframeworks, which means that we can create our application with a few lines of code, and when I say few, they are few:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
   return 'Hello, World!'

The previous code represents an application in Flask, yes, a COMPLETE application with all the letters, which runs in a single file, unlike Django where we have a lot of files and folders that represent our project, and it is Although both frameworks are web-type, they follow different philosophies, as we mentioned before, Flask and FastAPI are microframeworks and Django is one with everything included.

Therefore, by being able to add code and automatically be able to see in real time what that code represents, if it is a new route, if it is an HTML page, if you ruined the application... So, if you want to start web development with Python, these are your recommended frameworks.

Which one to choose Flask or FastAPI

Both frameworks are excellent, from now on, I am going to give my personal opinion on which should be your first Python framework.

Flask and Django are what CodeIgniter is for Laravel

What I mean with the previous sentence is that Flask is designed for more specific or simple applications, it's not that you can't make complete, complex and large applications, of course you can, the same applies to frameworks like CodeIgniter, but, You would have to implement functionalities that are probably more developed in larger frameworks such as Laravel and Django.

For example, if you want to create a Rest API with token and session protection, you can do it in Flask, but in Django it is much easier and faster when using DRF; so, the purpose of being a developer is to know when you have to use a technology and when you have to use another technology, seeing its advantages and disadvantages and adapting it as best as possible to the project you want to carry out.

Continuing to talk about Rest APIs, if your project you want to create is a RestAPI, then FastAPI is for you, which as its name indicates, is designed to create APIs, by providing automatic documentation for each resource you create, turning it into a great candidate for this purpose.

So which one to choose?

Having commented on the above, since it is a more general purpose to want to create a blog-type web application, forums... instead of a Rest API and when we are learning we usually first start creating simple web applications using HTML templates, my choice would be to first learn Flask, apart from the fact that FastAPI handles some implementations that can be a bit abstract, such as the automatic Pydentic documentation for model validations, dependency injection... all this is excellent, but for new web developers in Python and especially if you come of PHP or Node, I think it can be a little difficult to digest, if we compare it with Flask, which has a much more similar structure to other types of web frameworks based on PHP or JavaScript.

FastAPI, although it is a microframework, I think it is halfway between Flask and Django in terms of complexity when programming in the framework.

FastAPI incorporates some high-level features such as dependency injection, it is also a more abstract framework designed as its name indicates, to create APIs and Flask is more general purpose.

What should I know before working with Flask or FastAPI?

I already answered this point a little before in this and other publications, but, if Flask is your first web framework, whether you come from others or it is the first one you work with, yes, I highly recommend it, it is a framework evolutionary, in which each change we make can be seen reflected on the screen, which is excellent for learning to program.

Also, as in Django, in the end, we are using Python with ALL the advantages that this gives us by becoming familiar with the language so that later you can address topics that are specific to the framework and with this, do not feel lost, check the official documentation and see if you see yourself programming in this wonder.

Of course, you should know the evil trilogy of HTML, CSS and JavaScript, especially the first two, since it is of little use if we make the best administrative app but no one uses it because it looks horrible.

Why learn Flask/FastAPI? o Is it worth learning Flask/FastAPI? What skills should I have?

Flask is a multipurpose framework and also FastAPI (although as we mentioned more specific purpose to create APIs) if you want to learn Python web, I always consider that it is necessary to learn at least two web frameworks to have a broader vision of how they work and to be able to have a critically about what the advantages and disadvantages of each framework are and select the best one according to the situation, although this sounds like a lot of work, in the end, web frameworks have more similarities than differences when it comes to their organization, which is what more important to understand them.

And yes, a resounding yes it is worth learning Flask or FastAPI, firstly, I consider Flask better as it is general purpose and FastAPI could be taken to learn after Flask/Django.

Flask is a framework with many features that allow you, like Django, to create any application and you can master it in a few weeks.

You want a functional project in a few weeks, USA DJANGO, having so many functionalities, you will be able to create your dream project in no time.

If you are learning to create web applications with Python, then Flask will be easier to handle than Django.

The bad thing about Flask/FastAPI

Like everything in life, there are little things that can get complicated, although it is not a problem of the framework itself, it is a problem of the paradigm it uses, which is that of a microframework and Flask is a very moldable framework, that is, we can doing the same thing in several ways which is good and bad at the same time, good because we are not limited but bad when starting because we can quickly follow bad practices in development with Flask which is not good when starting; At once I take advantage and tell you that I have a lot of free and paid material to develop in Flask in which I teach you Flask from scratch, from its basics to having a good level in it.

You can also apply this previous comment to FastAPI, which in summary, would be the second framework that I would recommend you learn or go directly to Django.

Another negative aspect of Flask is that, compared to Django, it feels a bit abandoned by the developers, they do not make constant updates and above all, many of the extensions are outdated.

A negative aspect of FastAPI if we compare it with Flask, is that FastAPI, although you can create more than simple APIs, for example, a Rest API, to configure a traditional app with a template manager like Jinja and SQLAlchemy, requires greater configurations if we compare it. with Flask, which, for example, when installing Flask already comes with the Jinja template engine ready to use.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.