Everything new in Flask 2.0

Photo by Markus Spiske on Unsplash

Flask is one of the most used Python Frameworks in Web Development. It recently came out with a new release - 2.0.0.

In this post, we'll go through the major changes. If you want the full change log, click here.

Changes

Let's go over the major changes introduced in Flask 2.0.0

Dropped support for Python 2 and 3.5

The most breaking change is the dropped support for Python 2 and 3.5. Well, it's about time we move from those versions anyway, since Python 2 is really old!

Also, dropping 3.5 and 2 support is required for the next change:

Type hints

Python 3.7 added support for type annotations. This is where you can add type hints to variables to tell others what type it will be. This is a God send for people with linters and autocompletors, since you no longer have to view docs to find the return type of a certain function.

Keep in mind however, that type hints aren't enforced, meaning that this is completely valid code, as far as the python interpreter goes

a: str = 5

To check your types, use an external linter like mypy

Flask 2.0.0 is now fully typed, so no more awkward moments when you import request from flask and your IDE won't autocomplete its methods.

You can see the difference between the type hints in Flask 2.0.0 and its earlier versions with the help() function:

The above image shows Flask 1.1.4's docstring and the below image shows Flask 2.0.1's docstring. Notice the type hints

New Config.from_file() method

You may remember the Config.from_json(), or the app.config.from_json() method that you use to configure Flask with a JSON File. In Flask 2.0.0, it has been deprecated in favour of Config.from_file.

The syntax of this method is as follows:

app.config.from_file(filename: str, loads_function)

Now, if we wanted to implement the old from_json behaviour, look at the code below:

import json

app.config.from_file(filename: str, json.load)

This opens us to do the same to parse TOML files as well:

import toml

app.config.from_file(filename: str, toml.load)

You need to install the toml package for this!

New route decorators

Flask has new decorators for defining routes now. Before we used @app.route(path: str) to define a route in our app, and for methods other than GET we add the methods parameter to our decorator.

Now, Flask has followed ExpressJS's routes and added decorators for defining routes specific to HTTP Methods.

# You can now use these instead of app.route
@app.get(path)
@app.post(path)
@app.put(path)
@app.delete(path)
@app.patch(path)

Smaller changes

Here's a list of smaller changes that may affect your projects:

  • Some send_file parameters have been renamed, the old names are deprecated. attachment_filename is renamed to download_name. cache_timeout is renamed to max_age. add_etags is renamed to etag.
  • When loading a .env or .flaskenv file, the current working directory is no longer changed to the location of the file.
  • helpers.total_seconds() is deprecated. Use timedelta.total_seconds() instead.

And my favorite feature of this list:

  • flask shell sets up tab and history completion like the default python shell if readline is installed

How to upgrade

Change your Flask dependency in requirements.txt, Pipfile or pyproject.toml to this:

Flask==2.0.0

And run:

pip3 install -r requirements.txt

You can no longer use python2, so beware of that!

Conclusion

This is of course not the full change log. If you want the full change log, click here.

17