Spaces:
Runtime error
Runtime error
File size: 2,796 Bytes
6f8c36d |
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 |
import streamlit as st
import pandas as pd
import plotly.express as px
from datetime import datetime
# Initialize session state for persistent data
if 'expenses' not in st.session_state:
st.session_state.expenses = []
if 'salary' not in st.session_state:
st.session_state.salary = 0
# Set page title
st.title("Expense Tracker")
# Sidebar for user inputs
with st.sidebar:
st.header("Enter Your Salary")
salary = st.number_input("Monthly Salary", value=st.session_state.salary, step=100)
st.session_state.salary = salary
st.header("Add Expense")
expense_date = st.date_input("Date", datetime.now())
expense_category = st.selectbox("Category", ["Food", "Transport", "Entertainment", "Bills", "Other"])
expense_amount = st.number_input("Amount", min_value=0.0, step=0.01)
add_expense = st.button("Add Expense")
# Add expense to session state
if add_expense:
st.session_state.expenses.append({
"date": expense_date,
"category": expense_category,
"amount": expense_amount
})
# Convert expenses to DataFrame
df = pd.DataFrame(st.session_state.expenses)
# Calculate total expenses and current balance
total_expenses = df["amount"].sum() if not df.empty else 0
current_balance = st.session_state.salary - total_expenses
# Main dashboard
st.header("Dashboard")
# Display current balance
st.metric(label="Current Balance", value=f"{current_balance:,.2f}")
# Show expenses DataFrame
st.subheader("Expenses")
if not df.empty:
st.dataframe(df)
# Plot expenses by category
fig = px.pie(df, names='category', values='amount', title='Expenses by Category')
st.plotly_chart(fig)
# Plot expenses over time
df['date'] = pd.to_datetime(df['date'])
fig = px.bar(df, x='date', y='amount', color='category', title='Expenses Over Time')
st.plotly_chart(fig)
else:
st.write("No expenses added yet.")
# Allow user to clear all data
if st.button("Clear All Data"):
st.session_state.expenses = []
st.experimental_rerun()
# Additional creativity: Monthly budget and savings goals
st.sidebar.header("Monthly Budget and Savings Goals")
monthly_budget = st.sidebar.number_input("Monthly Budget", min_value=0, value=0, step=100)
savings_goal = st.sidebar.number_input("Savings Goal", min_value=0, value=0, step=100)
# Display budget and savings status
st.sidebar.metric(label="Total Expenses", value=f"${total_expenses:,.2f}")
st.sidebar.metric(label="Monthly Budget Remaining", value=f"${monthly_budget - total_expenses:,.2f}")
st.sidebar.metric(label="Savings Towards Goal", value=f"${current_balance - (monthly_budget - total_expenses):,.2f}")
st.sidebar.text("Tip: Adjust your expenses to meet your savings goal!")
|