Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,41 +30,47 @@ def main():
|
|
| 30 |
st.set_page_config(page_title="Electricity Bill Calculator", layout="wide")
|
| 31 |
st.title("Electricity Bill Calculator")
|
| 32 |
|
| 33 |
-
# Sidebar for company selection
|
| 34 |
with st.sidebar:
|
| 35 |
-
st.header("
|
|
|
|
|
|
|
|
|
|
| 36 |
selected_company = st.selectbox("Company", options=list(TARIFF_URLS.keys()))
|
| 37 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Predefined appliances
|
| 49 |
-
appliances = {
|
| 50 |
-
"Fan": 75, # Power in watts
|
| 51 |
-
"Bulb": 40,
|
| 52 |
-
"Refrigerator": 150,
|
| 53 |
-
"Air Conditioner": 1500,
|
| 54 |
-
"Washing Machine": 500,
|
| 55 |
-
"Microwave": 1200
|
| 56 |
-
}
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
custom_appliance_power = st.number_input("Custom Appliance Power (Watts)", min_value=1, step=1)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
# Input fields for usage details
|
| 68 |
total_units = 0
|
| 69 |
for appliance in selected_appliances:
|
| 70 |
st.subheader(f"{appliance} ({appliances[appliance]}W)")
|
|
@@ -78,11 +84,14 @@ def main():
|
|
| 78 |
|
| 79 |
# Calculate the bill
|
| 80 |
if st.button("Calculate Bill"):
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
-
st.error("
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
main()
|
|
|
|
| 30 |
st.set_page_config(page_title="Electricity Bill Calculator", layout="wide")
|
| 31 |
st.title("Electricity Bill Calculator")
|
| 32 |
|
| 33 |
+
# Sidebar for company and appliance selection
|
| 34 |
with st.sidebar:
|
| 35 |
+
st.header("Settings")
|
| 36 |
+
|
| 37 |
+
# Select company
|
| 38 |
+
st.subheader("Select Company")
|
| 39 |
selected_company = st.selectbox("Company", options=list(TARIFF_URLS.keys()))
|
| 40 |
+
if st.button("Fetch Tariff Rates", key="fetch_tariff"):
|
| 41 |
+
url = TARIFF_URLS[selected_company]
|
| 42 |
+
st.write(f"Fetching data for {selected_company}...")
|
| 43 |
+
with st.spinner("Fetching tariff data..."):
|
| 44 |
+
tariff_data = scrape_tariff_data(url)
|
| 45 |
+
if tariff_data:
|
| 46 |
+
st.success(f"Tariff data fetched for {selected_company}!")
|
| 47 |
+
else:
|
| 48 |
+
st.error("Failed to fetch tariff data.")
|
| 49 |
|
| 50 |
+
# Appliance selection
|
| 51 |
+
st.subheader("Add Appliances")
|
| 52 |
+
appliances = {
|
| 53 |
+
"Fan": 75, # Power in watts
|
| 54 |
+
"Bulb": 40,
|
| 55 |
+
"Refrigerator": 150,
|
| 56 |
+
"Air Conditioner": 1500,
|
| 57 |
+
"Washing Machine": 500,
|
| 58 |
+
"Microwave": 1200
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
# Allow users to add predefined or custom appliances
|
| 62 |
+
selected_appliances = st.multiselect("Select Appliances", options=list(appliances.keys()))
|
| 63 |
+
custom_appliance_name = st.text_input("Custom Appliance Name")
|
| 64 |
+
custom_appliance_power = st.number_input("Custom Appliance Power (Watts)", min_value=1, step=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
if custom_appliance_name and custom_appliance_power:
|
| 67 |
+
appliances[custom_appliance_name] = custom_appliance_power
|
| 68 |
+
st.success(f"Added custom appliance: {custom_appliance_name} ({custom_appliance_power}W)")
|
|
|
|
| 69 |
|
| 70 |
+
# Main app interface for bill calculation
|
| 71 |
+
st.header("Appliance Usage")
|
| 72 |
+
st.write("Add usage details for the selected appliances:")
|
| 73 |
|
|
|
|
| 74 |
total_units = 0
|
| 75 |
for appliance in selected_appliances:
|
| 76 |
st.subheader(f"{appliance} ({appliances[appliance]}W)")
|
|
|
|
| 84 |
|
| 85 |
# Calculate the bill
|
| 86 |
if st.button("Calculate Bill"):
|
| 87 |
+
if 'tariff_data' in locals() and tariff_data:
|
| 88 |
+
bill = calculate_bill(total_units, tariff_data)
|
| 89 |
+
if bill > 0:
|
| 90 |
+
st.success(f"Estimated Monthly Bill: Rs. {bill:.2f}")
|
| 91 |
+
else:
|
| 92 |
+
st.error("Could not calculate the bill. Please check the tariff data or try again.")
|
| 93 |
else:
|
| 94 |
+
st.error("Please fetch the tariff rates first.")
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
main()
|