📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Я создал одинаковое приложение на 3 веб-фреймворках | Какой оказался лучше?

ZProger [ IT ]12:14

Transcription

The best Python web framework is often considered to be Django. It is a web framework for more advanced users, providing a vast amount of functionality and ready-made features. In contrast, Flask is more minimalist, allowing you to deploy a lightweight web application without dragging along a ton of unnecessary features like an admin panel, and so on.

Additionally, there's FastAPI, which has only been around for a few years but has already gained immense popularity due to its extremely simple documentation and development approach. You can write web applications very quickly and utilize ready-made functionality.

If we compare these frameworks on GitHub, we see the following: Flask has gathered 64,000 stars, Django has 72,000, and FastAPI has 61,000. The number of stars gives us an idea of the project's popularity and which framework is used most often. However, FastAPI is relatively young, so this criterion may not reflect the real situation.

When comparing their speed, we find that FastAPI is initially the fastest, followed by Flask, and then Django. In a second test, we see the same pattern: starting with FastAPI, then Flask, and finally Django. Moreover, there are also HTTP tests where FastAPI significantly outperforms both Django and Flask.

But can we say that we should only choose FastAPI? The choice of framework always depends on the specific task. Therefore, we need to compare them in a way that highlights all their strengths and weaknesses.

This is where it gets tricky because all three projects are excellent, making it very difficult for a newcomer to choose. That's why I created the same application using these frameworks, and we will see how it behaves in each case. You will understand which framework suits you best and why you should choose it.

You will learn the working principles of each framework individually and see all the nuances you need to know. As we go along, you will understand why one framework may be better suited for a particular task.

Let's start with Flask, as it is probably the simplest framework in this video. First, let me answer the question: what can you write with Flask? If we go to the main page, we find a wealth of documentation, static file support, routing, sessions, templates, and tutorials for each component, all very well explained.

To install it, we run this command. Before that, we also created a virtual environment and activated it. To create a basic application, the most minimal setup, we execute this code.

But still, let me answer the question: why do we need Flask? Let's assume we have an application where we just need to display "Hello, World." We can use either Flask or FastAPI because Django requires many components and also provides an admin panel. Why do we need an admin panel for just returning "Hello, World"?

However, if you store "Hello, World" in a separate database, make a request, and return it through a route, let me know in the comments. Personally, I can use Flask when I need to create a small, simple application. It requires much less code than in a Django project.

So, let's try to create the application that I will be working on throughout the video. First, I create the project. Next, we need the following code: just import Flask and then import the function we will implement. For now, I will comment out the index HTML.

First, we need to install Flask. If you need pip, you run this command. I use Poetry, so I run a different command to get the Poetry log. Then we need to create a folder for our templates.

Next, we add index HTML to the templates folder. All templates will be in the templates folder. Right now, I can run our software and already get a result by adding this line to start our application.

I made the code a bit larger in case you can't see it. Then we run the application and click here. Since there is nothing in index HTML, we get this result; only the title has changed.

Now I add an H1 tag, refresh the page, and get this result. But we need a different index HTML. The final HTML looks like this. By the way, I generated this with ChatGPT because I was too lazy to write index HTML.

Here we have regular Bootstrap, then text for entering an IP address, and an input field where we enter the IP address. Then there's a button that triggers the "Send IP" function. We need to get the IP from this column.

Next, we need to make a POST request to this address. This is an internal page we will add later. Then we display the content on the screen, so we get the IP. Here we will have processing and then output in the result block.

Again, if something is unclear, we have examples to understand how frameworks work. You just need to grasp the principle of how everything is designed.

We have a POST request that we need to make. I close index HTML and add a new request here. The new request looks like this: first, it's a POST method. Then we need to get the address, process it, and return it.

For this, we use this function. Again, we create a module called handlers and insert the following code. We send a request to this address. If we go to the site, we get this window. We need all this data, so we open the network tab and investigate everything.

We filter by requests, click here, and get the request. I took this line, then put it here for the IP. I will also take the headers from the request headers. In the end, we get this result: we just send a request to the URL, add the headers, and get the data.

If we go to the response, we get the same JSON. Next, I set up a request so that we have this library. But for it to work, we need to get this IP. I forgot about that, so I add the following lines to get the content from our POST request and extract the IP from it.

It sends the data in this format: a regular key for the IP that we get here. Then I click "Send" and get the same result.

Now, let's move on to FastAPI. Why do many people love this framework? Let me explain. Let's go here; we have all the languages first. Then we look at the documentation. Again, there are examples, and we can go to the docs and see this picture, understanding what parameters need to be sent and what parameters are supported.

We can even click here and send a request directly from the interface. We will also do this in this video. Personally, I like this documentation much more than Flask's. We can go here, see how to create the first application, then understand the parameters, and then look at headers, keys, and so on.

This is a very powerful project, and the documentation is excellent. Anyone can take the component they need and simply add it to their project through the documentation. Moreover, there is also asynchronous support; we can not add async, and it will work in synchronous mode.

There is also plenty of information on deploying applications. We just go in, and for each case, we have a separate tab. You can also compare different technologies and understand which one to use in your case.

If you have more experience with FastAPI and know additional features, feel free to share in the comments. But again, we need to redo our application. I just changed the name of our project.

Next, we need to install all dependencies. I run this command, which takes the Poetry log and installs all dependencies with the same versions as in the previous application.

Again, if you are using pip, you need to run these two commands. Everything we need for this application to work on FastAPI is to change index HTML and change the main.py.

Here we have regular imports, then import our module, create the application, and templates. What are templates? Let's go to index HTML. This is the template. The result is a regular page rendered without a template.

Here we have nothing, and the page needs to display the final result. In the end, we get this, and then we pass everything to the template, which is supplied here.

Finally, one more question: how do we get the IP address? First, we request it directly in the arguments. In the index, last time we sent JSON; this time we have the key and value directly.

In the end, this value goes into the argument, and we can use it. Now that everything is ready, we can run the application. We use this command, addressing uvicorn, giving the main app, and also using reload. This command is in the documentation.

Let's go to the docs to play around with it. In the end, we have one GET request and one POST request, which is our index HTML. Let's click here and send the request. We get this result: this is the request call, and here is our response.

Next, we do the same for the POST request. I enter the IP, click execute, and get the HTML page. In this case, the div with the result is already filled.

Now, what if we create the same application using Django? I won't even explain what Django allows us to do because it's easier to list what it can't do than what it can. You can probably do absolutely everything with Django, but is it worth using it for this application? You will understand right now.

First, we install the library. Then we need to create the project. Again, this should have been done differently, so I will move the example here and run our application to see if it works.

In the end, everything works. Now let's see how it differs from FastAPI and Flask. Initially, we already have ready-made files, the same settings, then URLs, and we need to create a view and manage.py. We also have a separate database, all to create "Hello, World."

First, I create a folder with the name of our application, and then inside templates, I create a folder with the same name. Again, the names must match, and we place index HTML inside this folder.

Here, I am already using a template, not a result, but the IP address directly. I will show the HTML a bit later because there is a catch. Next, for it to work, we need to specify the path and handler.

First, we need to create a view. Then we set the following code: if it's a POST request, we take the IP address, process it, and display it on our page. I also note that here we specify the HTML path, even though we have IP App, then templates, then again IP App, and only then HTML.

So we specify it like this and also add data for our template. If it's a GET request, we just return a regular page. This time, I create a package called handlers, inside which we have a module called info and then the same function.

We have a view, meaning the user goes to a specific page, and this function will execute. However, we have a problem because we haven't specified the path to this function. This is all specified in the URL.

In the end, we import from views because we will use this function. The first path is the root, meaning a direct transition to the page, and the second request is through the IP, which works from index HTML when the user clicks the send button.

But in the end, this won't work because we also need to install the application. I go here, and we need this list, specifying the name of our application. In the end, everything should work, but not so fast.

If we go to index HTML, we see differences. The first difference is the CSRF token. If we don't add this, we will get an error. Then we have a different template, but we've seen that already, and also sending requests through JSON, just like in Flask.

But how does the token look? If we look at the requests, the launch goes through this command. I entered some gibberish, then switched to the sniffer, and everything goes here. This is the request.

So we have this token and also the IP that we enter in the field, considering that it creates this token itself. We don't even need to know how it works.

In the end, I have a question: if you had to write this same application, which framework would you choose? I would add that this example perfectly shows how to choose the right tool. In this case, using Django is completely inappropriate, as it drags along a huge amount of functionality that we simply don't need.

But if I had to choose between Flask and FastAPI, I would probably settle on FastAPI because, first, it's faster, and second, it's simply more convenient for me to work with. Again, I look forward to your answers in the comments and would appreciate a like, as this video took quite a bit of time.

If you want to create modern web applications, you can visit the channel and check out recent videos on this topic. Also, check the description for a lot of useful material.