Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Initialize session state for persistent data
|
| 7 |
+
if 'expenses' not in st.session_state:
|
| 8 |
+
st.session_state.expenses = []
|
| 9 |
+
if 'salary' not in st.session_state:
|
| 10 |
+
st.session_state.salary = 0
|
| 11 |
+
|
| 12 |
+
# Set page title
|
| 13 |
+
st.title("Expense Tracker")
|
| 14 |
+
|
| 15 |
+
# Sidebar for user inputs
|
| 16 |
+
with st.sidebar:
|
| 17 |
+
st.header("Enter Your Salary")
|
| 18 |
+
salary = st.number_input("Monthly Salary", value=st.session_state.salary, step=100)
|
| 19 |
+
st.session_state.salary = salary
|
| 20 |
+
|
| 21 |
+
st.header("Add Expense")
|
| 22 |
+
expense_date = st.date_input("Date", datetime.now())
|
| 23 |
+
expense_category = st.selectbox("Category", ["Food", "Transport", "Entertainment", "Bills", "Other"])
|
| 24 |
+
expense_amount = st.number_input("Amount", min_value=0.0, step=0.01)
|
| 25 |
+
add_expense = st.button("Add Expense")
|
| 26 |
+
|
| 27 |
+
# Add expense to session state
|
| 28 |
+
if add_expense:
|
| 29 |
+
st.session_state.expenses.append({
|
| 30 |
+
"date": expense_date,
|
| 31 |
+
"category": expense_category,
|
| 32 |
+
"amount": expense_amount
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
# Convert expenses to DataFrame
|
| 36 |
+
df = pd.DataFrame(st.session_state.expenses)
|
| 37 |
+
|
| 38 |
+
# Calculate total expenses and current balance
|
| 39 |
+
total_expenses = df["amount"].sum() if not df.empty else 0
|
| 40 |
+
current_balance = st.session_state.salary - total_expenses
|
| 41 |
+
|
| 42 |
+
# Main dashboard
|
| 43 |
+
st.header("Dashboard")
|
| 44 |
+
|
| 45 |
+
# Display current balance
|
| 46 |
+
st.metric(label="Current Balance", value=f"{current_balance:,.2f}")
|
| 47 |
+
|
| 48 |
+
# Show expenses DataFrame
|
| 49 |
+
st.subheader("Expenses")
|
| 50 |
+
if not df.empty:
|
| 51 |
+
st.dataframe(df)
|
| 52 |
+
|
| 53 |
+
# Plot expenses by category
|
| 54 |
+
fig = px.pie(df, names='category', values='amount', title='Expenses by Category')
|
| 55 |
+
st.plotly_chart(fig)
|
| 56 |
+
|
| 57 |
+
# Plot expenses over time
|
| 58 |
+
df['date'] = pd.to_datetime(df['date'])
|
| 59 |
+
fig = px.bar(df, x='date', y='amount', color='category', title='Expenses Over Time')
|
| 60 |
+
st.plotly_chart(fig)
|
| 61 |
+
else:
|
| 62 |
+
st.write("No expenses added yet.")
|
| 63 |
+
|
| 64 |
+
# Allow user to clear all data
|
| 65 |
+
if st.button("Clear All Data"):
|
| 66 |
+
st.session_state.expenses = []
|
| 67 |
+
st.experimental_rerun()
|
| 68 |
+
|
| 69 |
+
# Additional creativity: Monthly budget and savings goals
|
| 70 |
+
st.sidebar.header("Monthly Budget and Savings Goals")
|
| 71 |
+
monthly_budget = st.sidebar.number_input("Monthly Budget", min_value=0, value=0, step=100)
|
| 72 |
+
savings_goal = st.sidebar.number_input("Savings Goal", min_value=0, value=0, step=100)
|
| 73 |
+
|
| 74 |
+
# Display budget and savings status
|
| 75 |
+
st.sidebar.metric(label="Total Expenses", value=f"${total_expenses:,.2f}")
|
| 76 |
+
st.sidebar.metric(label="Monthly Budget Remaining", value=f"${monthly_budget - total_expenses:,.2f}")
|
| 77 |
+
st.sidebar.metric(label="Savings Towards Goal", value=f"${current_balance - (monthly_budget - total_expenses):,.2f}")
|
| 78 |
+
|
| 79 |
+
st.sidebar.text("Tip: Adjust your expenses to meet your savings goal!")
|