Arrow keys or space to move, F for full screen. Every slide has a Book button back to the full text.
Session 3 · 3.1 Inside the back end: a Python web server · 1/2Book ↗
A complete dynamic web server
from flask import Flask
from datetime import datetime
app = Flask(__name__)
@app.route("/hours")
def hours():
if 7 <= datetime.now().hour < 18:
return "<p>We are <b>open</b> right now!</p>"
return "<p>We are closed. Back at 7 a.m.</p>"
app.run(port=5000)
Flask is a framework: it handles the routine parts of being a server
Each route connects a path to a function; the function's return value is the response
Session 3 · 3.1 Inside the back end: a Python web server · 2/2Book ↗
Localhost, ports, and templates
localhost means "this computer": browser and server on the same laptop
A port is a numbered door: 443 for HTTPS, 5000 or 3000 for testing
Two visitors a minute apart at 5:59 and 6:00 see different pages: that is "dynamic"
Real back ends fill in templates (HTML with blanks) rather than gluing strings
APIs in both directions. Your back end offers an API to your own front ends and to partners, and it consumes other companies' APIs for payments, AI, email, maps, and more. Being on both sides is normal.
You offer an API to your front ends and partners; you consume third-party APIs (Stripe, Claude, email) instead of building
An API key identifies your account to a third party. It lives on your server, never in front-end code
Session 3 · 3.3 Why every product needs a database · 1/2Book ↗
Why not just save to a file?
Many writers at once: two customers buying the last challah must not both succeed
Speed at scale: an index finds one order in ten million in milliseconds
Reliability: a transaction is all-or-nothing, even if the power fails halfway
Questions: "who ordered rye three times last quarter?" is one line
The database is a separate program (PostgreSQL, MySQL, SQLite) your back end talks to
Session 3 · 3.3 Why every product needs a database · 2/2Book ↗
The relational model
A schema for the bakery. Orders do not copy customer or product details; they point at them by key. Change Dana's email once and every order she ever placed reflects it.
Tables of rows and columns; each table holds one kind of thing
Primary key: the unique ID of a row. Foreign key: a column pointing at another table's row
Change Dana's email once; every order she placed reflects it
The table design (the schema) is really a design of the business.
INSERT INTO orders (customer_id, product_id, quantity, status)
VALUES (3, 1, 2, 'placed');
UPDATE orders SET status = 'ready' WHERE order_id = 88;
DELETE FROM orders WHERE order_id = 89;
DELETE FROM orders; with no WHERE deletes every order in the company
CREATE TABLE defines columns, data types, keys, and constraints the database enforces
You will not write SQL in this course. You will read it and say what it does.
Session 3 · 3.5 Beyond tables: NoSQL and ORMsBook ↗
NoSQL and ORMs
NoSQL relaxes the rules: a document database (MongoDB) stores JSON-like records with any fields
Price of flexibility: fewer guarantees, clumsier joins. Rule of thumb: start relational
An ORM lets Python treat rows as objects and writes the SQL behind the scenes
Claude Code will almost certainly use an ORM; the concepts underneath are unchanged
Session 3 · 3.6 The three-tier architecture · 1/3Book ↗
Presentation, application, data
Presentation tier: the front end in the browser or app
Application tier: the back-end program; where business logic lives (prices, permissions, workflows)
Data tier: the database
Session 3 · 3.6 The three-tier architecture · 2/3Book ↗
One order through three tiers
One order through three tiers. The browser never touches the database and never sees the payment key. Everything that must be trusted happens in the application tier, on the server.
Session 3 · 3.6 The three-tier architecture · 3/3Book ↗
Three consequences
The browser never touches the database directly; the back end checks identity and applies rules
Each tier can be changed independently: redesign the front end without touching the data
Each tier can live on different hardware: today one laptop, tomorrow ten servers (Session 4)
Anything in the browser can be altered by the user. Rules and secrets live on the server.