-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpensetracker.py
59 lines (47 loc) · 1.65 KB
/
expensetracker.py
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
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
recorded_purchases = []
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form.get('name')
month = request.form.get('month')
return render_template('reports.html', name=name, month=month)
return render_template('index.html')
@app.route('/record', methods=['GET', 'POST'])
def record():
if request.method == 'POST':
# Handle form submission
item = request.form.get('item')
price = request.form.get('price')
if item and price:
# Create a dictionary for the purchase
purchase = {'item': item, 'price': float(price)}
# Append the purchase to recorded_purchases list
recorded_purchases.append(purchase)
return "Purchase recorded successfully!"
else:
return "Error: Missing item or price in form data."
else:
# Render the record page
return render_template('record.html')
@app.route('/reports')
def reports():
# Retrieve user's name and selected month from the query parameters
name = request.args.get('name')
month = request.args.get('month')
# Calculate total price and format the report
total_price = sum(purchase['price'] for purchase in recorded_purchases)
report_data = {
'recorded_purchases': recorded_purchases,
'total_price': total_price,
'name': name,
'month': month
}
return render_template('reports.html', report_data=report_data)
@app.route('/viewreports')
def view_reports():
# Return recorded purchases data in JSON format
return jsonify(recorded_purchases)
if __name__ == '__main__':
app.run(debug=True)