Build a web application (preferably STREAMLIT) that compares the following options: Option A: Leasing a car from work Option B: Buying a car Option C: Operational leasing At this stage, each option is linked to a specific vehicle and there is no choice between different vehicles (we will change this in the next stage). The different options together with all the options that are worth displaying in the following Excel data (the numbers are in shekels): Downloads/car Before presenting the different options to the user, ask them to fill out a short form: Name: _______ Type of vehicle: Family car not older than 2020 Financing capabilities: ______ shekels Km per year: ________ Show: (CHECKBOX) Leasing vehicle | Buying a car | Operational leasing
To upload files, please first save the app
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
st.set_page_config(page_title="Car Comparison Tool", layout="wide")
# Initialize session state
if 'submitted' not in st.session_state:
st.session_state.submitted = False
def calculate_total_cost(data, years=5):
"""Calculate total cost over 5 years"""
return {
'Initial Cost': data.get('initial_cost', 0),
'Monthly Payments': data.get('monthly_payment', 0) * 12 * years,
'Maintenance': data.get('maintenance_yearly', 0) * years,
'Insurance': data.get('insurance_yearly', 0) * years,
'Fuel': data.get('fuel_yearly', 0) * years,
'Total': (data.get('initial_cost', 0) +
(data.get('monthly_payment', 0) * 12 * years) +
(data.get('maintenance_yearly', 0) * years) +
(data.get('insurance_yearly', 0) * years) +
(data.get('fuel_yearly', 0) * years))
}
# Sample data (in real app, this would come from Excel)
car_options = {
'Work Lease': {
'initial_cost': 0,
'monthly_payment': 2000,
'maintenance_yearly': 3000,
'insurance_yearly': 5000,
'fuel_yearly': 6000,
},
'Purchase': {
'initial_cost': 150000,
'monthly_payment': 0,
'maintenance_yearly': 4000,
'insurance_yearly': 6000,
'fuel_yearly': 6000,
},
'Operational Lease': {
'initial_cost': 10000,
'monthly_payment': 2500,
'maintenance_yearly': 0, # Included in monthly payment
'insurance_yearly': 0, # Included in monthly payment
'fuel_yearly': 6000,
}
}
st.title("Car Comparison Tool 🚗")
# User Input Form
with st.form("user_form"):
st.write("### Personal Information")
col1, col2 = st.columns(2)
with col1:
name = st.text_input("Name")
financing = st.number_input("Financing capabilities (₪)", min_value=0, value=150000)
with col2:
vehicle_type = st.selectbox("Type of vehicle", ["Family car not older than 2020"])
km_per_year = st.number_input("Kilometers per year", min_value=0, value=20000)
st.write("### Compare Options")
col1, col2, col3 = st.columns(3)
with col1:
show_work_lease = st.checkbox("Work Leasing", value=True)
with col2:
show_purchase = st.checkbox("Car Purchase", value=True)
with col3:
show_op_lease = st.checkbox("Operational Leasing", value=True)
submitted = st.form_submit_button("Compare Options")
if submitted:
st.session_state.submitted = True
# Display comparison if form is submitted
if st.session_state.submitted:
st.write(f"### Comparison Results for {name}")
# Calculate costs for each selected option
options_to_show = []
if show_work_lease:
options_to_show.append(('Work Lease', calculate_total_cost(car_options['Work Lease'])))
if show_purchase:
options_to_show.append(('Purchase', calculate_total_cost(car_options['Purchase'])))
if show_op_lease:
options_to_show.append(('Operational Lease', calculate_total_cost(car_options['Operational Lease'])))
# Create comparison visualizations
col1, col2 = st.columns([2, 1])
with col1:
# Bar chart comparison
fig = go.Figure()
categories = ['Initial Cost', 'Monthly Payments', 'Maintenance', 'Insurance', 'Fuel']
for option_name, costs in options_to_show:
fig.add_trace(go.Bar(
name=option_name,
x=categories,
y=[costs[cat] for cat in categories],
text=[f'₪{costs[cat]:,.0f}' for cat in categories],
textposition='auto',
))
fig.update_layout(
title='Cost Breakdown Over 5 Years',
xaxis_title='Cost Category',
yaxis_title='Amount (₪)',
barmode='group',
height=500
)
st.plotly_chart(fig, use_container_width=True)
with col2:
# Total cost comparison table
st.write("### Total Cost Over 5 Years")
comparison_data = {
'Option': [],
'Total Cost (₪)': [],
'Monthly Cost (₪)': []
}
for option_name, costs in options_to_show:
comparison_data['Option'].append(option_name)
comparison_data['Total Cost (₪)'].append(f'₪{costs["Total"]:,.0f}')
monthly_cost = costs["Total"] / (5 * 12)
comparison_data['Monthly Cost (₪)'].append(f'₪{monthly_cost:,.0f}')
comparison_df = pd.DataFrame(comparison_data)
st.table(comparison_df.set_index('Option'))
# Highlight best option based on total cost
min_cost_option = min(options_to_show, key=lambda x: x[1]['Total'])
st.success(f"💡 Based on total cost over 5 years, the most economical option is: **{min_cost_option[0]}**")
# Additional considerations
st.write("### Additional Considerations")
st.info("""
- Work Lease: Usually includes maintenance and sometimes insurance
- Purchase: You own the asset but have higher initial costs
- Operational Lease: All-inclusive but may have mileage restrictions
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?