File size: 2,289 Bytes
4b1347e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd

# Set the title of the app
st.title("Simple Finance Tracker")

# Sidebar for data input
st.sidebar.header("Enter Your Income and Expenses")

# Input for total income
income = st.sidebar.number_input("Enter your total income for the month ($)", min_value=0.0, value=0.0, step=50.0)

# Input for custom expense categories
st.sidebar.header("Add Expense Categories")

# Use a form to input dynamic expenses
with st.sidebar.form(key='expense_form'):
    # Allow user to input a category and its corresponding expense
    expense_category = st.text_input("Expense Category (e.g., Rent, Groceries, etc.)", "")
    expense_amount = st.number_input("Expense Amount ($)", min_value=0.0, value=0.0, step=10.0)
    
    # Add expense to the list
    submit_button = st.form_submit_button(label="Add Expense")
    
    # Initialize session state for tracking expenses
    if 'expenses' not in st.session_state:
        st.session_state['expenses'] = {}

    # When submit button is pressed, add the expense
    if submit_button and expense_category:
        st.session_state.expenses[expense_category] = expense_amount
        st.success(f"Added {expense_category}: ${expense_amount:.2f}")

# Display current list of expenses
if st.session_state.expenses:
    st.write("### Current Expenses")
    expense_df = pd.DataFrame(list(st.session_state.expenses.items()), columns=["Category", "Amount"])
    st.write(expense_df)
else:
    st.write("No expenses added yet.")

# Calculate total expenses and balance
total_expenses = sum(st.session_state.expenses.values())
balance = income - total_expenses

# Show income, expenses summary, and balance
st.write("### Income and Expenses Summary")
st.write(f"**Total Income:** ${income:,.2f}")
st.write(f"**Total Expenses:** ${total_expenses:,.2f}")
st.write(f"**Balance:** ${balance:,.2f}")

# Display expense distribution in a bar chart
if st.session_state.expenses:
    st.write("#### Expense Distribution (Bar Chart):")
    st.bar_chart(expense_df.set_index('Category')['Amount'])

# Show balance and savings
st.write("### Your Balance")
if balance >= 0:
    st.success(f"You're doing great! You have a balance of **${balance:,.2f}** left.")
else:
    st.error(f"You're in deficit! You owe **${-balance:,.2f}**.")