Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (51 loc) Β· 1.64 KB
/
Copy pathapp.py
File metadata and controls
65 lines (51 loc) Β· 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Cosmic Pizza π β a tiny Flask app for building and pricing a pizza.
Run locally:
pip install -r requirements.txt
flask --app app run
Then open http://127.0.0.1:5000
"""
from flask import Flask, render_template, request
from pizzeria.menu import SIZES, TOPPINGS, size_names, topping_names
from pizzeria.orders import Order
from pizzeria.pricing import format_price
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
"""Render the pizza builder form."""
return render_template(
"index.html",
sizes=size_names(),
toppings=topping_names(),
receipt=None,
)
@app.route("/order", methods=["POST"])
def order():
"""Build an order from the submitted form and show a receipt."""
size = request.form.get("size", "medium")
selected = request.form.getlist("toppings")
pizza = Order(size)
for topping in selected:
if topping in TOPPINGS:
pizza.add_topping(topping)
return render_template(
"index.html",
sizes=size_names(),
toppings=topping_names(),
receipt=pizza.receipt(),
)
@app.route("/menu.json", methods=["GET"])
def menu_json():
"""Return the full menu as JSON, with prices formatted for display."""
items = {}
for name, details in TOPPINGS.items():
items[name] = {
"price": format_price(details["price"]),
"calories": details["calories"],
"category": details["category"],
}
return {
"sizes": {name: format_price(price) for name, (price, _) in SIZES.items()},
"toppings": items,
}
if __name__ == "__main__":
app.run(debug=True)