-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschool_app.py
More file actions
309 lines (214 loc) · 7.1 KB
/
Copy pathschool_app.py
File metadata and controls
309 lines (214 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import streamlit as st
import pandas as pd
import sqlite3
import re
# =========================================================
# PAGE CONFIG
# =========================================================
st.set_page_config(
page_title="School Management System",
page_icon="🏫",
layout="centered"
)
# =========================================================
# DATABASE CONNECTION
# =========================================================
def get_connection():
return sqlite3.connect("school.db")
conn = get_connection()
cursor = conn.cursor()
# =========================================================
# CREATE TABLE
# =========================================================
cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Age INTEGER NOT NULL,
Standard INTEGER NOT NULL,
Contact TEXT NOT NULL
)
""")
conn.commit()
# =========================================================
# TITLE
# =========================================================
st.title("🏫 School Management System")
st.write("Manage student records easily")
# =========================================================
# SIDEBAR MENU
# =========================================================
menu = st.sidebar.selectbox(
"Select Option",
[
"New Admission",
"View Students",
"Search Student",
"Update Student",
"Delete Student"
]
)
# =========================================================
# NEW ADMISSION
# =========================================================
if menu == "New Admission":
st.header("📝 New Student Admission")
name = st.text_input("Enter Student Name")
age = st.number_input(
"Enter Age",
min_value=5,
max_value=18,
step=1
)
standard = st.selectbox(
"Select Standard",
list(range(1, 13))
)
contact = st.text_input(
"Enter Guardian Contact Number"
)
if st.button("Register Student"):
pattern = r'^[6-9]\d{9}$'
# VALIDATIONS
if not name.strip():
st.error("Student name cannot be empty")
elif not all(i.isalpha() for i in name.split()):
st.error("Only alphabets are allowed in name")
elif not re.match(pattern, contact):
st.error(
"Invalid contact number. Must start from 6-9 and contain 10 digits"
)
else:
# INSERT DATA INTO DATABASE
cursor.execute("""
INSERT INTO students(Name, Age, Standard, Contact)
VALUES (?, ?, ?, ?)
""", (name.title(), age, standard, contact))
conn.commit()
st.success("✅ Student Registered Successfully")
# =========================================================
# VIEW STUDENTS
# =========================================================
elif menu == "View Students":
st.header("📋 All Student Records")
df = pd.read_sql_query(
"SELECT * FROM students",
conn
)
if not df.empty:
st.dataframe(
df,
use_container_width=True
)
else:
st.warning("No student records found")
# =========================================================
# SEARCH STUDENT
# =========================================================
elif menu == "Search Student":
st.header("🔍 Search Student By ID")
search_id = st.number_input(
"Enter Student ID",
min_value=1,
step=1
)
if st.button("Search"):
cursor.execute("""
SELECT * FROM students
WHERE ID = ?
""", (search_id,))
student = cursor.fetchone()
if student:
st.success("Student Found")
st.write(f"### ID: {student[0]}")
st.write(f"**Name:** {student[1]}")
st.write(f"**Age:** {student[2]}")
st.write(f"**Standard:** {student[3]}")
st.write(f"**Contact:** {student[4]}")
else:
st.error("Student Not Found")
# =========================================================
# UPDATE STUDENT
# =========================================================
elif menu == "Update Student":
st.header("✏️ Update Student Information")
update_id = st.number_input(
"Enter Student ID",
min_value=1,
step=1
)
if st.button("Find Student"):
cursor.execute("""
SELECT * FROM students
WHERE ID = ?
""", (update_id,))
student = cursor.fetchone()
if student:
st.success("Student Found")
st.write("### Current Record")
st.write({
"ID": student[0],
"Name": student[1],
"Age": student[2],
"Standard": student[3],
"Contact": student[4]
})
# STORE ID IN SESSION
st.session_state.update_id = update_id
else:
st.error("Student ID Not Found")
# SHOW UPDATE FORM
if "update_id" in st.session_state:
new_standard = st.selectbox(
"Update Standard",
list(range(1, 13))
)
new_contact = st.text_input(
"Update Contact Number"
)
if st.button("Update Student"):
pattern = r'^[6-9]\d{9}$'
if not re.match(pattern, new_contact):
st.error("Invalid Contact Number")
else:
cursor.execute("""
UPDATE students
SET Standard = ?, Contact = ?
WHERE ID = ?
""", (
new_standard,
new_contact,
st.session_state.update_id
))
conn.commit()
st.success("✅ Student Updated Successfully")
del st.session_state.update_id
# =========================================================
# DELETE STUDENT
# =========================================================
elif menu == "Delete Student":
st.header("🗑️ Delete Student Record")
delete_id = st.number_input(
"Enter Student ID",
min_value=1,
step=1
)
if st.button("Delete Student"):
cursor.execute("""
SELECT * FROM students
WHERE ID = ?
""", (delete_id,))
student = cursor.fetchone()
if student:
cursor.execute("""
DELETE FROM students
WHERE ID = ?
""", (delete_id,))
conn.commit()
st.success("✅ Student Deleted Successfully")
else:
st.error("Student ID Not Found")
# =========================================================
# CLOSE DATABASE CONNECTION
# =========================================================
conn.close()