Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,105 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
st.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from tariff_scraper import fetch_tariff_data, save_tariff_data
|
| 4 |
+
|
| 5 |
+
# Predefined appliance list with typical loads in watts
|
| 6 |
+
APPLIANCE_OPTIONS = {
|
| 7 |
+
"Fan": 75,
|
| 8 |
+
"Air Conditioner (1 Ton)": 1500,
|
| 9 |
+
"Air Conditioner (1.5 Ton)": 2200,
|
| 10 |
+
"Refrigerator": 150,
|
| 11 |
+
"LED Bulb (20W)": 20,
|
| 12 |
+
"Tube Light": 40,
|
| 13 |
+
"Iron": 1000,
|
| 14 |
+
"Microwave Oven": 1200,
|
| 15 |
+
"Washing Machine": 500,
|
| 16 |
+
"Electric Heater": 1500,
|
| 17 |
+
"Laptop": 50,
|
| 18 |
+
"Desktop Computer": 200,
|
| 19 |
+
"Television (LCD/LED)": 120,
|
| 20 |
+
"Water Pump": 1000,
|
| 21 |
+
"Geyser (Electric)": 3000
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Initialize session state
|
| 25 |
+
if "appliance_data" not in st.session_state:
|
| 26 |
+
st.session_state.appliance_data = []
|
| 27 |
+
if "tariff_data" not in st.session_state:
|
| 28 |
+
st.session_state.tariff_data = None
|
| 29 |
+
if "tariff_rate" not in st.session_state:
|
| 30 |
+
st.session_state.tariff_rate = None
|
| 31 |
+
|
| 32 |
+
# Function to calculate total units consumption
|
| 33 |
+
def calculate_total_units():
|
| 34 |
+
total_energy_wh = 0
|
| 35 |
+
for data in st.session_state.appliance_data:
|
| 36 |
+
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 37 |
+
return total_energy_wh / 1000 # Convert watt-hours to kilowatt-hours
|
| 38 |
+
|
| 39 |
+
# Streamlit App
|
| 40 |
+
st.title("Dynamic Tariff Electricity Bill Calculator")
|
| 41 |
+
|
| 42 |
+
# Tariff Input
|
| 43 |
+
st.sidebar.subheader("Fetch Tariff Data")
|
| 44 |
+
tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:",
|
| 45 |
+
value="https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 46 |
+
load_type = st.sidebar.selectbox("Select Load Type", ["A-1 GENERAL SUPPLY TARIFF - RESIDENTIAL",
|
| 47 |
+
"A-2 GENERAL SUPPLY TARIFF - COMMERCIAL",
|
| 48 |
+
"B - INDUSTRIAL SUPPLY TARIFFS",
|
| 49 |
+
"D - AGRICULTURE TARIFF"])
|
| 50 |
+
|
| 51 |
+
if st.sidebar.button("Fetch Tariff"):
|
| 52 |
+
try:
|
| 53 |
+
# Fetch and save tariff data using the scraper
|
| 54 |
+
tariff_data = fetch_tariff_data(tariff_url)
|
| 55 |
+
save_tariff_data(tariff_data, "tariff_data.xlsx")
|
| 56 |
+
st.session_state.tariff_data = tariff_data
|
| 57 |
+
|
| 58 |
+
# Set the tariff rate for the selected load type
|
| 59 |
+
if load_type in tariff_data:
|
| 60 |
+
df = tariff_data[load_type]
|
| 61 |
+
if "Variable Rs/kWh" in df.columns:
|
| 62 |
+
st.session_state.tariff_rate = float(df["Variable Rs/kWh"].iloc[0])
|
| 63 |
+
st.sidebar.success(f"Tariff fetched for {load_type}: {st.session_state.tariff_rate} PKR/unit")
|
| 64 |
+
else:
|
| 65 |
+
st.sidebar.error(f"Variable Rs/kWh not found in {load_type}.")
|
| 66 |
else:
|
| 67 |
+
st.sidebar.warning(f"No data available for {load_type}.")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.sidebar.error(f"Error fetching tariff data: {e}")
|
| 70 |
+
|
| 71 |
+
# Add Appliances
|
| 72 |
+
st.sidebar.subheader("Add Appliances")
|
| 73 |
+
appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys()))
|
| 74 |
+
default_load = APPLIANCE_OPTIONS[appliance]
|
| 75 |
+
quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1)
|
| 76 |
+
load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load)
|
| 77 |
+
usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0)
|
| 78 |
+
|
| 79 |
+
if st.sidebar.button("Add Appliance"):
|
| 80 |
+
st.session_state.appliance_data.append({
|
| 81 |
+
"appliance": appliance,
|
| 82 |
+
"load": load,
|
| 83 |
+
"quantity": quantity,
|
| 84 |
+
"usage_hours": usage_hours
|
| 85 |
+
})
|
| 86 |
+
st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
|
| 87 |
+
|
| 88 |
+
# Display Appliance List
|
| 89 |
+
st.subheader("Appliance List")
|
| 90 |
+
if st.session_state.appliance_data:
|
| 91 |
+
for idx, data in enumerate(st.session_state.appliance_data, start=1):
|
| 92 |
+
st.write(f"{idx}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day")
|
| 93 |
+
else:
|
| 94 |
+
st.write("No appliances added.")
|
| 95 |
|
| 96 |
+
# Calculate Monthly Units and Electricity Bill
|
| 97 |
+
if st.button("Calculate Monthly Bill"):
|
| 98 |
+
if not st.session_state.tariff_rate:
|
| 99 |
+
st.warning("Please fetch tariff data first.")
|
| 100 |
+
else:
|
| 101 |
+
total_units_kwh = calculate_total_units()
|
| 102 |
+
monthly_units_kwh = total_units_kwh * 30 # Assuming 30 days in a month
|
| 103 |
+
monthly_bill = monthly_units_kwh * st.session_state.tariff_rate
|
| 104 |
+
st.subheader("Electricity Bill")
|
| 105 |
+
st.write(f"**Total Monthly Bill: PKR {monthly_bill:.2f}**")
|