Engineer786 commited on
Commit
f4575cc
·
verified ·
1 Parent(s): 84559a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -26
app.py CHANGED
@@ -1,36 +1,88 @@
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()
 
1
  import streamlit as st
2
  from tariff_scraper import TARIFF_URLS, scrape_tariff_data
3
 
4
+ def calculate_bill(units_consumed, tariff_data):
5
+ """
6
+ Calculate the bill based on units consumed and tariff rates.
7
+ Assumes the tariff data contains rates per unit in a structured format.
8
+ """
9
+ for row in tariff_data:
10
+ if "to" in row.lower(): # Check if the row contains range information
11
+ try:
12
+ parts = row.split("|")
13
+ unit_range = parts[0].strip()
14
+ rate = float(parts[-1].strip()) # Assuming rate is the last column
15
+
16
+ # Parse the unit range (e.g., "1-100" or ">300")
17
+ if "-" in unit_range:
18
+ low, high = map(int, unit_range.split("-"))
19
+ if low <= units_consumed <= high:
20
+ return units_consumed * rate
21
+ elif ">" in unit_range:
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
+
29
  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("Select Company")
36
+ selected_company = st.selectbox("Company", options=list(TARIFF_URLS.keys()))
37
+ st.write("Selected Company:", selected_company)
38
 
39
+ # Fetch tariff data from the selected company
40
  url = TARIFF_URLS[selected_company]
41
+ with st.spinner("Fetching tariff data..."):
42
+ tariff_data = scrape_tariff_data(url)
43
+
44
+ # Main app interface for bill calculation
45
+ st.header("Appliances and Usage")
46
+ st.write("Add your appliances and usage details below:")
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
+ # Allow users to add appliances and customize
59
+ selected_appliances = st.multiselect("Select Appliances", options=list(appliances.keys()))
60
+ custom_appliance_name = st.text_input("Add Custom Appliance Name")
61
+ custom_appliance_power = st.number_input("Custom Appliance Power (Watts)", min_value=1, step=1)
62
+
63
+ if custom_appliance_name and custom_appliance_power:
64
+ appliances[custom_appliance_name] = custom_appliance_power
65
+ st.success(f"Added custom appliance: {custom_appliance_name} ({custom_appliance_power}W)")
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)")
71
+ num_units = st.number_input(f"How many {appliance}s?", min_value=1, step=1, key=f"{appliance}_units")
72
+ usage_hours = st.number_input(f"Daily usage hours for {appliance}:", min_value=1, step=1, key=f"{appliance}_hours")
73
+ monthly_units = (appliances[appliance] / 1000) * num_units * usage_hours * 30 # Convert watts to kWh
74
+ total_units += monthly_units
75
+ st.write(f"Estimated Monthly Units for {appliance}: {monthly_units:.2f} kWh")
76
+
77
+ st.write("### Total Estimated Monthly Units Consumed:", f"{total_units:.2f} kWh")
78
+
79
+ # Calculate the bill
80
+ if st.button("Calculate Bill"):
81
+ bill = calculate_bill(total_units, tariff_data)
82
+ if bill > 0:
83
+ st.success(f"Estimated Monthly Bill: Rs. {bill:.2f}")
84
+ else:
85
+ st.error("Could not calculate the bill. Please check the tariff data or try again.")
86
 
87
  if __name__ == "__main__":
88
  main()