Flask 2.0 !!!

Flask 2.0 !!!

FLASK 2.0 is the most awaited release and it brings major changes and features with it.

Flask is the most used and preferred web-framework due to its ease for creating micro-service architecture and flexibility.

In 05 May 2021, version 2.0.0 of Flask came out with 31 new changes, then 13 more changes are added in version 2.0.1 on 21 May 2021.

In this post, you'll learn about some of those changes and the refactoring you might want to do to your code to take the most advantage of this new version.

flask2.1.png

What's New

Flask 2.0 comes with a lot of changes and new features which you can check here.

Let's see what are the major changes and feature released.

Drop support for Python 2 and 3.5.

Flask has officially dropped support for Python 2 and 3.5. It's time to update the python versions to 3.6 or above if you develop on flask.

Type Hinting

explicit is always better than implicit

Type hinting is a beautiful way of explicitly telling anyone reading a class or function documentation what that object expects as a parameter and what response it will return. Now, all of Flask has typing annotations.

Lets see an example to understand

previously

def add(param1, param2):
    total = param1 + param2
    return total

updated

def add(param1: int, param2:int) -> int:
    total = param1 + param2
    return total

As you can see in above example, where we are not using the type hinting user may confuse with the param1 and param2 type , whether its a string or integer but in the latest version we have clearly stated that the function is expected two integers as parameter and will return a parameter in response.

Load config from any file

Loading config files has become easy now. Now all you have to use is a single function to load any file as long as file reader is able to read those files.

.from_file() has been introduced in Flask 2.0 where it accepts two arguments, file path and file reader.

import toml

from flask import Flask

app = Flask('__name__')
app.config.from_file('config.toml', toml.load)

In the above example I have used the toml file, any other file can also be used like yaml or JSON but you must install the library packages for reading and parsing that file. For above example I installed toml package.

Also, If you were using .from_json() in you code previously , update it to .from_file() as .from_json() has been deprecated.

New HTTP Method Specific Decorators

If you've been doing Restful APIs using Flask, you are probably accustomed to doing something like the following code snippet:

from flask import Flask

app = Flask('__name__')

@app.route('/first', methods=['GET'])
def my_get_endpoint():
    return 'This was a GET request.'

@app.route('/second', methods=['POST'])
def my_post_endpoint():
    return 'This was a POST request.'

@app.route('/third', methods=['PUT'])
def my_put_endpoint():
    return 'This was a PUT request.'

New version of Flask allows you to use the HTTP code directly when specifying the route decorator like most of the other frameworks allows.

If you want to use this, you just have to change your traditional @app.route for a @app.. Here is a refactored version of the above example:

from flask import Flask

app = Flask('__name__')

@app.get('/first')
def my_get_endpoint():
    return 'This was a GET request.'

@app.post('/second')
def my_post_endpoint():
    return 'This was a POST request.'

@app.put('/third')
def my_put_endpoint():
    return 'This was a PUT request.'

Flask 2.0 provides many new features but above shared are my favourite. Other feature and changes that I like are:

  • Set the default encoding to “UTF-8” when loading .env and .flaskenv files to allow to use non-ASCII characters.
  • register_blueprint takes a name option to change the (pre-dotted) name the blueprint is registered with. This allows the same blueprint to be registered multiple times with unique names for url_for. Registering the same blueprint with the same name multiple times is deprecated.
  • Combine URL prefixes when nesting blueprints that were created with a url_prefix value.

How to upgrade ?

pip install -U Flask==2.0.0

We can upgrade the flask version by simply using the pip command with -U flag and specify the 2.0.0 version.

Wrapping up

I personally liked the flask changes and its been around 3 years since FLASK 1.0 is released.

What were your favourite changes, and why? Are you planning on upgrading to the newest version of Flask? Leave your comments.

Did you find this article valuable?

Support Abhishek Saini by becoming a sponsor. Any amount is appreciated!