Engineer786 commited on
Commit
31e2234
·
verified ·
1 Parent(s): 6df4b6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -3,25 +3,27 @@ from tariff_scraper import scrape_tariff_data
3
 
4
  def main():
5
  st.title("Electricity Bill Calculator with Tariff Scraper")
6
- st.sidebar.subheader("Tariff Data Scraper")
7
-
8
- # Tariff Scraper Section
9
- st.sidebar.write("Enter the URL of the electricity tariff page:")
10
- url = st.sidebar.text_input("Tariff URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
11
 
12
- if st.sidebar.button("Scrape Tariff Data"):
13
- if url:
14
- with st.spinner("Scraping data..."):
15
- data = scrape_tariff_data(url)
16
- if isinstance(data, list):
17
- st.success("Tariff data fetched successfully!")
18
- st.write("### Scraped Tariff Data")
19
- for row in data[:10]: # Display first 10 rows
20
- st.write(row)
21
- else:
22
- st.error(data)
23
- else:
24
- st.sidebar.error("Please provide a valid URL.")
 
 
 
 
 
 
 
25
 
26
  # Electricity Bill Calculator Section
27
  st.write("## Electricity Bill Calculator")
@@ -31,7 +33,7 @@ def main():
31
  "Fan": 75, "Air Conditioner (1 Ton)": 1500, "Refrigerator": 150,
32
  "LED Bulb (20W)": 20, "Iron": 1000, "Microwave Oven": 1200
33
  }
34
- tariff_rate = st.sidebar.number_input("Tariff Rate (PKR/unit)", min_value=0.0, value=20.0)
35
  appliance = st.sidebar.selectbox("Select Appliance", options=list(appliances.keys()))
36
  quantity = st.sidebar.number_input("Quantity", min_value=1, value=1)
37
  usage_hours = st.sidebar.number_input("Usage Hours/Day", min_value=1.0, value=6.0)
@@ -60,7 +62,7 @@ def main():
60
  app["quantity"] * app["load"] * app["usage_hours"] / 1000 for app in st.session_state["appliances"]
61
  )
62
  bill = total_units * 30 * tariff_rate
63
- st.success(f"Estimated Monthly Bill: PKR {bill:.2f}")
64
  else:
65
  st.warning("No appliances added for calculation.")
66
 
 
3
 
4
  def main():
5
  st.title("Electricity Bill Calculator with Tariff Scraper")
 
 
 
 
 
6
 
7
+ # Default URL for scraping tariff rates
8
+ url = "https://iesco.com.pk/index.php/customer-services/tariff-guide"
9
+
10
+ # Scrape tariff rates
11
+ tariff_rate = 0.0
12
+ with st.spinner("Fetching tariff rates..."):
13
+ data = scrape_tariff_data(url)
14
+ if isinstance(data, list) and data:
15
+ # Extract tariff rate from the scraped data
16
+ # Assuming tariff rate is mentioned as a numeric value in PKR/unit
17
+ for row in data:
18
+ if "PKR" in row: # Replace with specific logic matching the structure of the scraped data
19
+ try:
20
+ tariff_rate = float(row.split()[1]) # Adjust this parsing logic based on your data
21
+ break
22
+ except (ValueError, IndexError):
23
+ continue
24
+ if tariff_rate == 0.0:
25
+ st.warning("Could not fetch tariff rates. Using default rate of PKR 20.0/unit.")
26
+ tariff_rate = 20.0 # Default rate
27
 
28
  # Electricity Bill Calculator Section
29
  st.write("## Electricity Bill Calculator")
 
33
  "Fan": 75, "Air Conditioner (1 Ton)": 1500, "Refrigerator": 150,
34
  "LED Bulb (20W)": 20, "Iron": 1000, "Microwave Oven": 1200
35
  }
36
+
37
  appliance = st.sidebar.selectbox("Select Appliance", options=list(appliances.keys()))
38
  quantity = st.sidebar.number_input("Quantity", min_value=1, value=1)
39
  usage_hours = st.sidebar.number_input("Usage Hours/Day", min_value=1.0, value=6.0)
 
62
  app["quantity"] * app["load"] * app["usage_hours"] / 1000 for app in st.session_state["appliances"]
63
  )
64
  bill = total_units * 30 * tariff_rate
65
+ st.success(f"Estimated Monthly Bill: PKR {bill:.2f} (Tariff Rate: PKR {tariff_rate:.2f}/unit)")
66
  else:
67
  st.warning("No appliances added for calculation.")
68