How Websites Use Python

The browser asks, your Python app answers

Intermediate 9 min

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.

Example
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"))
Output
<h1>Hello, Ada!</h1>
A function builds the HTML the browser will receive.

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.

Example
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"]())
Output
About us
Mapping a path to the function that answers it.

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]())

Common mistake: Thinking Python runs inside the visitor’s browser

Why it happens:

Browsers do run JavaScript, so it’s natural to assume Python runs there too.

How to fix it:

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

Why it happens:

Once your app runs at localhost it feels live, so it’s easy to think the world can see it.

How to fix 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

Why it happens:

An HTML file looks openable, so double-clicking it seems reasonable.

How to fix it:

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?

What is an HTTP response?

What does localhost (127.0.0.1) mean?

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"))

What to learn next

You built a clear mental model of the web: the browser sends a request, your Python runs on the server, and it returns a response — with dynamic pages generated per request and localhost meaning your own machine. You modelled a framework’s job by writing a tiny handle() router.

Now build the real thing. Your First Flask App creates a minimal app.py with routes and view functions and runs the development server. When you’re ready, continue to the next lesson.