import streamlit as st from streamlit import dialog from tomlkit import key from queries import crud, daily_stats, weekly_stats, monthly_stats, yearly_stats from enums import CounterType @dialog("Add New Counter", icon=":material/add_box:") def _add_counter(): colors = crud.get_colors(1) with st.form(key="add_counter", border=False, clear_on_submit=True): 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", 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"): 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) st.rerun() @dialog("Remove Counter", icon=":material/delete:") def _remove_counter(remove_counter_id:int): 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"): if st.form_submit_button("Confirm", icon=":material/delete:", key="remove_counter_submit_btn"): crud.remove_counter(remove_counter_id) st.rerun() df = crud.get_counters() 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) 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) 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"] case CounterType.MONTHLY.value: stats = monthly_stats.get_monthly_analytics(counter_id) stats_current = stats.iloc[-1]["count"] stats_prev = stats.iloc[-2]["count"] case CounterType.YEARLY.value: stats = yearly_stats.get_yearly_analytics(counter_id) 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}*") 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)}) st.html(f""" """) if st.button("Add Counter", width="stretch", icon=":material/add_box:", key="new_counter_button"): _add_counter()