-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHOME.py
283 lines (237 loc) · 9.9 KB
/
HOME.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# TODO: Make sidebar items collapsible. Maybe even home page items.
# TODO: Fix the session state issue with the date_from.
# TODO: Make an export settings button that exports the accounts, banks, categories. Also import settings.
# TODO: Make reset to factory settings button in the settings.
# TODO: Settings for default currency and for each account. Figure out how to load the conversion rates
# (for historical transactions they might differ greatly).
from my_scripts.Cashflow import Cashflow
from my_scripts.Account import AccountManager
from my_scripts import aggrid_stuff
from utils.checks import general_checks
from utils.example_setup import example_setup
from settings.constants import ALL_ACCOUNTS_FILTER
import layouts.sidebar as my_sidebar
import streamlit as st
import datetime
from st_aggrid import AgGrid, JsCode, GridOptionsBuilder, ColumnsAutoSizeMode
from utils.page_config import default_page_config
def change_date_sidebar_beginning():
st.session_state.date_from = first_date
st.session_state.date_to = datetime.date.today()
def change_date_sidebar_this_month():
st.session_state.date_from = datetime.date.today().replace(day=1)
st.session_state.date_to = datetime.date.today()
def change_date_sidebar_this_year():
st.session_state.date_from = datetime.date.today().replace(day=1, month=1)
st.session_state.date_to = datetime.date.today()
# ---PAGE CONFIG---
st.set_page_config(**default_page_config)
# ---PAGE BODY---
st.title("Cashmoney")
st.markdown("######")
# ---CHECK FOR CORRECT SETUP---
ctgs_check, accs_check, banks_check = general_checks()
if not ctgs_check or not accs_check or not banks_check:
st.text("Welcome to Cashmoney!")
st.text("To get started you should set up at least one account, define some categories and at provide a csv bank "
"statement with at least one transaction.")
st.text("You can define these in the sidebar on the left, or click this button to set up an example account and "
"few categories. You can delete/change these later.")
template_butt = st.button("Set up from templates")
if template_butt:
st.write("Creating example account and categories...")
example_setup()
st.experimental_rerun()
if not ctgs_check:
st.warning("You need to define at least one category to use Cashmoney.")
if not banks_check:
st.warning("You need to define at least one bank to use Cashmoney.")
if not accs_check:
st.warning("You have no accounts set up yet. Please set up at least one account.")
st.stop()
# --- INITIALIZE DF AND LOAD ITS FIRST DATE
cashflow = Cashflow()
acc_dict = cashflow.get_acc_dict()
cashflow_df = cashflow.get_df()
account_manager = AccountManager()
first_date = cashflow_df.index[-1].to_pydatetime()
# ---SIDEBAR---
# Account filter
selected_account, account_list = my_sidebar.account_filter(acc_dict)
acc_nums = []
account_transfers_checkbox = False
for acc in acc_dict:
acc_nums.append(acc_dict[acc]["account_number"])
if selected_account == ALL_ACCOUNTS_FILTER:
omitted_account_transfers = acc_nums
else:
account_list = selected_account
omitted_account_transfers = []
account_transfers_checkbox = my_sidebar.account_transfers_filter()
owners_checkbox = my_sidebar.recalculate_by_owners()
if owners_checkbox:
cashflow_df = cashflow.df.apply(cashflow.recalculate_by_owners, axis=1)
# Date filters
date_from, date_to = my_sidebar.date_filter(first_date)
col_1, col_2, col_3 = st.sidebar.columns(3)
with col_1:
st.button("All", on_click=change_date_sidebar_beginning, key="butt_from_beginning")
with col_2:
st.button("Month", on_click=change_date_sidebar_this_month, key="butt_this_month")
with col_3:
st.button("Year", on_click=change_date_sidebar_this_year, key="butt_this_year")
st.sidebar.divider()
# Income filter
check_income, check_expense = my_sidebar.income_filter()
st.sidebar.divider()
# Category filters
main_category, subcategory, wni = my_sidebar.category_filters(include_account_transfer=account_transfers_checkbox)
# ---QUERY AND STUFF---
# Omit unimportant columns
cashflow_df = cashflow_df[
[
"account_id",
"value",
"main_category",
"subcategory",
"wni",
"note_1",
"note_2",
"income",
"account",
"counterparty_account_id",
"sys_note",
"acc_note",
]
]
# Query
cashflow_df = cashflow_df.query(
"counterparty_account_id != @omitted_account_transfers &"
"account_id == @account_list & "
"main_category == @main_category & "
"subcategory == @subcategory & "
"wni == @wni & "
"(income == @check_income |"
"income != @check_expense )&"
"date >= @date_from & "
"date <= @date_to"
)
if len(cashflow_df) == 0:
st.warning("No transactions found for the selected filters.")
st.stop()
# ---PAGE BODY--- cont.
# Basic statistics
st.header("Basic stats")
stats = cashflow.basic_stats(cashflow_df)
stats_sums = stats.get("stats")
stats_monthly = stats.get("monthly")
available_balance = 0
# Available balance
if selected_account == ALL_ACCOUNTS_FILTER:
for acc in acc_dict:
acc_balance = acc_dict[acc][account_manager.balance]
acc_balance += acc_dict[acc][account_manager.delta]
if owners_checkbox:
acc_balance = acc_balance / acc_dict[acc][account_manager.owners]
available_balance += acc_balance
else:
available_balance = acc_dict[selected_account][account_manager.balance] + \
acc_dict[selected_account][account_manager.delta]
if owners_checkbox:
available_balance = available_balance / acc_dict[selected_account][account_manager.owners]
available_balance = int(available_balance)
# Sums
expenses_sum = stats_sums.get("expenses")
incomes_sum = stats_sums.get("incomes")
net_sum = stats_sums.get("net")
# Monthly averages
cashflow_incomes, cashflow_expenses = cashflow.filter_incomes(cashflow_df)
if len(cashflow_expenses) > 0:
monthly_avg_expenses = int(cashflow_expenses.resample("MS").sum().mean()[cashflow.value])
else:
monthly_avg_expenses = 0
if len(cashflow_incomes) > 0:
monthly_avg_incomes = int(cashflow_incomes.resample("MS").sum().mean()[cashflow.value])
else:
monthly_avg_incomes = 0
monthly_avg_net = monthly_avg_incomes + monthly_avg_expenses
# Visualize
st.subheader("Available balance")
st.subheader(":black_heart:" + f"{available_balance:,}".replace(",", " "))
col_1, col_2, col_3 = st.columns(3)
with col_1:
st.subheader("Sum of expenses:")
st.subheader(":heart:" + f"{expenses_sum:,}".replace(",", " "))
st.metric(label="Monthly averages:", value="", delta=monthly_avg_expenses)
with col_2:
st.subheader("Sum of incomes:")
st.subheader(":green_heart:" + f"{incomes_sum:,}".replace(",", " "))
st.metric(label="Sum of incomes", value="", delta=monthly_avg_incomes, label_visibility="hidden")
with col_3:
st.subheader(f"Net value:")
st.subheader(":blue_heart:" + f"{net_sum:,}".replace(",", " "))
st.metric(label="Net value", value="", delta=monthly_avg_net, label_visibility="hidden")
# --- Plots ---
tab_1, tab_2 = st.tabs(("Monthly bars", "Cumulative lines"))
# Bar chart
with tab_1:
st.header(f"Monthly bar chart")
monthly_expenses_bars = cashflow.plot_bars(df=cashflow.df_inc_exp_net, y_axis=cashflow.value, plot_vlines=True)
st.plotly_chart(monthly_expenses_bars, use_container_width=True)
# Line chart for last 3 months
with tab_2:
st.header("Cumulative sums for the past three months")
last_q_lines = cashflow.plot_last_q(cashflow_df)
st.plotly_chart(last_q_lines, use_container_width=True)
st.divider()
# Pies
# WNI pie
st.header("WNI Pie")
wni_pie = cashflow.plot_wni_pie(df=cashflow_df, exclude="account_transfer")
st.plotly_chart(wni_pie, use_container_width=True)
st.divider()
# Incomes/Expenses by category pies
st.header("Categories pies")
pie_expenses, pie_incomes = st.columns(2)
with pie_expenses:
st.subheader("Expenses")
categories_pie_expenses = cashflow.plot_categories_pies(df=cashflow_df, expenses=True)
st.plotly_chart(categories_pie_expenses, use_container_width=True)
with pie_incomes:
st.subheader("Incomes")
categories_pie_incomes = cashflow.plot_categories_pies(df=cashflow_df, expenses=False)
st.plotly_chart(categories_pie_incomes, use_container_width=True)
st.divider()
# --- TABLE ---
display_df = cashflow_df.reset_index()
gb = GridOptionsBuilder.from_dataframe(display_df)
gb.configure_default_column(editable=False, cellStyle=aggrid_stuff.unassigned_highlight, autosize=True)
gb.configure_column("date", type=["dateColumnFilter", "customDateTimeFormat"], custom_format_string='dd / MM / yyyy',
header_name="Date", width=110)
gb.configure_column("account_id", cellStyle=aggrid_stuff.highlight_accounts(acc_dict), header_name="Account")
gb.configure_column("value", header_name="Value", type=["numericColumn", "numberColumnFilter", "customNumericFormat"],
precision=2, width=100, cellStyle=aggrid_stuff.income_highlight)
gb.configure_column("main_category", header_name="Main category")
gb.configure_column("subcategory", header_name="Subcategory")
gb.configure_column("wni", header_name="Want / Need / Invest")
gb.configure_column("note_1", header_name="Note")
gb.configure_column("note_2", header_name="Additional note")
gb.configure_column("ID", hide=True)
gb.configure_column("account", hide=True, header_name="Account")
gb.configure_column("counterparty_account_id", hide=True, header_name="Counterparty account ID")
gb.configure_column("sys_note", hide=True, header_name="System note")
gb.configure_column("acc_note", hide=True, header_name="Account note")
gb.configure_column("income", hide=True, header_name="Income")
gridOptions = gb.build()
aggrid_height = None
if len(cashflow_df) > 10:
aggrid_height = 500
st.header("The data")
grid_response = AgGrid(
display_df,
gridOptions=gridOptions,
width='100%',
height=aggrid_height,
fit_columns_on_grid_load=True,
allow_unsafe_jscode=True,
)