Get started with Flask 2.0

One reason Python is a prime choice for web development is the breadth of web frameworks available in the language. Among the most popular and useful is Flask, which lets you start simple (“one drop at a time”) but grows with your application to add just about all of the functionality you need.

In this article we’ll walk through setting up and using Flask 2.0 for basic web apps. We’ll also touch on using Jinja2 for templating, and dealing with common issues like changing response types and handling redirects.

Setting up Flask

Flask 2.0 is easy to set up. Use pip install flask to install both Flask and all of its dependencies including the Jinja2 templating system.

As with any Python framework, it’s best to create a project using Flask inside a Python virtual environment. This isolates your project from your main Python installation and from other projects that might use Flask and its dependencies (as you might find yourself maintaining different versions for different projects).

Note that if you want to install Flask with support for async, use pip install flask[async]. See the “Using async” section below for more on this.

A basic Flask app

A simple, one-route Flask app can be written in only a few lines of code. Save this in a file named

Copyright © 2021 IDG Communications, Inc.

Source link