Session 3 Slides
Session 3 · 75 minutes · slides

Dynamic Websites, APIs, and Databases

Inside the back end: server code, APIs and JSON, the relational model, and SQL by example

  1. 3.1 Inside the back end: a Python web server
  2. 3.2 APIs and JSON
  3. 3.3 Why every product needs a database
  4. 3.4 SQL by example
  5. 3.5 Beyond tables: NoSQL and ORMs
  6. 3.6 The three-tier architecture

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)
Session 3 · 3.1 Inside the back end: a Python web server · 2/2Book ↗

Localhost, ports, and templates

Session 3 · 3.2 APIs and JSON · 1/4Book ↗

The menu is the API

Session 3 · 3.2 APIs and JSON · 2/4Book ↗

JSON: the common tongue of the web

{
  "products": [
    {"id": 1, "name": "Sourdough loaf", "price": 7.5, "in_stock": true},
    {"id": 2, "name": "Rye bread", "price": 6.0, "in_stock": false}
  ]
}
Session 3 · 3.2 APIs and JSON · 3/4Book ↗

Verbs: nouns in the URL, verbs in the method

MethodMeaningBakery exampleCRUD
GETReadGET /api/productsRead
POSTCreatePOST /api/ordersCreate
PUT / PATCHUpdatePATCH /api/orders/88Update
DELETERemoveDELETE /api/orders/88Delete
This style is REST. Most business software is CRUD screens over a database.
Session 3 · 3.2 APIs and JSON · 4/4Book ↗

APIs in both directions

Web page JavaScript in the browser Mobile app iPhone or Android Partner system e.g. a delivery marketplace Your back end GET /api/products POST /api/orders GET /api/orders/88 Answers in JSON Stripe API charge a card Claude API generate, summarise Email API send confirmation Your clients call you… …and you call other companies' APIs
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.
Session 3 · 3.3 Why every product needs a database · 1/2Book ↗

Why not just save to a file?

Session 3 · 3.3 Why every product needs a database · 2/2Book ↗

The relational model

customers customer_id (PK) name email city orders order_id (PK) customer_id (FK) product_id (FK) quantity ordered_at status products product_id (PK) name price refers to refers to PK = primary key FK = foreign key. One customer can have many orders; each order has exactly one customer.
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.
The table design (the schema) is really a design of the business.
Session 3 · 3.4 SQL by example · 1/4Book ↗

Reading: SELECT

SELECT name, email FROM customers WHERE city = 'Boston';

SELECT status, COUNT(*) AS how_many
FROM orders
GROUP BY status;
Session 3 · 3.4 SQL by example · 2/4Book ↗

Combining tables: JOIN

SELECT customers.name, products.name, orders.quantity
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
JOIN products  ON orders.product_id  = products.product_id
WHERE orders.status = 'placed';
Session 3 · 3.4 SQL by example · 3/4Book ↗

Writing: INSERT, UPDATE, DELETE

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;
Session 3 · 3.4 SQL by example · 4/4Book ↗

Which database?

ProductTypeTypical use
SQLiteRelational, single filePrototypes, phone apps; built into Python
PostgreSQLRelational serverDefault for new web products
MySQLRelational serverOlder web products, WordPress
SQL Server, OracleRelational serverLarge enterprises, finance
MongoDBDocument (NoSQL)Flexible data shapes
RedisKey-value, in memoryCaching, sessions
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

Session 3 · 3.6 The three-tier architecture · 1/3Book ↗

Presentation, application, data

Session 3 · 3.6 The three-tier architecture · 2/3Book ↗

One order through three tiers

Presentation tier Browser: HTML, CSS, JavaScript Application tier Back end: Python + framework Data tier Database: PostgreSQL 1 POST /api/orders {product: 3, qty: 2} 2. Business logic: is Dana logged in? in stock? price? 3 INSERT INTO orders … ; UPDATE stock … 4 Saved. New order_id = 90 5. Call Stripe API to charge; call email API to confirm (third-party APIs, off to the side) 6 201 Created {order_id: 90} 7. Show "Order #90 placed"
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

Anything in the browser can be altered by the user. Rules and secrets live on the server.
1 / 1Dynamic Websites, APIs, and Databases