Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 72 ยท Getting Started

What Is Flask?

Learn what Flask is, how WSGI connects Python to the web, why a micro-framework is useful, how Flask compares with Django and FastAPI, and what you will build.

Flask is a Python library for building web applications and APIs. You write ordinary Python functions, tell Flask which URLs should run them, and Flask takes care of the web plumbing: reading the request, picking the right function, turning your return value into an HTTP response and sending it back.

Flask is called a micro-framework. That does not mean it is only for small projects. It means the core is small and keeps few opinions: Flask gives you routing, request and response objects, sessions, templates and a test client, and nothing more. There is no built-in database layer, no form library and no admin panel. You add those pieces when you need them, and you choose which ones. This course follows that path, one piece at a time, until you have built complete applications.

This first lesson is a read-only overview with a few small experiments. You do not need to install anything yet: the examples run right here in your browser. If Python itself is new to you, start with the Python tutorial and come back; you should be comfortable with functions, dictionaries and decorators.

How a web app talks to a browser

When you open a web page, your browser sends an HTTP request (a method such as GET, a path such as /books/7 and some headers). A server answers with an HTTP response (a status code such as 200 OK, headers and a body, usually HTML or JSON). A web application is the program that decides what that answer is.

Python programs do not listen on network ports directly in production. A dedicated web server (Gunicorn, uWSGI, Waitress and others) handles the network and calls your Python code through a standard interface called WSGI, the Web Server Gateway Interface.

flowchart LR
    B["Browser"] -->|"HTTP request"| S["Web server<br/>(Gunicorn, Waitress)"]
    S -->|"WSGI call"| F["Flask app<br/>(your code)"]
    F -->|"status, headers, body"| S
    S -->|"HTTP response"| B

The server speaks HTTP to the browser and WSGI to your application, so any WSGI server can run any WSGI framework.

WSGI in a few lines

WSGI is smaller than its name suggests. An application is any callable that takes two arguments: environ, a dictionary describing the request, and start_response, a function the app calls once to announce the status and headers. The callable then returns the body as an iterable of bytes.

You can write one by hand and call it yourself, with no server and no framework:

Python A WSGI app without a framework

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is exactly what a WSGI server does for every request, except that it fills environ from the real network connection. Writing every app this way would be tedious: you would parse paths with strip(), build header lists by hand and write your own router. A framework exists to do that repeated work for you.

The same app in Flask

A Flask application object is a WSGI callable. Here is the app above rewritten with Flask. The first line installs Flask inside the page with micropip; on your own machine you would run pip install flask once instead (the next lesson shows how).

Python Hello in Flask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the two versions. The route table replaces the manual path handling, the return statement replaces start_response, and Flask picks the status code and Content-Type for you. The test_client() used here sends requests straight into the app, in the same process, so you can see results without a server. You will use it throughout this course and in your own tests.

What Flask gives you

Flask is built on two libraries that you will meet by name:

  • Werkzeug handles the low-level WSGI work: request and response objects, URL routing, the test client and password hashing helpers.
  • Jinja is the template language that turns data into HTML pages.

On top of those, Flask adds the glue: the Flask application object, decorators for routes, the request and session objects, url_for, flash messages, error handlers, blueprints for splitting an app into parts and a command-line tool.

Python What a tiny app already knows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Routing, HTML templates and JSON all work with no extra installs. Notice that static shows up in the route table too: Flask serves a /static/ route by default.

Everything else comes from extensions and ordinary Python libraries: Flask-SQLAlchemy for databases, Flask-WTF for forms, Flask-Login for user sessions, Flask-Migrate for schema changes. Because Flask makes few decisions, you decide the structure of your project, and the same skills apply from a ten-line script to a large application.

Flask, Django and FastAPI

All three are popular Python web frameworks. They solve overlapping problems with different philosophies.

Flask Django FastAPI
Philosophy Small core, you pick the parts Batteries included API-first, type hints drive everything
Interface WSGI (sync, async views supported) WSGI and ASGI ASGI (async first)
Database layer Your choice (usually SQLAlchemy) Built-in ORM and migrations Your choice
Templates and forms Jinja, extensions for forms Built-in templates and forms Mostly JSON APIs
Admin panel Extensions Built in None
Best for Custom apps, small to mid-size services, learning how the web works Content-heavy sites and large teams that want conventions High-performance JSON APIs with automatic docs

None of them is "the best". Django saves time when your project looks like what it expects (users, an admin, a relational database). FastAPI shines for typed JSON APIs. Flask is the right tool when you want to understand each part, keep control of the structure, or build something that does not fit a larger framework's mold. Skills from Flask transfer well: routing, requests, templates, sessions and testing mean the same things everywhere. If you are curious about the others, see the Django tutorial and the FastAPI basics lesson.

What you will build

The course is organized in twelve chapters, and every chapter ends with something you can run:

  1. Getting started: your first app, how a request travels through Flask, and how a project is laid out.
  2. Routing and views: URLs, methods, responses, the request object and error pages.
  3. Templates: Jinja pages, inheritance, filters and static files.
  4. Forms and input: handling forms, validation, flash messages and uploads.
  5. State: cookies and sessions.
  6. Databases: sqlite3, SQLAlchemy, CRUD, relationships and migrations.
  7. Structure: application factories, blueprints, configuration and hooks.
  8. Authentication and security: password hashing, login systems, permissions and web security.
  9. REST APIs: JSON, design, validation and tokens.
  10. Production: testing, logging, caching, background work and deployment.
  11. Projects: a blog, a todo API, a URL shortener, a user system and a mini bookshop.
  12. Practice: exercises, interview questions and a cheat sheet.

Note: Every example in this course runs inside your browser. There is no real server, so apps are exercised with the test client, which behaves like a browser that talks directly to the app. Anything that truly needs a server, such as Gunicorn or Docker, is shown as a read-only snippet with a runnable stand-in where possible.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does WSGI define?

    WSGI is the contract that lets any compatible server (Gunicorn, Waitress and others) call any compatible application, including a Flask app.
  2. 2. Why is Flask called a micro-framework?

    Micro describes the size of the core and its lack of fixed choices, not the size of the projects you can build.
  3. 3. Which two libraries is Flask built on?

    Werkzeug supplies the WSGI tools, routing and request/response objects, and Jinja renders templates.
  4. 4. In this course, what stands in for a real web server in the examples?

    app.test_client() sends requests directly into the Flask app in the same process, so you can see responses without a server or a network port.

Summary

  • Flask is a small Python web framework: routing, request and response objects, sessions, Jinja templates and a test client, with everything else added as needed.
  • A WSGI application is a callable taking environ and start_response; a Flask app object is such a callable.
  • Flask is built on Werkzeug (WSGI tools) and Jinja (templates).
  • Django includes more by default, FastAPI focuses on typed async APIs, and Flask gives you the most freedom to assemble your own stack.
  • In this course the test client replaces a real server so every example can run in the browser.

Next: Setup and Your First App, where you install Flask and write your first routes.