Your First Flask App

Routes, view functions, and the dev server

Intermediate 11 min

In this lesson

Now you’ll meet the real thing. A Flask app is a short Python file that creates an app and connects functions to URLs using routes (with a decorator), then runs a small development server. This lesson walks through that file and the few commands to run it — then you’ll write your own route and view function.

Explain it like I’m 5

A Flask route is a doorbell: when someone visits that URL, Python runs the function wired to it and sends back whatever that function returns.

A minimal app.py

Three pieces make a Flask app: import Flask, create app = Flask(__name__), and attach functions to paths with the @app.route(...) decorator. A function attached to a route is called a view function; whatever it returns becomes the response.

Example
# app.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, world!"

@app.route("/about")
def about():
    return "<h1>About this site</h1>"
Two routes: / returns plain text, /about returns HTML.
Example
pip install flask
flask --app app run --debug   # then visit http://127.0.0.1:5000/
Install Flask, then run the development server with debug on.

View functions just return the response

Strip away the decorators and a view is an ordinary function returning a string. That’s why it’s easy to reason about: the return value is the page.

Example
def home():
    return "Hello, world!"

def about():
    return "<h1>About this site</h1>"

print(home())
print(about())
Output
Hello, world!
<h1>About this site</h1>
Without the @app.route wrapper, a view is just a function.

Add your own route

Adding a page is just adding a view function (and, in a real app, an @app.route above it).

Write the view function for a /contact route — it should return a short HTML paragraph. (In a real app you'd put @app.route("/contact") above it.)

# In a real Flask app this sits under @app.route("/contact")
def contact():
    # TODO: return a short paragraph wrapped in <p> ... </p>
    return ""

print(contact())

Common mistake: Naming the file flask.py

Why it happens:

It seems tidy to name your Flask file flask.py.

How to fix it:

That shadows the installed flask package, so from flask import Flask imports your own empty file and fails. Name it something else, like app.py.

Common mistake: Forgetting to activate the virtual environment

Why it happens:

You install Flask once and assume it’s always available.

How to fix it:

Activate the project’s venv before running, or Python won’t find Flask. A ModuleNotFoundError: flask usually means the environment isn’t active.

Common mistake: Expecting changes to appear without a refresh or restart

Why it happens:

You edit a view and the old page is still showing.

How to fix it:

Refresh the browser; with --debug the server auto-reloads on save. Without debug mode you must restart the server to pick up changes.

Which decorator connects a URL path to a view function?

Why should you not name the file flask.py?

What URL do you visit to see the local development server by default?

Mini exercise (medium)

A view function returns what the visitor sees. Write greet(name) that returns an HTML heading like <h1>Hello, Ada!</h1>, and home() that returns greet("world").

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

def greet(name):
    # TODO: return an <h1> heading like "<h1>Hello, Ada!</h1>"
    return ""

def home():
    # TODO: return greet("world")
    return ""

print(home())
print(greet("Ada"))

What to learn next

You wrote your first Flask app: Flask(__name__), the @app.route decorator wiring URLs to view functions, returning text or HTML, and running the dev server with --debug. You saw that a view is just a function whose return value is the response, and why the file mustn’t be named flask.py.

Returning HTML as Python strings gets messy fast. Flask Templates and HTML Pages moves your markup into template files that Python fills in with variables. When you’re ready, continue to the next lesson.