Spaces:
Runtime error
Runtime error
LearnwithShashank commited on
Commit Β·
858f00c
1
Parent(s): 5e7abce
Files changed (4)
- README.md+2-1
- expense.pyAdded+74
- expenses.csvAdded+1
- requirements.txtAdded+2
2 unmodified lines34567892 unmodified linesemoji: πcolorFrom: bluecolorTo: redsdk: staticpinned: false---2 unmodified lines3456789102 unmodified linesemoji: πcolorFrom: bluecolorTo: redsdk: streamlitapp_file: expense.pypinned: false---
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import pandas as pdimport streamlit as stimport osfrom datetime import datetimeimport matplotlib.pyplot as plt
# App configst.set_page_config(page_title="Expense Tracker", layout="wide")st.title("πΈ Personal Expense Tracker")
# File to store expensesDATA_FILE = "expenses.csv"
# Initialize fileif not os.path.exists(DATA_FILE): df_init = pd.DataFrame(columns=["Date", "Category", "Amount", "Notes"]) df_init.to_csv(DATA_FILE, index=False)
# Sidebar Navigationmenu = st.sidebar.radio("Navigate", ["β Add Expense", "π View Analytics"])
# Add Expense Pageif menu == "β Add Expense": st.header("Log Your Expense")
with st.form("expense_form"): date = st.date_input("Date", value=datetime.today()) category = st.selectbox("Category", ["Food", "Transport", "Utilities", "Shopping", "Entertainment", "Other"]) amount = st.number_input("Amount Spent (βΉ)", min_value=0.0, format="%.2f") notes = st.text_input("Notes (optional)") submitted = st.form_submit_button("Add Expense")
if submitted: new_data = pd.DataFrame([[date, category, amount, notes]], columns=["Date", "Category", "Amount", "Notes"]) new_data.to_csv(DATA_FILE, mode='a', header=False, index=False) st.success("Expense added successfully!")
# View Analytics Pageelse: st.header("π Expense Analytics") df = pd.read_csv(DATA_FILE, parse_dates=["Date"])
if df.empty: st.warning("No expenses found. Please add some data first.") else: col1, col2 = st.columns(2) with col1: st.metric("Total Spent", f"βΉ{df['Amount'].sum():,.2f}") with col2: st.metric("Total Entries", len(df))
df["Month"] = df["Date"].dt.to_period("M").astype(str)
# Daily expense trend daily = df.groupby("Date")["Amount"].sum() st.subheader("ποΈ Daily Spending Trend") st.line_chart(daily)
# Category-wise pie chart category_totals = df.groupby("Category")["Amount"].sum() st.subheader("π Spending by Category") fig, ax = plt.subplots() ax.pie(category_totals, labels=category_totals.index, autopct='%1.1f%%', startangle=90) ax.axis("equal") st.pyplot(fig)
# Monthly summary st.subheader("π
Monthly Summary") monthly = df.groupby("Month")["Amount"].sum().reset_index() st.bar_chart(monthly.set_index("Month"))
# Download data st.download_button("Download Data as CSV", data=df.to_csv(index=False), file_name="expenses.csv")1Date,Category,Amount,Notes12streamlitmatplotlibREADME.md+2-1
expense.pyAdded+74
expenses.csvAdded+1
requirements.txtAdded+2