Engineer786 commited on
Commit
bdc1d95
·
verified ·
1 Parent(s): 736f4af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -14
app.py CHANGED
@@ -1,24 +1,42 @@
1
  import streamlit as st
2
- from tariff_scraper import TARIFF_URLS, scrape_tariff_data
 
3
 
4
  def main():
5
- st.title("IESCO Tariff Rates Viewer")
6
- st.write("This app fetches and displays the latest IESCO tariff rates.")
7
 
8
- # Display the fetch button
9
- if st.button("Fetch IESCO Tariff Rates"):
10
- url = TARIFF_URLS["IESCO"]
11
- st.write(f"Fetching data from {url}...")
12
 
 
 
 
 
13
  with st.spinner("Scraping data..."):
14
- data = scrape_tariff_data(url)
15
- if data and isinstance(data, list) and len(data) > 0:
16
- st.success("Data fetched successfully!")
17
- st.write("### Tariff Rates:")
18
- for row in data:
19
- st.write(row)
20
  else:
21
- st.error("Failed to fetch tariff data or no data found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  if __name__ == "__main__":
24
  main()
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ from tariff_scraper import TARIFF_URLS, scrape_tariff_data_to_csv
4
 
5
  def main():
6
+ st.title("Electricity Tariff Data Viewer")
7
+ st.sidebar.header("Tariff Data Scraper")
8
 
9
+ # Dropdown for selecting a company
10
+ selected_company = st.sidebar.selectbox("Select a Company", options=list(TARIFF_URLS.keys()))
11
+ st.sidebar.write("Selected Company:", selected_company)
 
12
 
13
+ # Button to fetch and scrape data
14
+ if st.sidebar.button("Fetch Tariff Data"):
15
+ url = TARIFF_URLS[selected_company]
16
+ st.sidebar.write(f"Fetching data from: {url}")
17
  with st.spinner("Scraping data..."):
18
+ result = scrape_tariff_data_to_csv(url, output_file=f"{selected_company}_tariff_data.csv")
19
+ if result.endswith(".csv"):
20
+ st.sidebar.success(f"Data successfully saved to {result}")
21
+ st.session_state["csv_file"] = result
 
 
22
  else:
23
+ st.sidebar.error(f"Error: {result}")
24
+
25
+ # Display the preview of the data if available
26
+ if "csv_file" in st.session_state and st.session_state["csv_file"]:
27
+ st.subheader("Preview of Tariff Data")
28
+ try:
29
+ csv_file = st.session_state["csv_file"]
30
+ data = pd.read_csv(csv_file)
31
+ st.write(data)
32
+ st.download_button(
33
+ label="Download Tariff Data as CSV",
34
+ data=open(csv_file, "rb").read(),
35
+ file_name=csv_file,
36
+ mime="text/csv"
37
+ )
38
+ except Exception as e:
39
+ st.error(f"An error occurred while loading the CSV file: {e}")
40
 
41
  if __name__ == "__main__":
42
  main()