Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,36 @@
|
|
| 1 |
-
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from tariff_scraper import
|
| 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 |
-
# Function to calculate total units consumption
|
| 34 |
-
def calculate_total_units():
|
| 35 |
-
total_energy_wh = 0
|
| 36 |
-
for data in st.session_state.appliance_data:
|
| 37 |
-
total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
|
| 38 |
-
return total_energy_wh / 1000 # Convert watt-hours to kilowatt-hours
|
| 39 |
-
|
| 40 |
-
# Streamlit App
|
| 41 |
-
st.title("Dynamic Tariff Electricity Bill Calculator")
|
| 42 |
-
|
| 43 |
-
# Tariff Input
|
| 44 |
-
st.sidebar.subheader("Fetch Tariff Data")
|
| 45 |
-
tariff_url = st.sidebar.text_input("Enter URL to scrape tariff data:",
|
| 46 |
-
value="https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 47 |
-
load_type = st.sidebar.selectbox("Select Load Type", ["A-1 GENERAL SUPPLY TARIFF - RESIDENTIAL",
|
| 48 |
-
"A-2 GENERAL SUPPLY TARIFF - COMMERCIAL",
|
| 49 |
-
"B - INDUSTRIAL SUPPLY TARIFFS",
|
| 50 |
-
"D - AGRICULTURE TARIFF"])
|
| 51 |
-
|
| 52 |
-
if st.sidebar.button("Fetch Tariff"):
|
| 53 |
-
try:
|
| 54 |
-
# Fetch and save tariff data using the scraper
|
| 55 |
-
tariff_data = fetch_tariff_data(tariff_url)
|
| 56 |
-
save_tariff_data(tariff_data, "tariff_data.xlsx")
|
| 57 |
-
st.session_state.tariff_data = tariff_data
|
| 58 |
-
|
| 59 |
-
# Set the tariff rate for the selected load type
|
| 60 |
-
if load_type in tariff_data:
|
| 61 |
-
df = tariff_data[load_type]
|
| 62 |
-
if "Variable Rs/kWh" in df.columns:
|
| 63 |
-
# Filter numeric values only
|
| 64 |
-
df_numeric = df[pd.to_numeric(df["Variable Rs/kWh"], errors="coerce").notnull()]
|
| 65 |
-
if not df_numeric.empty:
|
| 66 |
-
st.session_state.tariff_rate = float(df_numeric["Variable Rs/kWh"].iloc[0])
|
| 67 |
-
st.sidebar.success(f"Tariff fetched for {load_type}: {st.session_state.tariff_rate} PKR/unit")
|
| 68 |
-
else:
|
| 69 |
-
st.sidebar.warning(f"No numeric tariff rates found for {load_type}.")
|
| 70 |
else:
|
| 71 |
-
st.
|
| 72 |
-
else:
|
| 73 |
-
st.sidebar.warning(f"No data available for {load_type}.")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
st.sidebar.error(f"Error fetching tariff data: {e}")
|
| 76 |
-
|
| 77 |
-
# Add Appliances
|
| 78 |
-
st.sidebar.subheader("Add Appliances")
|
| 79 |
-
appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys()))
|
| 80 |
-
default_load = APPLIANCE_OPTIONS[appliance]
|
| 81 |
-
quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1)
|
| 82 |
-
load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load)
|
| 83 |
-
usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0)
|
| 84 |
-
|
| 85 |
-
if st.sidebar.button("Add Appliance"):
|
| 86 |
-
st.session_state.appliance_data.append({
|
| 87 |
-
"appliance": appliance,
|
| 88 |
-
"load": load,
|
| 89 |
-
"quantity": quantity,
|
| 90 |
-
"usage_hours": usage_hours
|
| 91 |
-
})
|
| 92 |
-
st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
|
| 93 |
-
|
| 94 |
-
# Display Appliance List
|
| 95 |
-
st.subheader("Appliance List")
|
| 96 |
-
if st.session_state.appliance_data:
|
| 97 |
-
for idx, data in enumerate(st.session_state.appliance_data, start=1):
|
| 98 |
-
st.write(f"{idx}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day")
|
| 99 |
-
else:
|
| 100 |
-
st.write("No appliances added.")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if not st.session_state.tariff_rate:
|
| 105 |
-
st.warning("Please fetch tariff data first.")
|
| 106 |
-
else:
|
| 107 |
-
total_units_kwh = calculate_total_units()
|
| 108 |
-
monthly_units_kwh = total_units_kwh * 30 # Assuming 30 days in a month
|
| 109 |
-
monthly_bill = monthly_units_kwh * st.session_state.tariff_rate
|
| 110 |
-
st.subheader("Electricity Bill")
|
| 111 |
-
st.write(f"**Total Monthly Bill: PKR {monthly_bill:.2f}**")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from tariff_scraper import TARIFF_URLS, scrape_tariff_data
|
| 3 |
+
|
| 4 |
+
def main():
|
| 5 |
+
st.title("Electricity Tariff Scraper")
|
| 6 |
+
st.write("Select a company to fetch tariff data:")
|
| 7 |
+
|
| 8 |
+
# Dropdown menu for selecting the company
|
| 9 |
+
selected_company = st.selectbox("Select Company", options=list(TARIFF_URLS.keys()))
|
| 10 |
+
|
| 11 |
+
if st.button("Fetch Tariff Rates"):
|
| 12 |
+
url = TARIFF_URLS[selected_company]
|
| 13 |
+
st.write(f"Fetching data from {selected_company} ({url})...")
|
| 14 |
+
|
| 15 |
+
with st.spinner("Scraping data..."):
|
| 16 |
+
data = scrape_tariff_data(url)
|
| 17 |
+
if data:
|
| 18 |
+
st.success(f"Data scraped successfully for {selected_company}!")
|
| 19 |
+
st.write("### Preview of Tariff Data:")
|
| 20 |
+
st.write("Here is the scraped data (showing the first 10 rows):")
|
| 21 |
+
for row in data[:10]: # Show only the first 10 rows for readability
|
| 22 |
+
st.write(row)
|
| 23 |
+
st.write("### Download Full Data:")
|
| 24 |
+
if st.button("Download as Text"):
|
| 25 |
+
full_data = "\n".join(data)
|
| 26 |
+
st.download_button(
|
| 27 |
+
label="Download Tariff Data",
|
| 28 |
+
data=full_data,
|
| 29 |
+
file_name=f"{selected_company}_tariff_data.txt",
|
| 30 |
+
mime="text/plain"
|
| 31 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
+
st.error("Failed to scrape data or no data available.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|