Umar4321's picture
Update app.py
4389850 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# Set the title and style of the page
st.set_page_config(page_title="Expense Tracker", page_icon="๐Ÿ’ธ", layout="wide")
st.title("๐Ÿ’ฐ Expense Tracker")
# Add a description to explain the app
st.write("""
This app helps you track your expenses across various categories.
Fill in the amounts for each category, and the app will generate charts to help you visualize your spending.
""")
# Create a list of expense categories
categories = [
"Food ๐Ÿ”", "Transport ๐Ÿš—", "Utilities ๐Ÿ’ก", "Entertainment ๐ŸŽฎ",
"Other ๐Ÿ›๏ธ", "School Fees ๐ŸŽ“", "Health ๐Ÿ’Š", "Savings ๐Ÿ’ธ", "Charity ๐Ÿ’–", "Vacation ๐Ÿ–๏ธ"
]
# Collect user input for each category
amounts = []
for category in categories:
amount = st.number_input(f"Enter amount for {category}:", min_value=0.0, step=1.0)
amounts.append(amount)
# Create a DataFrame from the user input
df = pd.DataFrame({
"Category": categories,
"Amount": amounts
})
# Show the data in a table format
st.subheader("๐Ÿ“Š Expense Overview")
st.dataframe(df)
# Total expenses calculation
total_expenses = df["Amount"].sum()
# Display total expenses
st.markdown(f"### Total Expenses: ๐Ÿ’ธ **{total_expenses:.2f} PKR**")
# Generate the Pie chart and Bar chart if there are any expenses
if total_expenses > 0:
# ๐Ÿฅง Pie chart
st.subheader("๐Ÿฅง Expense Distribution")
fig1, ax1 = plt.subplots(figsize=(8, 6))
ax1.pie(df["Amount"], labels=df["Category"], autopct="%1.1f%%", startangle=90, colors=plt.cm.Paired.colors)
ax1.axis("equal") # Equal aspect ratio ensures that pie is drawn as a circle.
st.pyplot(fig1)
# ๐Ÿ“Š Bar chart
st.subheader("๐Ÿ“Š Expense Comparison")
fig2, ax2 = plt.subplots(figsize=(8, 6))
ax2.bar(df["Category"], df["Amount"], color=plt.cm.Paired.colors)
ax2.set_ylabel("Amount (PKR)")
ax2.set_title("Monthly Expenses by Category")
ax2.set_xticklabels(ax2.get_xticklabels(), rotation=45, ha="right")
st.pyplot(fig2)
else:
st.warning("Please enter some expenses to see the charts.")
# Add some fun interactivity with a slider
st.subheader("๐Ÿ’ก Quick Tip")
st.slider("What percentage of your expenses do you want to save this month?", 0, 100, 10)