now allows downloading, defaults include health care and better leave calc. This is probably good enough now, so I have also saved a snapshot/csv of the data

This commit is contained in:
2025-01-31 20:07:40 +11:00
parent ae867d8fac
commit c2886faa30
7 changed files with 90 additions and 17 deletions

34
main.py
View File

@@ -1,7 +1,7 @@
# main.py
from flask import Flask, render_template, request, redirect, url_for, Response
from calc import calculate_savings_depletion
from db import init_db, get_finance_data, update_finance
from db import init_db, get_finance_data, update_finance, get_budget_data
from collections import defaultdict
from datetime import datetime
import csv
@@ -16,16 +16,10 @@ init_db()
def index():
finance_data = get_finance_data()
depletion_date, savings_per_fortnight, final_savings, TLS, CBA = calculate_savings_depletion(finance_data)
BUDGET=get_budget_data(finance_data, CBA, TLS)
if depletion_date:
depletion_date=depletion_date.date(); # just show date
# annual bills - rates (2.4), electricity (1.5), gas (2), internet (1.6), car insurance (.7), rego (.8), house insurance (2.4), GFC (2.2), phones (.5), melb. pollen (.03), nabu casa (.1), eweka (.1) --- noting phone is elevated presuming I also go onto Aldi plan, but that there is no family discount
bills = 14330
BUDGET=[]
BUDGET.append( ('Bills', f"${bills:,.2f}") )
BUDGET.append( ('Buffer', f"${CBA*finance_data['CBA_price']:,.2f}") )
BUDGET.append( ('Monthly budget', f"${((finance_data['Living_Expenses']-12000) / 12):,.2f}" ) )
BUDGET.append( ('Weekly budget', f"${((finance_data['Living_Expenses']-12000) / 52):,.2f}" ) )
return render_template('index.html', finance=finance_data, depletion_date=depletion_date, savings=savings_per_fortnight, TLS=TLS, CBA=CBA, BUDGET=BUDGET)
@app.route('/update', methods=['POST'])
@@ -65,6 +59,7 @@ def download_csv():
finance_data = get_finance_data()
depletion_date, savings_per_fortnight, final_savings, TLS, CBA = calculate_savings_depletion(finance_data)
BUDGET=get_budget_data(finance_data, CBA, TLS)
# Group data by year
data_by_year = defaultdict(list)
@@ -78,6 +73,10 @@ def download_csv():
# Sort years for column ordering
years = sorted(data_by_year.keys())
# get finance data into a list for spitting out during csv last column dump
fd_lst = list(finance_data.items())
fd_lst[0]=('Variable', 'Value')
# Create an in-memory output file
output = io.StringIO()
@@ -91,6 +90,7 @@ def download_csv():
writer.writerow(header)
# Write the data rows
cnt=0
for i in range(max_entries_per_year):
row = []
for year in years:
@@ -100,8 +100,26 @@ def download_csv():
row.extend([date, value])
else:
row.extend(["", ""]) # If no data for this year, leave blank cells
# spacer
row.extend([""])
# now add budget data
if cnt < len(BUDGET):
row.extend( BUDGET[cnt] )
else:
row.extend(["", ""]) # If no data for this year, leave blank cells
# spacer
row.extend([""])
# now add reference data
if cnt < len(fd_lst):
row.extend(fd_lst[cnt])
writer.writerow(row)
cnt += 1
csv_data = output.getvalue()
# Create a Flask Response object, with CSV mime type and downloadable as a file