Files
daily-counter/app/pages/counters.py

103 lines
4.7 KiB
Python
Raw Normal View History

2026-03-31 19:09:37 +02:00
import streamlit as st
2026-04-25 10:38:21 +02:00
from streamlit import dialog
from tomlkit import key
from queries import crud, daily_stats, weekly_stats, monthly_stats, yearly_stats
2026-03-31 19:09:37 +02:00
from enums import CounterType
2026-04-25 10:38:21 +02:00
@dialog("Add New Counter", icon=":material/add_box:")
2026-03-31 19:09:37 +02:00
def _add_counter():
colors = crud.get_colors(1)
2026-03-31 19:09:37 +02:00
with st.form(key="add_counter", border=False, clear_on_submit=True):
2026-04-25 10:38:21 +02:00
title = st.text_input("Title:", key="new_counter_title")
counter_type_name = st.selectbox("Type", options=[e.name for e in CounterType], key="new_counter_type")
selected_color = st.radio("Color",
key="new_counter_color_selector",
2026-03-31 19:09:37 +02:00
width="stretch",
options=[colors[key][0] for key in colors],
format_func=lambda c: f"#{c}")
with st.container(horizontal=True, width="stretch", horizontal_alignment="center"):
2026-04-25 10:38:21 +02:00
if st.form_submit_button(label="Create", icon=":material/save:", key="create_counter_submit_btn"):
if not title:
raise ValueError("Title cannot be empty")
crud.create_counter(title, CounterType[counter_type_name], selected_color)
2026-03-31 19:09:37 +02:00
st.rerun()
2026-04-25 10:38:21 +02:00
@dialog("Remove Counter", icon=":material/delete:")
def _remove_counter(remove_counter_id:int):
2026-03-31 19:09:37 +02:00
with st.form(key="remove_counter", border=False, clear_on_submit=True):
st.subheader("Are you sure?")
with st.container(horizontal=True, width="stretch", horizontal_alignment="center"):
2026-04-25 10:38:21 +02:00
if st.form_submit_button("Confirm", icon=":material/delete:", key="remove_counter_submit_btn"):
crud.remove_counter(remove_counter_id)
2026-03-31 19:09:37 +02:00
st.rerun()
df = crud.get_counters()
2026-03-31 19:09:37 +02:00
with st.container(key="counter-table"):
for counter_id, name, counter_type_str, color in zip(df['id'], df['name'], df['type'], df['color']):
with st.container(width="stretch", key=f"counter_{counter_id}"):
with st.container(horizontal=True, width="stretch"):
st.header(f":material/calendar_clock: {name}", width="stretch")
if st.button("", icon=":material/exposure_plus_1:", key=f"increment_counter_{counter_id}"):
crud.increment_counter(counter_id)
2026-03-31 19:09:37 +02:00
st.rerun()
if st.button("", icon=":material/delete_forever:", key=f"remove_counter_{counter_id}"):
_remove_counter(counter_id)
with st.container(horizontal=True, width="stretch"):
counter_type = CounterType(counter_type_str)
stats_current_unit = counter_type.current_unit_text()
stats_prev_unit = counter_type.previous_unit_text()
match counter_type:
case CounterType.DAILY.value | CounterType.SIMPLE.value:
stats = daily_stats.get_daily_analytics(counter_id)
2026-03-31 19:09:37 +02:00
stats_current = stats.iloc[0]["count"]
stats_prev = stats.iloc[1]["count"]
case CounterType.WEEKLY.value:
stats = weekly_stats.get_weekly_analytics(counter_id)
stats_current = stats.iloc[0]["count"]
stats_prev = stats.iloc[1]["count"]
2026-03-31 19:09:37 +02:00
case CounterType.MONTHLY.value:
stats = monthly_stats.get_monthly_analytics(counter_id)
2026-03-31 19:09:37 +02:00
stats_current = stats.iloc[-1]["count"]
stats_prev = stats.iloc[-2]["count"]
case CounterType.YEARLY.value:
stats = yearly_stats.get_yearly_analytics(counter_id)
2026-03-31 19:09:37 +02:00
stats_current = stats.iloc[-1]["count"]
stats_prev = stats.iloc[-2]["count"]
if counter_type is CounterType.SIMPLE.value:
st.markdown(f"**{stats_current} {stats_current_unit}**")
else:
st.markdown(f"**{stats_current} {stats_current_unit}** *{stats_prev} {stats_prev_unit}*")
2026-03-31 19:09:37 +02:00
with st.container(horizontal=True, width="stretch", horizontal_alignment="right"):
st.page_link("pages/stats.py",
icon=":material/bar_chart:",
icon_position="right",
label="",
query_params={"counter_id": str(counter_id)})
2026-03-31 19:09:37 +02:00
st.html(f"""
<style>
div:has(> .st-key-counter_{counter_id}) {{
background-color: {color};
}}
</style>
""")
2026-04-25 10:38:21 +02:00
if st.button("Add Counter", width="stretch", icon=":material/add_box:", key="new_counter_button"):
2026-03-31 19:09:37 +02:00
_add_counter()