Spaces:
Build error
Build error
Create main_app.py
Browse files- main_app.py +68 -0
main_app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
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")
|
| 28 |
+
st.sidebar.subheader("Appliance Usage")
|
| 29 |
+
|
| 30 |
+
appliances = {
|
| 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)
|
| 38 |
+
|
| 39 |
+
if st.sidebar.button("Add Appliance"):
|
| 40 |
+
st.session_state.setdefault("appliances", []).append({
|
| 41 |
+
"appliance": appliance,
|
| 42 |
+
"load": appliances[appliance],
|
| 43 |
+
"quantity": quantity,
|
| 44 |
+
"usage_hours": usage_hours
|
| 45 |
+
})
|
| 46 |
+
st.sidebar.success(f"{quantity} x {appliance} added.")
|
| 47 |
+
|
| 48 |
+
# Display Appliances
|
| 49 |
+
st.write("### Selected Appliances")
|
| 50 |
+
if "appliances" in st.session_state:
|
| 51 |
+
for app in st.session_state["appliances"]:
|
| 52 |
+
st.write(f"{app['quantity']} x {app['appliance']} ({app['usage_hours']} hours/day)")
|
| 53 |
+
else:
|
| 54 |
+
st.write("No appliances added yet.")
|
| 55 |
+
|
| 56 |
+
# Calculate and Display Bill
|
| 57 |
+
if st.button("Calculate Bill"):
|
| 58 |
+
if "appliances" in st.session_state:
|
| 59 |
+
total_units = sum(
|
| 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 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|