Chapter 17: Flask Fundamentals

1. Introduction to Flask

Your Music Time Machine project from Chapter 16 needs a face: a small web page someone can open in a browser. This chapter introduces Flask, the Python web framework you will use to build that page. By the end, you will have a small Flask app that reads from your existing database and renders one genuine page in the browser.

Flask logo: a white flask on a red background

One thing to hold onto from the start: Flask is not only for this project's dashboard. The same routes, templates, forms, sessions, and responses you learn here are reused across the rest of the book: the dashboard build in Chapter 18, the route tests in Chapter 19, deployment in Chapter 20, the webhook receivers in Chapter 22, and the database-backed app in Chapter 25. That is why this chapter teaches Flask as a foundation in its own right, rather than as a thin wrapper around one dashboard.

Introducing Flask

Flask is a lightweight Python web framework. A framework gives you the structure and tools for building a web application, so you do not have to handle every low-level detail yourself. Flask is popular because it is fast to set up, easy to understand, and does not force a large project structure on you before you need one.

Compared with Django, which includes many built-in features such as an admin panel, authentication, and a full database layer, Flask starts smaller. That makes it a good choice when you want to learn how web applications work piece by piece: routes, view functions, templates, static files, forms, sessions, and responses.

Flask is the bridge between your Python code and the web. It lets you take Python logic and place it behind browser routes such as /, /dashboard, or /playlists. When someone visits one of those URLs, Flask receives the request, runs the matching Python function, and returns a response.

Preview of how Flask works

You use Flask by importing it into your Python file and creating a Flask application object, such as app = Flask(__name__). That object gives you the tools to define routes, return HTML pages, process form data, redirect users, manage sessions, and send responses that browsers can understand.

One of those tools is @app.route. It connects a URL to a Python function.

app.py
from flask import Flask

app = Flask(__name__)


@app.route("/")
def home():
    return "Hello from the Music Time Machine!"

Here, @app.route("/") connects the home-page URL to the home() function. When the browser requests /, Flask calls home() and sends the returned string back to the browser.

Later in the chapter, your route functions will return full HTML pages with render_template(), move users to another page with redirect(), and remember small pieces of user state with sessions. The example above is deliberately small, but it contains the basic pattern every Flask page builds on.

What you can build with Flask

Flask can be used for small web pages, dashboards, JSON APIs, internal tools, and larger applications that grow over time. It starts small, but it does not trap you in a small project.

Build RESTful APIs. Flask can power backends and microservices that return JSON data to frontend frameworks such as React or Vue, mobile apps, or other services.

Create full-stack web applications. Flask includes Jinja2, a template engine that lets your Python code render dynamic HTML pages. That means you can build complete user-facing websites with routes, templates, forms, and server-side data.

Grow into a larger structure. A Flask project can begin in a single Python file. As the application grows, you can use Blueprints to split routes into smaller, focused modules.

Choose your own tools. Flask does not lock you into a specific database, ORM, authentication system, or frontend approach. You can plug in tools such as SQLAlchemy, PostgreSQL, MongoDB, Redis, or an authentication library when your project needs them.

In this chapter, you will build a small Flask app that serves pages, handles a form, and manages a session. This is the foundation for the dashboard you build in Chapter 18, but the skills you learn here are reusable across any web project you want to build with Python.

From Python script to Flask app

Moving from a terminal script to a Flask app changes how the program runs, how the user interacts with it, and how your Python functions return results. The Python language is the same, but the shape of the program changes.

1. The program keeps running

A terminal program usually has a simple lifecycle: you run a Python file, the code executes, the result appears in the console, and the program ends. A Flask application works differently. When you start the app, it keeps running and waits for browser requests. Each request runs one route function, sends one response back, and then the app waits for the next request.

The process continues until the application stops because of an error or because you manually stop it in the terminal, usually with Ctrl + C.

2. The browser becomes the user interface

In a standard Python program, the terminal is often the interface. You run a script, and it prints output to the console. In a Flask app, the browser becomes the interface. You still start the app from the terminal, but the user interacts with it by opening a browser and visiting a URL.

By default, a local Flask app listens at http://127.0.0.1:5000. When you open that address, the browser sends a request to the Flask app. Flask handles the request and sends back a response that the browser can display.

3. URLs become entry points

When you run a standard terminal script, your code always starts at the exact same place: the top of the file. A Flask application doesn't have a single starting line. Instead, it waits for a browser to request a specific URL, and that URL decides which function runs.

This means every URL you define acts as an entry point into your application. If a user visits /home, Flask runs the function mapped to the home page. If they visit /profile, it runs a different function.

That is why routes are such a fundamental building block in Flask. Each route establishes a direct, isolated cycle: one URL, one matching function, one response.

4. Functions return responses

In standard Python, a function returns a value to whatever other part of your code called it. In Flask, your functions have a different target: they return data directly to the user's browser.

When a route function finishes running, Flask takes whatever value you returned and packages it into a formal HTTP response. This behind-the-scenes package includes the raw content, a status code (like 200 OK for success or 404 Not Found), and metadata headers that tell the browser exactly how to handle the data.

Because the browser is receiving a full web response, what your function returns dictates what the user sees next:

  • If the function returns HTML, the browser renders a visual web page.
  • If it returns JSON, a frontend script or mobile app can read it as structured data.
  • If it returns a redirect, the browser is told to request a different URL.

Where Chapter 16 fits

Your Chapter 16 work still matters. The snapshot scripts continue writing to music_time_machine.db. The Flask app you build in this chapter reads from that same database and presents the data in the browser. The database becomes the boundary between the background data-collection scripts and the web interface.

That separation is important. The scripts collect and store the data. Flask gives that stored data a web interface. You are not throwing away the command-line project from Chapter 16; you are adding a browser-facing layer on top of it.