Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
# Function to calculate renting cost
|
| 5 |
+
def calculate_renting_cost(rent, annual_increase, years):
|
| 6 |
+
total_rent = 0
|
| 7 |
+
for year in range(years):
|
| 8 |
+
total_rent += rent * 12
|
| 9 |
+
rent += rent * (annual_increase / 100)
|
| 10 |
+
return total_rent
|
| 11 |
+
|
| 12 |
+
# Function to calculate buying cost
|
| 13 |
+
def calculate_buying_cost(property_price, down_payment_percentage, mortgage_rate, loan_years, tax, maintenance):
|
| 14 |
+
down_payment = property_price * (down_payment_percentage / 100)
|
| 15 |
+
loan_amount = property_price - down_payment
|
| 16 |
+
monthly_interest_rate = mortgage_rate / 100 / 12
|
| 17 |
+
months = loan_years * 12
|
| 18 |
+
|
| 19 |
+
# Monthly mortgage payment formula
|
| 20 |
+
monthly_payment = (loan_amount * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -months)
|
| 21 |
+
total_mortgage_cost = monthly_payment * months
|
| 22 |
+
|
| 23 |
+
# Additional costs
|
| 24 |
+
total_tax = tax * loan_years
|
| 25 |
+
total_maintenance = maintenance * loan_years
|
| 26 |
+
|
| 27 |
+
return total_mortgage_cost + total_tax + total_maintenance
|
| 28 |
+
|
| 29 |
+
# Streamlit app
|
| 30 |
+
st.title("Rent vs. Buy Calculator")
|
| 31 |
+
st.write("Compare the cost of renting vs. buying a property to make an informed decision.")
|
| 32 |
+
|
| 33 |
+
# User inputs
|
| 34 |
+
st.header("Renting Details")
|
| 35 |
+
rent = st.number_input("Monthly Rent ($)", value=1500.0, step=100.0)
|
| 36 |
+
annual_increase = st.number_input("Annual Rent Increase (%)", value=3.0, step=0.1)
|
| 37 |
+
years_renting = st.number_input("Number of Years Renting", value=5, step=1, min_value=1)
|
| 38 |
+
|
| 39 |
+
st.header("Buying Details")
|
| 40 |
+
property_price = st.number_input("Property Price ($)", value=300000.0, step=5000.0)
|
| 41 |
+
down_payment_percentage = st.number_input("Down Payment (%)", value=20.0, step=1.0)
|
| 42 |
+
mortgage_rate = st.number_input("Mortgage Interest Rate (%)", value=5.0, step=0.1)
|
| 43 |
+
loan_years = st.number_input("Loan Tenure (Years)", value=30, step=1, min_value=1)
|
| 44 |
+
tax = st.number_input("Annual Property Tax ($)", value=3000.0, step=100.0)
|
| 45 |
+
maintenance = st.number_input("Annual Maintenance Costs ($)", value=2000.0, step=100.0)
|
| 46 |
+
|
| 47 |
+
# Calculate costs
|
| 48 |
+
if st.button("Calculate"):
|
| 49 |
+
renting_cost = calculate_renting_cost(rent, annual_increase, years_renting)
|
| 50 |
+
buying_cost = calculate_buying_cost(property_price, down_payment_percentage, mortgage_rate, loan_years, tax, maintenance)
|
| 51 |
+
|
| 52 |
+
st.header("Results")
|
| 53 |
+
st.write(f"**Total Renting Cost:** ${renting_cost:,.2f}")
|
| 54 |
+
st.write(f"**Total Buying Cost (including taxes and maintenance):** ${buying_cost:,.2f}")
|
| 55 |
+
|
| 56 |
+
if buying_cost < renting_cost:
|
| 57 |
+
st.success("Buying is more cost-effective than renting based on the inputs provided.")
|
| 58 |
+
else:
|
| 59 |
+
st.warning("Renting is more cost-effective than buying based on the inputs provided.")
|
| 60 |
+
|
| 61 |
+
# Add footer
|
| 62 |
+
st.write("\n---")
|
| 63 |
+
st.write("App created using Streamlit and ready to deploy on Hugging Face Spaces!")
|