-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_spending_by_year.py
49 lines (41 loc) · 1.56 KB
/
visualize_spending_by_year.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
# visualize_spending_by_year.py
"""From an input of expense data summarized
by group per year, gnerate a set of visualizations
"""
import webbrowser
import os
import visualization_methods as vms
# Import the shared configuration file
import expenses_config as ec
# Name of the html report generated by this module
HTML_OUT = ec.REPORTS_PATH + "annual-spending.html"
HTML_F = open(HTML_OUT, "w")
# Create a dataframe from the annual spending by group data file
df = vms.read_structured_transactions(
ec.PATH_TO_SPENDING_BY_GROUP,
ec.PATH_TO_YOUR_TRANSACTIONS,
"Spending Group",
"summarized spending group data",
)
# year over year visualizations we may have different categories each year
# Create an assigned color for each category so the colors are consistent
colors = vms.assign_colors_to_groups(df)
# Iterate through the columns which are formatted "YEAR Amount"
for col in df.columns:
year = col.split(" ", 1)[0]
# Ignore years with dirty or incomplete data
if int(year) < ec.IGNORE_YEARS_BEFORE:
continue
print("Visualizing spending for year:" + year + "...")
year_df = df[year + " Amount"]
if not len(year_df):
print("No data found for " + year)
continue
report_png = str(year) + "-spending-by-category.png"
vms.visualize_expenses_by_group(year, year_df, colors, ec.REPORTS_PATH + report_png)
print("<image src=./" + report_png + ">", file=HTML_F)
# Show the report in a webbrowser
HTML_F.close()
webbrowser.open(
"file://" + os.path.realpath(HTML_OUT), new=2
) # new=2: open in a new tab, if possible