Flask Python Framework: Lightweight Web Development Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Flask - Complete Guide to the Python Web Framework

Introduction to Flask

Flask is a minimalist Python web framework designed for building web applications and REST APIs. It provides a simple and flexible structure that lets you quickly develop everything from prototypes to full-featured web apps. Flask is built on the Werkzeug WSGI toolkit and uses the Jinja2 template engine.

Created by Armin Ronacher in 2010, Flask gained popularity thanks to its "do one thing and do it well" philosophy. Unlike heavier frameworks, Flask provides only the essential core, letting developers choose the components they need for their projects.

Key Features of Flask

Minimalist and Extensible Approach

Flask follows the microframework principle, offering basic functionality while allowing you to extend it through plugins and extensions.

Built-in Routing and Request Handling

The framework provides an intuitive routing system with support for various HTTP methods and URL parameters.

Jinja2 Template Support

Integration with the powerful Jinja2 template engine makes it easy to work with HTML templates and dynamic content.

REST API Capabilities

Flask is perfect for building RESTful APIs thanks to its JSON response support and flexible HTTP method handling.

Extension Ecosystem

A rich ecosystem of extensions lets you add functionality for databases, authentication, forms, and much more.

Python 3.7+ Compatibility

Flask supports modern Python versions and follows best development practices.

Installation and Setup

Install via pip

pip install flask

Verify Installation

python -m flask --version

Create app.py and Run

export FLASK_APP=app.py
flask run

For Windows:

set FLASK_APP=app.py
flask run

Building Your First App

Simple app.py Example

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Welcome to Flask!"

if __name__ == '__main__':
    app.run(debug=True)

Run the Application

flask run

The app will be available at http://localhost:5000/

Route and URL Handling

Basic Routing

@app.route("/user/<username>")
def show_user_profile(username):
    return f"User profile: {username}"

Typed Routes

@app.route("/post/<int:post_id>")
def show_post(post_id):
    return f"Post #{post_id}"

@app.route("/user/<path:subpath>")
def show_subpath(subpath):
    return f"Subpath: {subpath}"

Supported Parameter Types

  • string - text strings (default)
  • int - integer numbers
  • float - floating-point numbers
  • path - strings with "/" characters
  • uuid - UUID strings

Working with HTTP Methods

Handling Different Methods

@app.route("/data", methods=["GET", "POST", "PUT", "DELETE"])
def handle_data():
    if request.method == "POST":
        return "Data created"
    elif request.method == "PUT":
        return "Data updated"
    elif request.method == "DELETE":
        return "Data deleted"
    return "Fetching data"

Working with Templates

Recommendations