How Websites Use Python
The browser asks, your Python app answers
In this lesson
Python powers websites by running on a server: when you visit a page, your browser sends a request, a Python program runs and builds a response, and that response comes back as the page you see. Before any Flask code, this lesson gives you a clear mental model of requests, responses, routes, and localhost — the foundation the rest of the unit builds on.
Explain it like I’m 5
Your browser asks a question (“show me /about”) and your Python app answers with a page. The asking happens on your computer; the answering happens on the server.
The browser asks, the server answers
The web runs on a simple back-and-forth. The browser (the client) sends an HTTP request for a particular path, like /about. A program on the server receives it, decides what to do, and sends back an HTTP response — usually an HTML page. Crucially, your Python code runs on the server, not inside the visitor’s browser.
def home_page(name):
return f"<h1>Hello, {name}!</h1>"
# The server builds the response text, then sends it to the browser:
print(home_page("Ada"))
<h1>Hello, Ada!</h1>
Static vs dynamic pages
A static page is a file that’s sent exactly as written — the same for everyone. A dynamic page is built by code each time it’s requested, so it can change per visitor or per moment (your name, today’s notes, a search result). Python’s job on the web is to generate dynamic responses.
A route is just a function for a path
A web framework like Flask handles the messy HTTP details and lets you focus on one thing: which function answers which path. You can picture that mapping as a dictionary from a path to the function that handles it. localhost (address 127.0.0.1) just means “this same computer” — a server only you can reach while developing.
def home():
return "Welcome home"
def about():
return "About us"
routes = {"/": home, "/about": about}
# A browser request for "/about" runs the matching function:
print(routes["/about"]())
About us
Add a handler for /contact and look it up to answer a request for that path.
def home():
return "Welcome home"
def contact():
# TODO: return a short paragraph string
return ""
routes = {"/": home}
# TODO: add "/contact" -> contact to routes
path = "/contact"
print(routes[path]())
Map the path to the function itself (no parentheses): routes["/contact"] = contact, then call the looked-up function with routes[path]().
def home():
return "Welcome home"
def contact():
return "Email us at [email protected]"
routes = {"/": home}
routes["/contact"] = contact
path = "/contact"
print(routes[path]())
Email us at [email protected]
Common mistake: Thinking Python runs inside the visitor’s browser
Browsers do run JavaScript, so it’s natural to assume Python runs there too.
Remember the split: your Python runs on the server and produces HTML; the browser only receives and displays that HTML. (Client-side scripting is JavaScript’s job.)
Common mistake: Confusing a local server with a public website
Once your app runs at localhost it feels live, so it’s easy to think the world can see it.
localhost / 127.0.0.1 only reaches your own machine. Making a site public is a separate deployment step covered later — fundamentals first.
Common mistake: Trying to open a template file directly in the browser
An HTML file looks openable, so double-clicking it seems reasonable.
Dynamic pages must be served by the running app (e.g. visit http://127.0.0.1:5000/), so Python can fill in the variables. Opening the raw file skips that step.
Where does a Flask app’s Python code run?
Your Python runs on the server and generates the response; the browser only displays it.
What is an HTTP response?
The browser sends a request; the server’s answer — usually an HTML page — is the response.
What does localhost (127.0.0.1) mean?
localhost points back at your own machine, which is how you test a site before deploying it.
Mini exercise (medium)
Model a tiny router: write handle(routes, path) that calls the function mapped to path and returns its result, or returns "404 Not Found" when the path isn’t in routes.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
def handle(routes, path):
# TODO: if path is in routes, call its function and return the result
# TODO: otherwise return "404 Not Found"
return ""
def home():
return "Home"
routes = {"/": home}
print(handle(routes, "/"))
print(handle(routes, "/missing"))
Check if path in routes: then return routes[path](); otherwise return "404 Not Found".
def handle(routes, path):
if path in routes:
return routes[path]()
return "404 Not Found"
def home():
return "Home"
routes = {"/": home}
print(handle(routes, "/"))
print(handle(routes, "/missing"))
Home
404 Not Found
def _h():
return "hi"
assert handle({"/x": _h}, "/x") == "hi", "a known path should call its function and return the result"
assert handle({"/x": _h}, "/y") == "404 Not Found", "an unknown path should return '404 Not Found'"
assert handle({}, "/") == "404 Not Found", "empty routes means every path is a 404"
print("✓ Router works!")