Before jumping into Flask 2.0, let’s discuss what is Flask in general. If you are a python geek and know something about webservers, you have definitely heard about Flask or worked on it. Flask is classified as a microframework because it does not require particular tools or libraries and it has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
A few days back on 11th May 2021 pallets projects have released a major stable version of Flask that is Flask 2.0. In this blog, we will try to cover all the new changes that are introduced in the new version and also what all things are dropped from it.
Overview of Flask 2.0
To get the full list of changelog of Flask 2.0 you can follow the official link. In this blog, we are going to cover only the following changes
- Drops the support of python 2 and 3.5.
- Added route decorators for common HTTP methods. For example,
@app.post("/login")is a shortcut for
- Built-in support for asynchronous routes, error handlers, before and after request functions, and teardown callbacks
- Type hinting introduced
- Nested blueprint introduced
- Config.from_file introduced to import config from arbitrary file loaders and deprecated config.from_json
- Improved CLI messages when the app failed to load when looking up commands.
- When loading environment from .env or .flaskenv file, the current working directory will no longer be changed to the directory of .env file
- flask shell will now have tab and history completion if
1. Flask 2.0 Drops support of python 2 and 3.5
Don’t get too much excited with the news as the Flask 2.0 doesn’t support python 2 and python3.5. If your current application python version is below 3.5 then, the flask will not upgrade from 1.1.4 to 2.0.1. To crosscheck first, check your current python version using the command below
If the python version is lower than python 3.6 then upgrading the flask version will not work
pip install -U flask
You can see the flask is upgraded to 1.1.4 instead of 2.0.1
Now check again with python version greater than 3.5
2. Flask 2.0 added route decorators for common HTTP methods.
This update is only syntactic sugar. As we all know to create a minimal server using Flask, we will create a route using Flask app context
Now, with Flask 2.0 we can replace route with the method, for example,
app.delete(). It's just saving some extra words that’s it, but on a personal note, this looks quite awesome.
The only concern will be when there are multiple routes for the same functionality so rather than saving words it will increase the route lines. If you also have the same concern, please do write that in a comment.
3. Async Await introduced in Flask 2.0
Starting with Flask 2.0, you can create asynchronous route handlers using
To run async-await just install flask with extra async using the below command
pip install flask[async]
Check out the below diagram for the working of coroutines
As flask 2.0 is not an async server as it is running the coroutine function in the same process where the master flask server is running. Unfortunately, the flask has started the support for coroutines but not to the full extent. If you want to have async support in Flask to a full extent check out the library aioflask created by Miguel Grinberg
4. Type Hinting Introduced in Flask 2.0
This one is the best. Type hints were introduced in Python 3.5 and from that time it has been extensively used in the python world. But sadly type hints were not there in Flask. If you check the Zen of python
The second point is Explicit is better than ugly. Type hints will make sure that this point is getting followed. If you have Flask 1.0 and fire the below command
Now try with Flask 2.0.1 version. The output will have all the type hints in the implementation of Flask
For Complete changes of Flask 2.0 please check out the Everything about Flask 2.0 on my blogging website — Progress Story
This is the gist of mostly all of the updates that are in Flask 2.0. It’s not a giant leap for mankind but yeah it’s a small step and if you think it will make a difference in your production application then, you must have to go for it. In my personal opinion, we can do much more in the async coroutines section as other frameworks like Quart/FastAPI are way ahead of Flask right now, in terms of coroutines.
I hope, it has helped you or it will help you. If you want to discuss anything or anything related to tech, you can contact me here or on the Contact Page. If you are interested in becoming a part of the Progress story please reach out to me or check out the Create Blog page.
See you next time! Peace Out ✌️