Get AI summaries of any video or article — Sign up free
Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started thumbnail

Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Flask is used to build a blog-style app with user management, password resets, profile picture uploads, and post create/update/delete features.

Briefing

Flask is set up as a practical path to building a full-featured blog-style web app—complete with user registration and login, password reset emails, profile picture uploads with automatic resizing, and create/update/delete post management—so the learning payoff is tied directly to real application needs like databases, form input, file storage, and email sending. The tutorial frames the series around that end goal, then immediately shifts to the fundamentals: installing Flask, creating a minimal app, defining routes, and running the development server in a way that supports fast iteration.

After installing Flask via pip and verifying the import works, the walkthrough creates a new project directory (named “flask blog”) and a starter file (“flaskblog.py”). Using Flask’s simplest example, it instantiates an app object and defines a root route (the “/” path) that returns “hello world.” The tutorial emphasizes how Flask uses the special double-underscore __name__ variable to locate templates and static files, and it clarifies the role of the route decorator: it maps URLs typed into the browser to Python functions that return the content for each page.

Running the app requires setting an environment variable so Flask knows which module to serve. On macOS/Linux, that’s done with export FLASK_APP=flask_blog.py (with the transcript using “flask_app” naming), while Windows uses set instead of export. Once FLASK_APP is set, the server starts with flask run and listens on 127.0.0.1:5000. The tutorial also notes that localhost is an alias for 127.0.0.1, making URLs easier to type.

When the code changes (for example, wrapping “hello world” in HTML <h1> tags), the server must restart unless debug mode is enabled. The tutorial demonstrates two approaches: (1) set FLASK_DEBUG=1 and run flask run again so changes reload automatically, and (2) alternatively run the module directly with python flaskblog.py after adding an if __name__ == '__main__' block that calls app.run(debug=True). Debug mode is presented as the key workflow improvement for development.

Finally, the tutorial expands routing beyond the home page by adding an “/about” route. It shows how missing routes produce a 404 Not Found response, then fixes it by defining the about function and decorator. It also demonstrates that multiple URL paths can map to the same function by stacking decorators—such as serving the home page at both “/” and “/home.” The segment closes with a roadmap: the next step is returning more complex HTML using templates and passing variables into those templates.

Cornell Notes

The tutorial builds a minimal Flask application and then grows it into a multi-route setup. It starts by installing Flask, creating a project folder and a “flaskblog.py” file, and defining a Flask app instance plus a root route (/) that returns content. It explains how Flask knows where to find templates/static files using the __name__ variable, and how route decorators map URLs to Python functions. For development workflow, it contrasts restarting the server after code changes with enabling debug mode (FLASK_DEBUG=1) for automatic reloads. It then adds an /about route, demonstrates 404 behavior for undefined routes, and shows how multiple routes can share the same function via stacked decorators.

What are the minimum steps to get a Flask app running locally, and how does Flask know which file to serve?

First, install Flask with pip install flask and verify it imports cleanly. Next, create a project directory and a Python module (the transcript uses “flaskblog.py”) that instantiates a Flask app object. Before starting the server, set an environment variable pointing to the module Flask should run (the transcript uses export FLASK_APP=flask_blog.py on macOS/Linux and set FLASK_APP=... on Windows). Then start the server with flask run, which serves on 127.0.0.1:5000 (localhost can be used as an alias).

How do route decorators connect browser URLs to Python functions?

Routes are defined using decorators like @app.route('/'). The decorator registers the URL path and binds it to the decorated function. When a request hits that path (e.g., “/” for the home page), Flask calls the associated function, and whatever the function returns becomes the page content. The tutorial also notes that decorators can be confusing conceptually, but they’re mainly a mapping mechanism for this series.

Why did HTML changes not appear immediately after editing the code, and how does debug mode fix that?

After changing the return value to include HTML tags (like wrapping text in <h1>), the browser still showed the old output because the running development server kept using the previously loaded code. The tutorial resolves this by stopping the server (Ctrl+C) and rerunning it. Debug mode avoids that restart cycle: setting FLASK_DEBUG=1 and running flask run again enables automatic reloads, so edits propagate after a refresh without manually restarting.

What does a 404 Not Found mean in this Flask routing context?

When navigating to a path that hasn’t been defined—such as “/about” before the route exists—Flask returns a 404 response. The transcript ties this directly to the absence of a matching route decorator/function for that URL, then fixes it by adding @app.route('/about') and returning an about page response.

How can multiple URL paths display the same page content in Flask?

Multiple routes can point to the same function by stacking decorators. The tutorial demonstrates mapping both “/” and “/home” to the same home-page function by placing two @app.route decorators above a single function and returning the same content from that function.

What’s the role of if __name__ == '__main__' and when would someone run Flask this way?

The conditional if __name__ == '__main__' is true only when the module is executed directly (e.g., python flaskblog.py). Inside it, app.run(debug=True) starts the server without needing environment variables. The tutorial contrasts this with the recommended flask run workflow that relies on environment variables, and notes that running the module directly can be convenient to avoid repeatedly setting FLASK_APP.

Review Questions

  1. When you change the Flask route’s returned HTML, what determines whether you must restart the server or whether the change appears automatically?
  2. How does Flask decide which function to call for a given URL path like “/about” or “/home”?
  3. What is the practical difference between starting the app with flask run (using FLASK_APP) versus running python flaskblog.py (using if __name__ == '__main__')?

Key Points

  1. 1

    Flask is used to build a blog-style app with user management, password resets, profile picture uploads, and post create/update/delete features.

  2. 2

    A Flask app is created by instantiating a Flask object and defining routes with @app.route decorators that map URLs to functions.

  3. 3

    Setting the FLASK_APP environment variable tells flask run which module to serve, with export on macOS/Linux and set on Windows.

  4. 4

    The development server runs locally on 127.0.0.1:5000, and localhost can replace 127.0.0.1 in URLs.

  5. 5

    Without debug mode, code edits require stopping and restarting the server for changes to appear in the browser.

  6. 6

    Enabling FLASK_DEBUG=1 allows automatic reloads so edits show up after a refresh without manual restarts.

  7. 7

    Undefined routes return 404 Not Found until a matching route decorator and function are added, and multiple routes can share one function via stacked decorators.

Highlights

Debug mode (FLASK_DEBUG=1) eliminates the restart loop by reloading changes automatically during development.
Route decorators are the core mechanism that maps URL paths like “/” and “/about” to Python functions that return page content.
A missing route produces a 404 Not Found response, which is resolved by adding the corresponding @app.route decorator.
Multiple URL paths can be handled by the same function by stacking decorators (e.g., “/” and “/home”).
Flask can be started either via flask run with FLASK_APP or by running the module directly using if __name__ == '__main__' and app.run(debug=True).

Topics

Mentioned