Flask

What It Is

Flask is a Python library that helps you turn Python code into a website. Instead of only printing text in the terminal, you can use Flask to build pages that open in a browser.

Why We Use It

Flask is a good fit for this course because it lets beginners build real web apps without too much setup. It is simple enough to learn step by step, but powerful enough to support a real Demo Day project.

With Flask, you can:

  • Show pages in a browser
  • Collect input from forms
  • Move between different pages in your app
  • Build your project one feature at a time

The Basic Idea

A Flask app usually starts with one file called app.py.

Inside that file, you:

  1. Import Flask
  2. Create the app
  3. Make routes
  4. Run the app

Example:

from flask import Flask

app = Flask(__name__)


@app.route("/")
def home():
    return "<h1>Hello!</h1><p>My app is working.</p>"


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

What The Important Parts Mean

  • from flask import Flask loads the Flask tool into your file.
  • app = Flask(__name__) creates your app.
  • @app.route("/") tells Flask which URL should open this page.
  • def home(): is the Python function Flask runs for that page.
  • return ... is what the browser shows.
  • app.run(debug=True) starts the app and reloads it when you save changes.

Think of a route like an address in your app:

  • / = homepage
  • /about = an about page
  • /stats = a stats page

How To Install Flask

Flask does not come with Python automatically, so you need to install it once per coding environment.

Open the terminal and run:

pip install flask

If that does not work, try:

pip3 install flask

When you see Successfully installed or your prompt comes back, Flask is ready.

How To Run A Flask App

  1. Save app.py
  2. Make sure app.py is the active file
  3. Click the play button in the top-right of the editor
  4. Look for output like this:
 * Serving Flask app 'app'
 * Debug mode: on
 * Running on http://127.0.0.1:5000

Then open that address in your browser.

To stop the app, click in the terminal and press Ctrl + C.

Routes And Pages

Each route is one page or action in your app.

Example:

@app.route("/")
def home():
    return "<h1>Home</h1>"


@app.route("/about")
def about():
    return "<h1>About</h1>"

If you visit /, Flask runs home(). If you visit /about, Flask runs about().

This is how you build an app one page at a time.

Forms And User Input

Flask can also read what a user types into a form.

Example:

from flask import Flask, request

app = Flask(__name__)


@app.route("/hello", methods=["POST"])
def hello():
    name = request.form.get("name", "friend")
    return f"<h1>Hello, {name}!</h1>"

Important parts:

  • request lets Flask read data from a form
  • methods=["POST"] means the route accepts form data
  • request.form.get("name") reads what the user typed into a field named name

A Good Beginner Build Order

For this course, a good way to build a Flask project is:

  1. Get one route working
  2. Add a second page
  3. Add a form
  4. Make the app respond to user input
  5. Improve the design
  6. Move repeated HTML into templates later

Do not try to build the whole app at once. Small wins are better.

Flask And Templates

At first, it is okay to return HTML strings right inside app.py. That helps you see the connection between Python and the page.

Later, you can move your HTML into a templates/ folder. This is called using templates, and it keeps your code cleaner once your app gets bigger.

In this course, the usual path is:

  • Early build weeks: routes in app.py with string-based HTML
  • Later build weeks: Jinja templates in templates/

Common Problems

ProblemWhat it usually meansFix
No module named flaskFlask is not installed in this environmentRun pip install flask or pip3 install flask
Blank page or error pageThere is a mistake in your Python or HTMLSave the file, read the terminal error, and check the line it names
"Not Found"Flask does not have that route yetCheck @app.route(...) and make sure the function is above app.run(...)
"Method Not Allowed"The form and route are using different request methodsCheck method="post" in the form and methods=["POST"] in Flask
App will not stopThe server is still runningClick in the terminal and press Ctrl + C

Quick Reference

from flask import Flask, request

app = Flask(__name__)


@app.route("/")
def home():
    return "<h1>Hello!</h1>"


if __name__ == "__main__":
    app.run(debug=True)
pip install flask