2025-08-24 21:28:33 +02:00
|
|
|
import pandas as pd
|
2025-09-06 07:27:08 +02:00
|
|
|
from dash import Dash, dcc, html, dash_table
|
2025-08-24 21:28:33 +02:00
|
|
|
from dash.dependencies import Input, Output
|
|
|
|
|
|
|
|
from ..data.loader import DataSchema
|
|
|
|
from . import ids
|
|
|
|
|
|
|
|
|
|
|
|
def render(app: Dash, data: pd.DataFrame) -> html.Div:
|
|
|
|
@app.callback(
|
2025-09-06 07:27:08 +02:00
|
|
|
Output(ids.DATA_TABLE, "children"),
|
2025-08-24 21:28:33 +02:00
|
|
|
[
|
|
|
|
Input(ids.YEAR_DROPDOWN, "value"),
|
|
|
|
Input(ids.MONTH_DROPDOWN, "value"),
|
|
|
|
Input(ids.CATEGORY_DROPDOWN, "value"),
|
|
|
|
],
|
|
|
|
)
|
2025-09-05 05:46:52 +02:00
|
|
|
def update_data_table(
|
2025-08-24 21:28:33 +02:00
|
|
|
years: list[str], months: list[str], categories: list[str]
|
2025-09-06 07:07:08 +02:00
|
|
|
) -> html.Div:
|
2025-08-24 21:28:33 +02:00
|
|
|
filtered_data = data.query(
|
|
|
|
"year in @years and month in @months and category in @categories"
|
|
|
|
)
|
|
|
|
|
|
|
|
if filtered_data.shape[0] == 0:
|
2025-09-06 07:27:08 +02:00
|
|
|
return html.Div("No data selected.")
|
|
|
|
|
2025-08-24 21:28:33 +02:00
|
|
|
def create_pivot_table() -> pd.DataFrame:
|
|
|
|
pt = filtered_data.pivot_table(
|
|
|
|
values=DataSchema.AMOUNT,
|
|
|
|
index=[DataSchema.CATEGORY],
|
|
|
|
aggfunc="sum",
|
|
|
|
fill_value=0,
|
|
|
|
dropna=False,
|
|
|
|
)
|
|
|
|
return pt.reset_index().sort_values(DataSchema.AMOUNT, ascending=False)
|
|
|
|
|
2025-09-06 07:27:08 +02:00
|
|
|
pt = create_pivot_table()
|
|
|
|
|
|
|
|
return dash_table.DataTable(
|
|
|
|
data=pt.to_dict("records"),
|
|
|
|
columns=[{"name": i, "id": i} for i in pt.columns],
|
|
|
|
)
|
2025-09-05 05:46:52 +02:00
|
|
|
|
2025-09-06 07:27:08 +02:00
|
|
|
return html.Div(id=ids.DATA_TABLE)
|