Flask 2.0 !!!

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.

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

COPY

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

updated

COPY

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.

COPY

import tomlfrom flask import Flaskapp = 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:

COPY

from flask import Flaskapp = 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:

COPY

from flask import Flaskapp = 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 ?

COPY

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.

--

--

--

Software Developer with expertise in design and building RestAPI’s and micro-services for product. Enjoy reading technical blogs and solving coding problems.

Recommended from Medium

How to Write Better Tickets

Hexagon DSP — CPU offload

What is Time Complexity in Computer Science?

How I made a Coffee Pour Reveal Animation

I hope this looks as good as it felt on completion.

How to automatically resize virtual box disk with vagrant

An Aggressive Enemy that Rams the Player

For beginners: Download Images from the IOTA Tangle

Define Headless WordPress

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Saini

Abhishek Saini

Software Developer with expertise in design and building RestAPI’s and micro-services for product. Enjoy reading technical blogs and solving coding problems.

More from Medium

What is Docker ??

Starting with the basics.

Getting started with Flask

Write Shorter Conditionals (Using Dictionaries)