def last_month() -> str:
first = date.today().replace(day=1)
return date(first.year - (first.month == 1), (first.month - 2) % 12 + 1, 1
).strftime("%Y-%m")
class MonthlyReport(PageState):
month: str = field(default_factory=last_month)
def change_month(report: MonthlyReport) -> None:
"""The picked month is already in ``report``; the KPIs re-render."""
@refreshable(deps=[MonthlyReport])
def monthly_kpis() -> None:
month = date.fromisoformat(MonthlyReport().month + "-01")
seed = month.month + month.year % 10
with ui.vstack(gap="sm", classes="w-full"):
ui.heading(f"{month:%B %Y}", level=3, size="md")
with ui.grid(cols=3, gap="md"):
for label, value, color in (
("New customers", str(40 + seed * 7), "success"),
("Churned", str(3 + seed % 5), "error"),
("MRR", f"€{18_400 + seed * 910:,}", "primary"),
):
with ui.vstack(gap="none"):
ui.text(label, color="muted", size="sm")
ui.text(value, size="xl", weight="semibold", color=color)
def monthly_report() -> None:
with ui.card(padding="md", classes="w-full max-w-lg"), ui.vstack(gap="md"):
ui.month_picker(value=MonthlyReport().month, clearable=False,
max=date.today(), on_change=change_month,
classes="max-w-xs")
monthly_kpis()