-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
135 lines (119 loc) · 5.8 KB
/
index.py
File metadata and controls
135 lines (119 loc) · 5.8 KB
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
import streamlit as st
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
sns.set_style("whitegrid")
plt.rcParams["font.size"] = 12
st.title("💰 Personal Finance Tracker")
st.markdown("Analyze your spending patterns with ease—upload your transaction data below!")
file = st.file_uploader(
"Upload CSV File",
type=["csv"],
help="Expected columns: 'Date' (e.g., 2025-01-01), 'Amount' (e.g., 45.80), 'Category' (e.g., Food)"
)
if file is not None:
try:
df = pd.read_csv(file, on_bad_lines='skip')
if not {'Date', 'Amount', 'Category'}.issubset(df.columns):
st.error("CSV must include 'Date', 'Amount', and 'Category' columns.")
st.stop()
except Exception as e:
st.error(f"Error loading file: {e}")
st.stop()
df.fillna({'Amount': 0, 'Category': 'Unknown'}, inplace=True)
df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce').fillna(0)
df = df[df['Amount'] >= 0] # Filter out negatives
df['Category'] = df['Category'].str.capitalize().str.strip()
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
df.dropna(subset=['Date'], inplace=True)
if df.empty:
st.warning("No valid data after cleaning. Check your file format.")
st.stop()
st.sidebar.header("🔍 Filters")
category_options = ["All"] + sorted(df['Category'].unique().tolist())
category_filter = st.sidebar.selectbox("Category", options=category_options)
if category_filter != "All":
df = df[df['Category'] == category_filter]
if not df['Date'].empty:
min_date, max_date = df['Date'].min().date(), df['Date'].max().date()
if min_date < max_date:
date_range = st.sidebar.slider(
"Date Range", min_date, max_date, (min_date, max_date), format="YYYY-MM-DD"
)
df = df[(df['Date'] >= pd.to_datetime(date_range[0])) &
(df['Date'] <= pd.to_datetime(date_range[1]))]
else:
st.sidebar.info("Date range filter unavailable (single date detected).")
tab1, tab2, tab3 = st.tabs(["📊 Overview", "🍰 Spending Breakdown", "📈 Trends"])
with tab1:
st.subheader("Data Snapshot")
col1, col2 = st.columns([3, 1])
with col1:
st.dataframe(df.style.format({"Amount": "${:.2f}"}), height=250, use_container_width=True)
with col2:
total_spending = df['Amount'].sum().round(2)
st.metric("Total Spending", f"${total_spending:,.2f}", delta_color="off")
if not df.empty:
top_category = df.groupby('Category')['Amount'].sum().idxmax()
top_spending = df.groupby('Category')['Amount'].sum().max().round(2)
st.metric("Top Category", top_category, f"${top_spending:,.2f}", delta_color="off")
with tab2:
st.subheader("Spending by Category")
if not df.empty:
col1, col2 = st.columns([1, 2])
category_spending = df.groupby('Category')['Amount'].sum().round(2)
with col1:
st.write("**Category Totals:**")
for cat, amt in category_spending.items():
st.write(f"{cat}: ${amt:,.2f}")
with col2:
fig, ax = plt.subplots(figsize=(12, 8))
ax.pie(category_spending, labels=category_spending.index, autopct='%1.0f%%',
colors=sns.color_palette("muted"), startangle=90)
ax.set_title("Spending Distribution", pad=20)
st.pyplot(fig)
with tab3:
if not df['Date'].empty:
st.subheader("Spending Trends")
df['Month'] = df['Date'].dt.to_period('M').astype(str)
monthly_spending = df.groupby('Month')['Amount'].sum().reset_index()
col1, col2 = st.columns(2)
with col1:
fig, ax = plt.subplots(figsize=(12, 8))
sns.barplot(x='Month', y='Amount', data=monthly_spending, palette='viridis', ax=ax)
ax.set_title("Monthly Spending", pad=20)
ax.set_xlabel("Month", labelpad=10)
ax.set_ylabel("Amount ($)", labelpad=10)
plt.xticks(rotation=45, ha="right")
st.pyplot(fig)
with col2:
df['Week'] = df['Date'].dt.to_period('W').astype(str)
weekly_spending = df.groupby('Week')['Amount'].sum().reset_index().tail(10)
fig, ax = plt.subplots(figsize=(12, 8))
sns.barplot(x='Week', y='Amount', data=weekly_spending, palette='coolwarm', ax=ax)
ax.set_title("Weekly Spending (Last 10 Weeks)", pad=20)
ax.set_xlabel("Week", labelpad=10)
ax.set_ylabel("Amount ($)", labelpad=10)
plt.xticks(rotation=45, ha="right")
st.pyplot(fig)
st.subheader("Day & Month Trends")
df['DayOfWeek'] = df['Date'].dt.day_name()
df['Month'] = df['Date'].dt.month_name()
heatmap_data = df.pivot_table(values='Amount', index='DayOfWeek', columns='Month',
aggfunc='sum', fill_value=0)
fig, ax = plt.subplots(figsize=(12, 8))
sns.heatmap(heatmap_data, annot=True, fmt=".0f", cmap="YlGnBu",
cbar_kws={'label': 'Spending ($)'}, ax=ax)
ax.set_title("Spending by Day and Month", pad=20)
st.pyplot(fig)
st.download_button(
label="⬇️ Download Filtered Data",
data=df.to_csv(index=False).encode('utf-8'),
file_name="filtered_finance_data.csv",
mime="text/csv"
)
else:
st.info("Please upload a CSV file with 'Date', 'Amount', and 'Category' columns to get started.")
st.markdown("---")
st.caption("Built on Streamlit 👩💻| Sneha’s Finance Tracker")