Engineer786 commited on
Commit
84e68d2
·
verified ·
1 Parent(s): 683037f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -56
app.py CHANGED
@@ -1,59 +1,105 @@
 
1
  import streamlit as st
2
- import pandas as pd
3
- import tariff_scraper
4
-
5
- # UI Components
6
- def main():
7
- st.sidebar.title("Electricity Tariff App")
8
- st.sidebar.header("Navigation")
9
-
10
- # Button to Fetch Tariff Data
11
- if st.sidebar.button("Fetch Tariff Data"):
12
- url = "https://iesco.com.pk/index.php/customer-services/tariff-guide"
13
- try:
14
- tariff_data = tariff_scraper.fetch_tariff_data(url)
15
- tariff_scraper.save_tariff_data(tariff_data, "tariff_data.xlsx")
16
- st.success("Tariff data fetched and saved successfully!")
17
- except Exception as e:
18
- st.error(f"Error: {e}")
19
-
20
- # Dropdown for Load Type
21
- tariff_data_file = "tariff_data.xlsx"
22
- if st.sidebar.button("Load Tariff Data"):
23
- try:
24
- tariffs = pd.ExcelFile(tariff_data_file)
25
- sections = tariffs.sheet_names
26
- selected_section = st.sidebar.selectbox("Select Tariff Section", sections)
27
-
28
- df = tariffs.parse(selected_section)
29
- st.write(f"### {selected_section}")
30
- st.dataframe(df)
31
- except Exception as e:
32
- st.error(f"Error: {e}")
33
-
34
- st.header("Electricity Bill Calculator")
35
- appliances = []
36
-
37
- # Appliance Input Section
38
- col1, col2, col3 = st.columns(3)
39
- with col1:
40
- appliance_name = st.text_input("Appliance Name")
41
- with col2:
42
- appliance_count = st.number_input("Number of Appliances", min_value=1, step=1)
43
- with col3:
44
- usage_hours = st.number_input("Usage Hours per Day", min_value=0.0, step=0.5)
45
-
46
- if st.button("Add Appliance"):
47
- appliances.append((appliance_name, appliance_count, usage_hours))
48
- st.write(f"Added {appliance_name}: {appliance_count} appliances used {usage_hours} hours/day.")
49
-
50
- # Calculate Bill
51
- if st.button("Calculate Bill"):
52
- if not appliances:
53
- st.error("Please add appliances before calculating.")
 
 
 
 
 
 
 
 
 
 
 
54
  else:
55
- total_units = sum(appliance_count * usage_hours * 30 for _, appliance_count, usage_hours in appliances)
56
- st.write(f"Total Units Consumed: {total_units} kWh")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- if __name__ == "__main__":
59
- main()
 
 
 
 
 
 
 
 
 
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}**")