Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- 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 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
grouped
|
92 |
-
|
93 |
-
|
94 |
-
|
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 |
-
|
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}")
|
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:
|