Flask is one of the most used Python Frameworks in Web Development. It recently came out with a new release -
In this post, we'll go through the major changes. If you want the full change log, click here.
Let's go over the major changes introduced in Flask
The most breaking change is the dropped support for Python
3.5. Well, it's about time we move from those versions anyway, since Python
2 is really old!
2 support is required for the next change:
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
2.0.0 is now fully typed, so no more awkward moments when you import
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
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
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
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
tomlpackage for this!
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)
Here's a list of smaller changes that may affect your projects:
send_fileparameters have been renamed, the old names are deprecated.
attachment_filenameis renamed to
cache_timeoutis renamed to
add_etagsis renamed to
- When loading a
.flaskenvfile, the current working directory is no longer changed to the location of the file.
helpers.total_seconds()is deprecated. Use
And my favorite feature of this list:
flask shellsets up tab and history completion like the default python shell if
Flask dependency in
pyproject.toml to this:
pip3 install -r requirements.txt
You can no longer use
python2, so beware of that!
This is of course not the full change log. If you want the full change log, click here.