DEV Community

Cover image for What's New In Flask 3.0
Khalil Habib Shariff
Khalil Habib Shariff

Posted on • Updated on

What's New In Flask 3.0

Flask 3.0: Now with more features and customization!

Flask is a popular Python web framework that is known for its simplicity and flexibility. Flask 3.0 is a major release that includes a number of new features and improvements.

Here is a summary of the most important new features in Flask 3.0:

  • New customization points for many previously global behaviors. This makes it easier to tailor Flask to your specific needs. For example, you can now customize the way that Flask handles errors, redirects, and JSON serialization.
  • Added the View.init_every_request class attribute. If a view subclass sets this to False, the view will not create a new instance on every request. This can be useful for performance-critical views. -** Teardown functions are always run at the end of the request, even if the context is preserved**. They are also run after the preserved context is popped. This ensures that cleanup code is always executed, even if an exception is raised.
  • The CLI does not pass script_info to app factory functions. This avoids a potential conflict between the script_info and the URL_PREFIX configuration option.
  • config.from_json is replaced by config.from_file(name, load=json.load). The new API is more flexible and allows you to use other JSON libraries besides the standard library's json module.
  • json functions no longer take an encoding parameter. JSON encoding is now handled internally by Flask, so you no longer need to specify the encoding explicitly.
  • safe_join is removed. Use werkzeug.utils.safe_join instead. This ensures that the safe_join function is always available, even if you are not using Flask's default dependency injection container.
  • total_seconds is removed. Use timedelta.total_seconds instead. This change reflects the fact that timedelta is the preferred way to represent time intervals in Python.
  • The same blueprint cannot be registered with the same name. Use name= when registering to specify a unique name. This prevents conflicts between blueprints with the same name.

In addition to these new features, Flask 3.0 also includes a number of bug fixes and performance improvements.

Here are some examples of how to use the new features in Flask 3.0:

Customizing global behaviors
To customize a global behavior, you can now use the app.customizer dictionary. This dictionary maps behavior names to functions that can be used to customize the behavior.

For example, to customize the way that Flask handles errors, you could use the following code:

from flask import Flask, render_template

app = Flask(__name__)

app.customizer["errorhandler"][404] = lambda e: render_template("404.html")

if __name__ == "__main__":
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

This code tells Flask to use the 404.html template to render a 404 error page.

To use the View.init_every_request class attribute, you can simply set it to False in your view subclass. For example:

from flask import Flask, render_template

app = Flask(__name__)

class MyView(flask.View):
    init_every_request = False

    def dispatch_request(self):
        return render_template("my_view.html")

app.add_url_rule("/my_view", view_func=MyView.as_view("my_view"))

if __name__ == "__main__":
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

This code tells Flask to only create a new instance of the MyView class on the first request. On subsequent requests, Flask will reuse the existing instance.

Overall, Flask 3.0 is a significant release with a number of new features and improvements. If you are using Flask, I encourage you to upgrade to version 3.0 as soon as possible.

Top comments (0)