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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -108
app.py CHANGED
@@ -1,111 +1,36 @@
1
- import os
2
  import streamlit as st
3
- from tariff_scraper import fetch_tariff_data, save_tariff_data
4
- import pandas as pd
5
-
6
- # Predefined appliance list with typical loads in watts
7
- APPLIANCE_OPTIONS = {
8
- "Fan": 75,
9
- "Air Conditioner (1 Ton)": 1500,
10
- "Air Conditioner (1.5 Ton)": 2200,
11
- "Refrigerator": 150,
12
- "LED Bulb (20W)": 20,
13
- "Tube Light": 40,
14
- "Iron": 1000,
15
- "Microwave Oven": 1200,
16
- "Washing Machine": 500,
17
- "Electric Heater": 1500,
18
- "Laptop": 50,
19
- "Desktop Computer": 200,
20
- "Television (LCD/LED)": 120,
21
- "Water Pump": 1000,
22
- "Geyser (Electric)": 3000
23
- }
24
-
25
- # Initialize session state
26
- if "appliance_data" not in st.session_state:
27
- st.session_state.appliance_data = []
28
- if "tariff_data" not in st.session_state:
29
- st.session_state.tariff_data = None
30
- if "tariff_rate" not in st.session_state:
31
- st.session_state.tariff_rate = None
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.sidebar.error(f"Variable Rs/kWh column not found in {load_type}.")
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
- # Calculate Monthly Units and Electricity Bill
103
- if st.button("Calculate Monthly Bill"):
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()