File size: 3,041 Bytes
06ec561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# %%


# %%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import panel as pn

from bokeh.models.widgets.tables import NumberFormatter, DateFormatter
from IPython.display import display, Markdown
from loguru import logger

import ledger_husfinans_functions as lhf

pn.extension('tabulator')
plt.style.use("matplotlibstylehus.mplstyle")

split_percentage = {"Valentin":1-0.6278, "Bjørg&Marius":0.6278} #For budget felles alloc

# %%
dfs = pd.read_pickle("husfinans_hovedtabell.pickle")
dfs = dfs[
    (dfs["Account"] == "House")
]
dfs = dfs[["ID", "Type", "Date", "Amount", "Comment", "Name", "Split", "BudgetItem"]]
# dfs

# %%
baf = pd.read_pickle("husfinans_baf.pickle")
# baf = baf.set_index("BudgetItem")
# baf

# %%
actors = ["Bjørg&Marius", "Valentin"]

baf_formatters = {
    'Budget': NumberFormatter(format='0,0', text_align="right"),
    'Actual': NumberFormatter(format='0,0', text_align="right"),
    'Forecast': NumberFormatter(format='0,0', text_align="right"),
    'Estimate': NumberFormatter(format='0,0', text_align="right"),
    'Actual+F+E': NumberFormatter(format='0,0', text_align="right"),
    'Diff': NumberFormatter(format='0,0', text_align="right"),
}

def baf_prep(actors):
    cols = ["Budget", "Actual", "Forecast", "Estimate", "Actual+F+E", "Diff"]
    baf2 = baf[baf["Name"].isin(actors)]
    baf2 = baf2.pivot_table(index="BudgetItem", values=cols, aggfunc="sum", margins=True)
    baf2 = baf2[["Budget", "Actual", "Forecast", "Estimate", "Actual+F+E", "Diff"]]
    return baf2

baf_widget = pn.widgets.Tabulator(baf_prep(actors), disabled=True, formatters=baf_formatters)

dfs_formatters = {
    'Date': DateFormatter(format="%Y-%m-%d", text_align="right"),
    'Amount': NumberFormatter(format='0,0', text_align="right"),
    'Split': NumberFormatter(format='0.0%', text_align="right"),
}

dfs_widget = pn.widgets.Tabulator(pd.DataFrame(data=None, columns=[c for c in dfs.columns if c != "BudgetItem"]), disabled=True, show_index=False, formatters=dfs_formatters)


def click(event):
    # print(f'Clicked cell in {event.column!r} column, row {event.row!r} with value {event.value!r}')
    dfs2 = dfs[
        (dfs["BudgetItem"] == baf_widget.value.index[event.row])
        & dfs["Name"].isin(checkbutton_group.value)
        ]
    del dfs2["BudgetItem"]
    dfs_widget.value = dfs2

baf_widget.on_click(click) 


checkbutton_group = pn.widgets.CheckButtonGroup(name='Name', value=actors, options=actors, button_style="outline", button_type="primary")

def buttonclick(target, event):
    target.value = baf_prep(event.new)

checkbutton_group.link(baf_widget, callbacks={"value":buttonclick})

ww = pn.Column(checkbutton_group, pn.Row(baf_widget, dfs_widget))

title = "Bernhar Herres vei 3"
pn.template.BootstrapTemplate(
    title=title,
    main=ww,
    # main_max_width="min(50%, 698px)",
    header_background="#F08080",
).servable(title=title)