Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ def calculate_bill(units_consumed, tariff_data):
|
|
| 22 |
threshold = int(unit_range.strip(">").strip())
|
| 23 |
if units_consumed > threshold:
|
| 24 |
return units_consumed * rate
|
| 25 |
-
except:
|
| 26 |
pass
|
| 27 |
return 0.0 # Default to 0 if no matching rate is found
|
| 28 |
|
|
@@ -30,24 +30,23 @@ def main():
|
|
| 30 |
st.set_page_config(page_title="Electricity Bill Calculator", layout="wide")
|
| 31 |
st.title("Electricity Bill Calculator")
|
| 32 |
|
| 33 |
-
# Sidebar for
|
| 34 |
with st.sidebar:
|
| 35 |
st.header("Settings")
|
| 36 |
-
|
| 37 |
-
#
|
| 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.
|
| 43 |
-
|
| 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 |
-
#
|
| 51 |
st.subheader("Add Appliances")
|
| 52 |
appliances = {
|
| 53 |
"Fan": 75, # Power in watts
|
|
@@ -58,18 +57,19 @@ def main():
|
|
| 58 |
"Microwave": 1200
|
| 59 |
}
|
| 60 |
|
| 61 |
-
# Allow users to
|
| 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
|
| 71 |
st.header("Appliance Usage")
|
| 72 |
-
st.write("
|
| 73 |
|
| 74 |
total_units = 0
|
| 75 |
for appliance in selected_appliances:
|
|
@@ -84,14 +84,17 @@ def main():
|
|
| 84 |
|
| 85 |
# Calculate the bill
|
| 86 |
if st.button("Calculate Bill"):
|
| 87 |
-
if 'tariff_data' in
|
| 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
|
| 93 |
else:
|
| 94 |
st.error("Please fetch the tariff rates first.")
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 97 |
main()
|
|
|
|
| 22 |
threshold = int(unit_range.strip(">").strip())
|
| 23 |
if units_consumed > threshold:
|
| 24 |
return units_consumed * rate
|
| 25 |
+
except Exception:
|
| 26 |
pass
|
| 27 |
return 0.0 # Default to 0 if no matching rate is found
|
| 28 |
|
|
|
|
| 30 |
st.set_page_config(page_title="Electricity Bill Calculator", layout="wide")
|
| 31 |
st.title("Electricity Bill Calculator")
|
| 32 |
|
| 33 |
+
# Sidebar for settings
|
| 34 |
with st.sidebar:
|
| 35 |
st.header("Settings")
|
| 36 |
+
|
| 37 |
+
# Company selection
|
| 38 |
st.subheader("Select Company")
|
| 39 |
selected_company = st.selectbox("Company", options=list(TARIFF_URLS.keys()))
|
| 40 |
+
|
| 41 |
if st.button("Fetch Tariff Rates", key="fetch_tariff"):
|
| 42 |
url = TARIFF_URLS[selected_company]
|
| 43 |
+
st.session_state['tariff_data'] = scrape_tariff_data(url)
|
| 44 |
+
if st.session_state['tariff_data']:
|
|
|
|
|
|
|
| 45 |
st.success(f"Tariff data fetched for {selected_company}!")
|
| 46 |
else:
|
| 47 |
+
st.error("Failed to fetch tariff data. Please check the URL or try again.")
|
| 48 |
|
| 49 |
+
# Appliances selection
|
| 50 |
st.subheader("Add Appliances")
|
| 51 |
appliances = {
|
| 52 |
"Fan": 75, # Power in watts
|
|
|
|
| 57 |
"Microwave": 1200
|
| 58 |
}
|
| 59 |
|
| 60 |
+
# Allow users to select predefined or add custom appliances
|
| 61 |
selected_appliances = st.multiselect("Select Appliances", options=list(appliances.keys()))
|
| 62 |
custom_appliance_name = st.text_input("Custom Appliance Name")
|
| 63 |
custom_appliance_power = st.number_input("Custom Appliance Power (Watts)", min_value=1, step=1)
|
| 64 |
|
| 65 |
+
# Add custom appliances
|
| 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 application for usage inputs and bill calculation
|
| 71 |
st.header("Appliance Usage")
|
| 72 |
+
st.write("Enter usage details for the selected appliances:")
|
| 73 |
|
| 74 |
total_units = 0
|
| 75 |
for appliance in selected_appliances:
|
|
|
|
| 84 |
|
| 85 |
# Calculate the bill
|
| 86 |
if st.button("Calculate Bill"):
|
| 87 |
+
if 'tariff_data' in st.session_state and st.session_state['tariff_data']:
|
| 88 |
+
bill = calculate_bill(total_units, st.session_state['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.")
|
| 93 |
else:
|
| 94 |
st.error("Please fetch the tariff rates first.")
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
# Initialize session state to store tariff data
|
| 98 |
+
if 'tariff_data' not in st.session_state:
|
| 99 |
+
st.session_state['tariff_data'] = None
|
| 100 |
main()
|