-
Notifications
You must be signed in to change notification settings - Fork 0
/
networth.py
357 lines (307 loc) · 9.08 KB
/
networth.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from reactpy import component, html, hooks, event, utils
from reactpy.backend.fastapi import configure, Options
from fastapi import FastAPI
from sqlmodeldb import (create_db_and_tables, add_deal,
select_deals, delete_deal)
import pandas as pd
create_db_and_tables()
BOOTSTRAP_CSS = html.link(
{
'rel': 'stylesheet',
'href': 'https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/'
'dist/css/bootstrap.min.css',
'integrity': 'sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Y'
'z1ztcQTwFspd3yD65VohhpuuCOmLASjC',
'crossorigin': 'anonymous',
}
)
PAGE_HEADER_TITLE = 'ReactPy-NetWorth'
CARD_TEXT_COLOR = {'Assets': 'text-success', 'Liabilities': 'text-danger',
'Net Worth': 'text-primary'}
CAT_OPTIONS = ['Clothing', 'Donation', 'Education', 'Food', 'Healthcare',
'Housing', 'Income', 'Insurance', 'Loans', 'Others',
'Personals', 'Recreation', 'Retirement', 'Supplies',
'Transportation', 'Utilities']
def get_records(db) -> list[list]:
"""Converts db to list of list.
Args:
db: A list of Deal class from sqlmodeldb module.
"""
return [[da.id, da.date, da.value, da.category, da.comment] for da in db]
def select_options():
return [html.option(opt) for opt in CAT_OPTIONS]
@component
def FormDateInput(date, set_date):
return html.div(
{'class': 'mb-3'},
html.label(
{
'class': 'form-label fw-bold',
'for': 'date'
},
'Date',
),
html.input(
{
'class': 'form-control',
'type': 'date',
'id': 'date',
'placeholder': 'enter date',
'on_change': lambda event: set_date(event['target']['value']),
'value': date,
},
),
)
@component
def FormAmountInput(amount, set_amount):
return html.div(
{'class': 'mb-3'},
html.label(
{
'class': 'form-label fw-bold',
'for': 'amount'
},
'Amount',
),
html.input(
{
'class': 'form-control',
'type': 'text',
'id': 'amount',
'placeholder': 'enter amount',
'on_change': lambda event: set_amount(event['target']['value']),
'value': amount,
},
),
)
@component
def FormSelect(category, set_category):
return html.div(
{'class': 'mb-3'},
html.label(
{
'class': 'form-label fw-bold',
'for': 'category'
},
'Category',
),
html.select(
{
'class': 'form-select',
'id': 'category',
'on_change': lambda event: set_category(event['target']['value']),
'value': category,
},
select_options(),
),
)
@component
def FormCommentInput(comment, set_comment):
return html.div(
{'class': 'mb-3'},
html.label(
{
'class': 'form-label fw-bold',
'for': 'comment',
},
'Comment',
),
html.input(
{
'class': 'form-control',
'type': 'text',
'id': 'comment',
'placeholder': 'enter comment',
'on_change': lambda event: set_comment(event['target']['value']),
'value': comment,
},
),
)
@component
def FormSaveButton():
return html.input(
{
'class': 'btn btn-primary mb-3',
'type': 'submit',
'value': 'Save'
},
)
@component
def FormDeleteInput(bad_id, set_bad_id):
return html.div(
{'class': 'mt-3'},
html.label(
{
'class': 'form-label fw-bold',
'for': 'bad_id'
},
'Id to delete',
),
html.input(
{
'class': 'form-control',
'type': 'text',
'id': 'bad_id',
'placeholder': 'enter id to delete',
'on_change': lambda event: set_bad_id(event['target']['value']),
'value': bad_id
},
),
)
@component
def FormDeleteButton():
return html.input(
{
'class': 'btn btn-danger my-3',
'type': 'submit',
'value': 'Delete',
}
)
@component
def Card(label, amount):
"""Shows label and amount in a bootstrap card.
Text color is styled depending on the label and amount.
"""
text_color = CARD_TEXT_COLOR[label]
if label == 'Net Worth' and amount < 0:
text_color = 'text-warning'
return html.div(
{'class': 'col-sm-4'},
html.div(
{'class': 'card mb-3 border-primary'},
html.div(
{'class': 'card-body'},
html.div(
{'class': f'card-title {text_color}'},
html.h5(
{'style': {'font-family': 'Chalkduster, fantasy'}},
label
),
),
html.div(
{'class': 'card-text'},
html.span({'class': f'{text_color} fw-bold'}, amount),
),
),
),
)
@component
def Summary():
"""Generates three cards for asset, liability and net worth.
These cards are displayed in a row at the top of the page.
The asset and liablity values are taken from the sqlite database
using SQLModel library.
All positive amounts are assets whereas all negative
amounts are liabilities.
"""
db_results = select_deals()
records = get_records(db_results)
asset_value = sum([rec[2] for rec in records if rec[2] >= 0])
liability_value = sum([rec[2] for rec in records if rec[2] < 0])
net_value = asset_value + liability_value
return html.div(
html.div(
{'class': 'row'},
Card('Assets', asset_value),
Card('Liabilities', liability_value),
Card('Net Worth', net_value),
),
)
@component
def DealHistory():
"""Displays the history of deals or transactions.
Converts sqlite records to dataframe using pandas then to html and
finally to vdom.
"""
db_results = select_deals()
records = get_records(db_results)
# Show records in reverse order or latest deals first.
records = records[::-1]
df = pd.DataFrame(
records,
columns=['Id', 'Date', 'Amount', 'Category', 'Comment']
)
html_rec = df.to_html(
index=False,
border=0,
justify='center',
classes=[
'table', 'table-primary',
'table-bordered', 'text-center',
'table-striped', 'table-hover'
]
)
return html.div(
html.div({'class': 'fw-bold'}, 'Deals history'),
html.div(
{
'class': 'mt-2',
'style': {
'height': '210px',
'overflow-y': 'auto',
}
},
utils.html_to_vdom(html_rec),
),
)
@component
def NetWorth():
date, set_date = hooks.use_state('')
amount, set_amount = hooks.use_state('')
category, set_category = hooks.use_state('')
comment, set_comment = hooks.use_state('')
bad_id, set_bad_id = hooks.use_state('')
@event(prevent_default=True)
def delete_id(event):
delete_deal(int(bad_id))
set_bad_id('')
@event(prevent_default=True)
def save_record(event):
if amount == '':
return
add_deal(date, amount, category, comment)
# Reset
set_date('')
set_amount('')
set_category('')
set_comment('')
return html.div(
BOOTSTRAP_CSS,
html.div(
{'class': 'container mt-3'},
Summary(),
html.div(
{'class': 'row'},
html.div(
{'class': 'col-lg-6'},
html.form(
{'on_submit': save_record},
FormDateInput(date, set_date),
FormAmountInput(amount, set_amount),
FormSelect(category, set_category),
FormCommentInput(comment, set_comment),
FormSaveButton(),
),
),
html.div(
{'class': 'col-lg-6'},
DealHistory(),
html.form(
{'on_submit': delete_id},
FormDeleteInput(bad_id, set_bad_id),
FormDeleteButton(),
),
),
),
),
)
app = FastAPI()
configure(
app,
NetWorth,
options=Options(
head=html.head(
html.title(PAGE_HEADER_TITLE)
)
)
)