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}**.")