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( """ """, 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("
Add New Expense
", 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("
Expense Dashboard
", unsafe_allow_html=True) st.markdown("
Visualize your spending habits
", 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"

Total Spent

Rs {df['amount'].sum():,.2f}

", unsafe_allow_html=True) with col2: top_cat = df.groupby("category")["amount"].sum().idxmax() st.markdown(f"

Top Category

{top_cat}

", unsafe_allow_html=True) with col3: avg = df["amount"].mean() st.markdown(f"

Avg. Expense

Rs {avg:,.2f}

", 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("
Expense Records
", 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)