Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,153 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_car_info(car_name):
|
| 11 |
-
"""Fetch car details
|
| 12 |
-
|
| 13 |
-
params = {"model": car_name} # Adjust according to API documentation
|
| 14 |
-
|
| 15 |
-
response = requests.get(API_URL, headers=headers, params=params)
|
| 16 |
-
|
| 17 |
-
if response.status_code == 200:
|
| 18 |
-
return response.json() # Returns car data as JSON
|
| 19 |
-
else:
|
| 20 |
-
st.error("Car data not found!")
|
| 21 |
-
return None
|
| 22 |
|
| 23 |
def display_car_details(car_name):
|
| 24 |
-
"""Display
|
| 25 |
car_details = get_car_info(car_name)
|
| 26 |
if not car_details:
|
|
|
|
| 27 |
return
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
st.write(f"**Price:** ${car_details.get('price', 'N/A')}")
|
| 31 |
-
|
| 32 |
-
# Display car image if available
|
| 33 |
if car_details.get('image_url'):
|
| 34 |
st.image(car_details['image_url'], caption=car_name, width=300)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
col1, col2 = st.columns(2)
|
| 37 |
|
| 38 |
with col1:
|
| 39 |
st.markdown("**Specifications**")
|
| 40 |
-
st.write(f"**Type:** {car_details
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with col2:
|
| 48 |
st.markdown("**Features**")
|
| 49 |
-
st.write(f"**Interior:** {car_details
|
| 50 |
-
st.write(f"**Exterior:** {car_details
|
| 51 |
st.markdown("**Genuine Parts:**")
|
| 52 |
-
for part in car_details
|
| 53 |
st.write(f"- {part}")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def main():
|
| 56 |
-
st.title("π
|
| 57 |
-
st.markdown("
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
display_car_details(selected_car)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
+
# Extended car database (for demonstration, can be linked to an API)
|
| 4 |
+
car_info = {
|
| 5 |
+
'Toyota Camry': {
|
| 6 |
+
'type': 'Petrol',
|
| 7 |
+
'fuel_efficiency': '15 km/l',
|
| 8 |
+
'interior': 'Leather seats, Touchscreen display',
|
| 9 |
+
'exterior': 'Sleek design with alloy wheels',
|
| 10 |
+
'body': 'Sedan',
|
| 11 |
+
'tyres': '205/60 R16',
|
| 12 |
+
'genuine_parts': ['Engine parts', 'Suspension', 'Brakes'],
|
| 13 |
+
'price': 25000,
|
| 14 |
+
'image_url': 'https://example.com/toyota_camry.jpg'
|
| 15 |
+
},
|
| 16 |
+
'Tesla Model S': {
|
| 17 |
+
'type': 'Electric',
|
| 18 |
+
'battery': '100 kWh',
|
| 19 |
+
'range': '370 miles',
|
| 20 |
+
'charging_time': '1 hour fast charging',
|
| 21 |
+
'interior': 'Minimalistic with touch screen dashboard',
|
| 22 |
+
'exterior': 'Futuristic design with autopilot sensors',
|
| 23 |
+
'body': 'Sedan',
|
| 24 |
+
'tyres': '255/35 R20',
|
| 25 |
+
'genuine_parts': ['Battery module', 'Motor', 'Charging port'],
|
| 26 |
+
'price': 80000,
|
| 27 |
+
'image_url': 'https://example.com/tesla_model_s.jpg'
|
| 28 |
+
},
|
| 29 |
+
'Honda Civic': {
|
| 30 |
+
'type': 'Petrol',
|
| 31 |
+
'fuel_efficiency': '17 km/l',
|
| 32 |
+
'interior': 'Fabric seats, 7-inch touchscreen',
|
| 33 |
+
'exterior': 'Sporty design with LED headlights',
|
| 34 |
+
'body': 'Sedan',
|
| 35 |
+
'tyres': '215/55 R16',
|
| 36 |
+
'genuine_parts': ['Brakes', 'Suspension', 'Steering'],
|
| 37 |
+
'price': 23000,
|
| 38 |
+
'image_url': 'https://example.com/honda_civic.jpg'
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
|
| 42 |
def get_car_info(car_name):
|
| 43 |
+
"""Fetch car details based on the car name."""
|
| 44 |
+
return car_info.get(car_name, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def display_car_details(car_name):
|
| 47 |
+
"""Display details of a single car."""
|
| 48 |
car_details = get_car_info(car_name)
|
| 49 |
if not car_details:
|
| 50 |
+
st.error("Car not found!")
|
| 51 |
return
|
| 52 |
+
|
| 53 |
+
# Display car image
|
|
|
|
|
|
|
|
|
|
| 54 |
if car_details.get('image_url'):
|
| 55 |
st.image(car_details['image_url'], caption=car_name, width=300)
|
| 56 |
|
| 57 |
+
st.subheader(f"**{car_name}**")
|
| 58 |
+
st.write(f"**Price:** ${car_details['price']}")
|
| 59 |
+
|
| 60 |
col1, col2 = st.columns(2)
|
| 61 |
|
| 62 |
with col1:
|
| 63 |
st.markdown("**Specifications**")
|
| 64 |
+
st.write(f"**Type:** {car_details['type']}")
|
| 65 |
+
if car_details['type'] == 'Petrol':
|
| 66 |
+
st.write(f"**Fuel Efficiency:** {car_details['fuel_efficiency']}")
|
| 67 |
+
else:
|
| 68 |
+
st.write(f"**Battery Capacity:** {car_details['battery']}")
|
| 69 |
+
st.write(f"**Range:** {car_details['range']}")
|
| 70 |
+
st.write(f"**Charging Time:** {car_details['charging_time']}")
|
| 71 |
+
st.write(f"**Body Type:** {car_details['body']}")
|
| 72 |
+
st.write(f"**Tyres:** {car_details['tyres']}")
|
| 73 |
|
| 74 |
with col2:
|
| 75 |
st.markdown("**Features**")
|
| 76 |
+
st.write(f"**Interior:** {car_details['interior']}")
|
| 77 |
+
st.write(f"**Exterior:** {car_details['exterior']}")
|
| 78 |
st.markdown("**Genuine Parts:**")
|
| 79 |
+
for part in car_details['genuine_parts']:
|
| 80 |
st.write(f"- {part}")
|
| 81 |
|
| 82 |
+
def compare_cars(selected_cars):
|
| 83 |
+
"""Compare multiple cars side by side."""
|
| 84 |
+
if len(selected_cars) < 2:
|
| 85 |
+
st.warning("Please select at least two cars to compare.")
|
| 86 |
+
return
|
| 87 |
+
|
| 88 |
+
st.subheader("π Car Comparison")
|
| 89 |
+
comparison_data = {car: get_car_info(car) for car in selected_cars}
|
| 90 |
+
|
| 91 |
+
col_headers = ["Feature"] + selected_cars
|
| 92 |
+
comparison_table = []
|
| 93 |
+
|
| 94 |
+
# Features to compare
|
| 95 |
+
features = ["Price", "Type", "Fuel Efficiency / Battery", "Range", "Charging Time", "Body Type", "Tyres"]
|
| 96 |
+
|
| 97 |
+
for feature in features:
|
| 98 |
+
row = [feature]
|
| 99 |
+
for car in selected_cars:
|
| 100 |
+
car_details = comparison_data[car]
|
| 101 |
+
if feature == "Price":
|
| 102 |
+
row.append(f"${car_details['price']}")
|
| 103 |
+
elif feature == "Type":
|
| 104 |
+
row.append(car_details["type"])
|
| 105 |
+
elif feature == "Fuel Efficiency / Battery":
|
| 106 |
+
row.append(car_details.get("fuel_efficiency", car_details.get("battery", "N/A")))
|
| 107 |
+
elif feature == "Range":
|
| 108 |
+
row.append(car_details.get("range", "N/A"))
|
| 109 |
+
elif feature == "Charging Time":
|
| 110 |
+
row.append(car_details.get("charging_time", "N/A"))
|
| 111 |
+
elif feature == "Body Type":
|
| 112 |
+
row.append(car_details["body"])
|
| 113 |
+
elif feature == "Tyres":
|
| 114 |
+
row.append(car_details["tyres"])
|
| 115 |
+
comparison_table.append(row)
|
| 116 |
+
|
| 117 |
+
st.table(comparison_table)
|
| 118 |
+
|
| 119 |
+
def filter_cars_by_budget(budget):
|
| 120 |
+
"""Filter cars based on user budget."""
|
| 121 |
+
filtered_cars = [car for car, details in car_info.items() if details['price'] <= budget]
|
| 122 |
+
|
| 123 |
+
if not filtered_cars:
|
| 124 |
+
st.warning("No cars found in this budget range.")
|
| 125 |
+
return
|
| 126 |
+
|
| 127 |
+
st.subheader(f"π Cars Under ${budget}")
|
| 128 |
+
for car in filtered_cars:
|
| 129 |
+
display_car_details(car)
|
| 130 |
+
st.markdown("---")
|
| 131 |
+
|
| 132 |
def main():
|
| 133 |
+
st.title("π Car Information & Comparison App")
|
| 134 |
+
st.markdown("Find details about cars, compare models, and filter based on budget!")
|
| 135 |
+
|
| 136 |
+
option = st.sidebar.radio("Choose an action", ["View Car Details", "Compare Cars", "Find Cars by Budget"])
|
| 137 |
|
| 138 |
+
if option == "View Car Details":
|
| 139 |
+
car_list = list(car_info.keys())
|
| 140 |
+
selected_car = st.selectbox("Select a car model", car_list)
|
| 141 |
display_car_details(selected_car)
|
| 142 |
|
| 143 |
+
elif option == "Compare Cars":
|
| 144 |
+
car_list = list(car_info.keys())
|
| 145 |
+
selected_cars = st.multiselect("Select cars to compare", car_list)
|
| 146 |
+
compare_cars(selected_cars)
|
| 147 |
+
|
| 148 |
+
elif option == "Find Cars by Budget":
|
| 149 |
+
budget = st.slider("Select your budget ($)", min_value=20000, max_value=100000, step=5000)
|
| 150 |
+
filter_cars_by_budget(budget)
|
| 151 |
+
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
main()
|