GHarshasri commited on
Commit
c7bea95
Β·
verified Β·
1 Parent(s): 4446743

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +23 -30
src/streamlit_app.py CHANGED
@@ -3,10 +3,10 @@ import sqlite3
3
  import os
4
 
5
  # ---------------- DATABASE SETUP ----------------
6
- DB_PATH = "telangana.db"
7
 
8
  def create_tables():
9
-
10
  cursor = conn.cursor()
11
  cursor.execute("""
12
  CREATE TABLE IF NOT EXISTS districts (
@@ -32,11 +32,11 @@ def create_tables():
32
  conn.commit()
33
  conn.close()
34
 
35
-
36
 
37
  # ---------------- FUNCTIONS ----------------
38
  def add_district(name):
39
-
40
  cursor = conn.cursor()
41
  cursor.execute("INSERT OR IGNORE INTO districts(name) VALUES(?)", (name,))
42
  conn.commit()
@@ -62,17 +62,14 @@ def add_product(category_id, product_name, description, image_bytes):
62
  conn.close()
63
 
64
  def get_all_data():
65
-
66
  cursor = conn.cursor()
67
  cursor.execute("""
68
- SELECT d.name, c.category_name,
69
- p.product_name
70
- FROM districts d
71
- JOIN categories c ON d.id =
72
- c.district_id
73
- JOIN products p ON c.id =
74
- p.category_id
75
- """)
76
  data = cursor.fetchall()
77
  conn.close()
78
  return data
@@ -85,23 +82,19 @@ menu = st.sidebar.radio("Navigation", ["Home", "Add Data", "View by District"])
85
  if menu == "Home":
86
  st.title("🌾 Telangana Famous Products")
87
  st.subheader("Explore Products by District")
88
- if data:
89
- grouped = {}
90
- for district, category, product in data:
91
- grouped.setdefault(district, {}).setdefault(category, []).append(product)
92
-
93
- for district, categories in grouped.items():
94
- with st.expander(f"πŸ“ {district}"):
95
- for category, products in categories.items():
96
- st.write(f"{category}: {', '.join(products)}")
97
-
98
  for district, categories in grouped.items():
99
  with st.expander(f"πŸ“ {district}"):
100
  for category, products in categories.items():
101
- # CORRECTED LINE BELOW:
102
  st.write(f"{category}: {', '.join(products)}")
103
- else:
104
- st.info("No data available. Please add data in 'Add Data' section.")
105
 
106
  # ---------------- ADD DATA PAGE ----------------
107
  elif menu == "Add Data":
@@ -112,15 +105,15 @@ elif menu == "Add Data":
112
  district_name = st.text_input("District Name")
113
  submitted = st.form_submit_button("Add District")
114
  if submitted and district_name:
115
-
116
  st.success(f"βœ… District '{district_name}' added!")
117
 
118
  st.write("---")
119
 
120
  # Add Category
121
  with st.form("add_category_form"):
122
-
123
-
124
  cursor.execute("SELECT name FROM districts")
125
  district_list = [row[0] for row in cursor.fetchall()]
126
  conn.close()
@@ -206,7 +199,7 @@ elif menu == "View by District":
206
  if image_data:
207
  st.image(image_data, use_column_width=True)
208
  with col2:
209
- st.write(f"{product_name}") # Bolding product name for consistency
210
  st.caption(description)
211
  st.write("---")
212
  else:
 
3
  import os
4
 
5
  # ---------------- DATABASE SETUP ----------------
6
+ DB_PATH = "/tmp/telangana.db" # Hugging Face writable directory
7
 
8
  def create_tables():
9
+ conn = sqlite3.connect(DB_PATH)
10
  cursor = conn.cursor()
11
  cursor.execute("""
12
  CREATE TABLE IF NOT EXISTS districts (
 
32
  conn.commit()
33
  conn.close()
34
 
35
+ create_tables()
36
 
37
  # ---------------- FUNCTIONS ----------------
38
  def add_district(name):
39
+ conn = sqlite3.connect(DB_PATH)
40
  cursor = conn.cursor()
41
  cursor.execute("INSERT OR IGNORE INTO districts(name) VALUES(?)", (name,))
42
  conn.commit()
 
62
  conn.close()
63
 
64
  def get_all_data():
65
+ conn = sqlite3.connect(DB_PATH)
66
  cursor = conn.cursor()
67
  cursor.execute("""
68
+ SELECT d.name, c.category_name, p.product_name
69
+ FROM districts d
70
+ JOIN categories c ON d.id = c.district_id
71
+ JOIN products p ON c.id = p.category_id
72
+ """)
 
 
 
73
  data = cursor.fetchall()
74
  conn.close()
75
  return data
 
82
  if menu == "Home":
83
  st.title("🌾 Telangana Famous Products")
84
  st.subheader("Explore Products by District")
85
+
86
+ data = get_all_data()
87
+ if data:
88
+ grouped = {}
89
+ for district, category, product in data:
90
+ grouped.setdefault(district, {}).setdefault(category, []).append(product)
91
+
 
 
 
92
  for district, categories in grouped.items():
93
  with st.expander(f"πŸ“ {district}"):
94
  for category, products in categories.items():
 
95
  st.write(f"{category}: {', '.join(products)}")
96
+ else:
97
+ st.info("No data available. Please add data in 'Add Data' section.")
98
 
99
  # ---------------- ADD DATA PAGE ----------------
100
  elif menu == "Add Data":
 
105
  district_name = st.text_input("District Name")
106
  submitted = st.form_submit_button("Add District")
107
  if submitted and district_name:
108
+ add_district(district_name)
109
  st.success(f"βœ… District '{district_name}' added!")
110
 
111
  st.write("---")
112
 
113
  # Add Category
114
  with st.form("add_category_form"):
115
+ conn = sqlite3.connect(DB_PATH)
116
+ cursor = conn.cursor()
117
  cursor.execute("SELECT name FROM districts")
118
  district_list = [row[0] for row in cursor.fetchall()]
119
  conn.close()
 
199
  if image_data:
200
  st.image(image_data, use_column_width=True)
201
  with col2:
202
+ st.write(f"**{product_name}**")
203
  st.caption(description)
204
  st.write("---")
205
  else: