Your First Flask App
Routes, view functions, and the dev server
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
# 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>"
pip install flask
flask --app app run --debug # then visit http://127.0.0.1:5000/
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.
def home():
return "Hello, world!"
def about():
return "<h1>About this site</h1>"
print(home())
print(about())
Hello, world! <h1>About this site</h1>
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())
Return a string wrapped in <p> and </p> tags.
def contact():
return "<p>Get in touch at [email protected]</p>"
print(contact())
<p>Get in touch at [email protected]</p>
Common mistake: Naming the file flask.py
It seems tidy to name your Flask file flask.py.
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
You install Flask once and assume it’s always available.
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
You edit a view and the old page is still showing.
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?
@app.route("/path") above a function tells Flask to run it for that path.
Why should you not name the file flask.py?
A local flask.py is imported instead of the real package, so from flask import Flask fails.
What URL do you visit to see the local development server by default?
Flask’s dev server runs on localhost port 5000 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"))
greet: return f"<h1>Hello, {name}!</h1>". home: return greet("world").
def greet(name):
return f"<h1>Hello, {name}!</h1>"
def home():
return greet("world")
print(home())
print(greet("Ada"))
<h1>Hello, world!</h1>
<h1>Hello, Ada!</h1>
assert greet("Ada") == "<h1>Hello, Ada!</h1>", "greet should wrap the name in an <h1> heading"
assert home() == "<h1>Hello, world!</h1>", "home should return greet('world')"
print("✓ Views return HTML!")