File size: 5,227 Bytes
c632215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import streamlit as st
import pandas as pd
import os
import plotly.express as px
from datetime import datetime

# -------------------------------
# CONFIG & STYLES
# -------------------------------
st.set_page_config(page_title="πŸ’Έ Expense Tracker", layout="wide")

st.markdown(
    """
    <style>
    body {
        background-color: #f8f9fa;
    }
    .title {
        font-size: 40px;
        font-weight: bold;
        color: #2c3e50;
        text-align: center;
        margin-bottom: 10px;
    }
    .subtitle {
        font-size: 18px;
        color: #7f8c8d;
        text-align: center;
        margin-bottom: 30px;
    }
    .metric-card {
        background: black;
        padding: 20px;
        border-radius: 15px;
        text-align: center;
        box-shadow: 0px 2px 6px rgba(0,0,0,0.1);
    }
    </style>
    """,
    unsafe_allow_html=True,
)

# -------------------------------
# FILE SETUP
# -------------------------------
CSV_FILE = "expenses.csv"

if not os.path.exists(CSV_FILE):
    df = pd.DataFrame(columns=["date", "category", "amount", "description"])
    df.to_csv(CSV_FILE, index=False)

# -------------------------------
# LOAD DATA
# -------------------------------
df = pd.read_csv(CSV_FILE)

# -------------------------------
# SIDEBAR NAVIGATION
# -------------------------------
st.sidebar.title("πŸ“Œ Navigation")
page = st.sidebar.radio("Go to", ["βž• Add Expense", "πŸ“Š Dashboard", "πŸ“‚ Expense Table"])

st.sidebar.markdown("---")
st.sidebar.write("πŸ’‘ Tip: Use filters to explore your expenses easily!")

# -------------------------------
# PAGE: ADD EXPENSE
# -------------------------------
if page == "βž• Add Expense":
    st.markdown("<div class='title'>Add New Expense</div>", unsafe_allow_html=True)

    with st.form("expense_form", clear_on_submit=True):
        col1, col2 = st.columns(2)
        with col1:
            date = st.date_input("Date", datetime.today())
            category = st.selectbox("Category", ["Food", "Transport", "Bills", "Entertainment", "Other"])
        with col2:
            amount = st.number_input("Amount", min_value=0.0, step=0.01)
            description = st.text_input("Description")

        submitted = st.form_submit_button("πŸ’Ύ Save Expense")

        if submitted:
            new_expense = pd.DataFrame(
                [[date, category, amount, description]],
                columns=["date", "category", "amount", "description"],
            )
            new_expense.to_csv(CSV_FILE, mode="a", header=False, index=False)
            st.success("βœ… Expense saved successfully!")

# -------------------------------
# PAGE: DASHBOARD
# -------------------------------
elif page == "πŸ“Š Dashboard":
    st.markdown("<div class='title'>Expense Dashboard</div>", unsafe_allow_html=True)
    st.markdown("<div class='subtitle'>Visualize your spending habits</div>", unsafe_allow_html=True)

    if df.empty:
        st.warning("⚠️ No data available yet. Add some expenses first.")
    else:
        # Metrics Row
        col1, col2, col3 = st.columns(3)
        with col1:
            st.markdown(f"<div class='metric-card'><h3>Total Spent</h3><p>Rs {df['amount'].sum():,.2f}</p></div>", unsafe_allow_html=True)
        with col2:
            top_cat = df.groupby("category")["amount"].sum().idxmax()
            st.markdown(f"<div class='metric-card'><h3>Top Category</h3><p>{top_cat}</p></div>", unsafe_allow_html=True)
        with col3:
            avg = df["amount"].mean()
            st.markdown(f"<div class='metric-card'><h3>Avg. Expense</h3><p>Rs {avg:,.2f}</p></div>", unsafe_allow_html=True)

        st.markdown("---")

        # Charts
        col1, col2 = st.columns(2)
        with col1:
            fig1 = px.pie(df, names="category", values="amount", title="Expenses by Category", hole=0.4)
            st.plotly_chart(fig1, use_container_width=True)

        with col2:
            df["date"] = pd.to_datetime(df["date"])
            fig2 = px.line(df, x="date", y="amount", title="Expenses Over Time", markers=True)
            st.plotly_chart(fig2, use_container_width=True)

# -------------------------------
# PAGE: EXPENSE TABLE
# -------------------------------
elif page == "πŸ“‚ Expense Table":
    st.markdown("<div class='title'>Expense Records</div>", unsafe_allow_html=True)

    if df.empty:
        st.warning("⚠️ No expenses found. Add some first.")
    else:
        with st.expander("πŸ” Filters", expanded=True):
            col1, col2 = st.columns(2)
            with col1:
                categories = st.multiselect("Filter by Category", df["category"].unique())
            with col2:
                date_range = st.date_input("Filter by Date Range", [])
            
            filtered = df.copy()
            if categories:
                filtered = filtered[filtered["category"].isin(categories)]
            if date_range:
                if len(date_range) == 2:
                    start, end = date_range
                    filtered = filtered[(pd.to_datetime(filtered["date"]) >= pd.to_datetime(start)) & 
                                        (pd.to_datetime(filtered["date"]) <= pd.to_datetime(end))]

        st.dataframe(filtered, use_container_width=True)