-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (49 loc) · 1.68 KB
/
app.py
File metadata and controls
57 lines (49 loc) · 1.68 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
from flask import Flask, render_template, request, redirect, url_for
import json
import os
app = Flask(__name__)
DATA_FILE = "data.json"
def load_goals():
if not os.path.exists(DATA_FILE):
return []
with open(DATA_FILE, "r") as f:
return json.load(f)
def save_goals(goals):
with open(DATA_FILE, "w") as f:
json.dump(goals, f, indent=2)
@app.route("/")
def index():
goals = load_goals()
return render_template("index.html", goals=goals)
@app.route("/add", methods=["POST"])
def add_goal():
goals = load_goals()
name = request.form["name"]
target = float(request.form["target"])
goals.append({"name": name, "target": target, "saved": 0, "starter":0})
save_goals(goals) # Save updated goals
return redirect(url_for("index")) # Redirect to the main page after adding a goal
@app.route("/deposit", methods=["POST"])
def deposit():
goals = load_goals()
name = request.form["name"]
amount = float(request.form["amount"])
for goal in goals:
if goal["name"] == name:
goal["saved"] += amount
break
save_goals(goals) # Save updated goals after deposit
return redirect(url_for("index")) # Redirect to the main page after depositing
@app.route("/starter", methods=["POST"])
def starter():
goals = load_goals()
name = request.form["name"]
amount = float(request.form["amount"])
for goal in goals:
if goal["name"] == name:
goal["starter"] = amount
break
save_goals(goals) # Save updated goals after adding starter
return redirect(url_for("index")) # Redirect to the main page after adding starter
if __name__ == "__main__":
app.run(debug=True)