forked from adityarao1612/TimeTableManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontend.py
More file actions
1023 lines (807 loc) · 35.3 KB
/
Frontend.py
File metadata and controls
1023 lines (807 loc) · 35.3 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import random
import mysql.connector
import time
import pandas as pd
# Establish a connection to MySQL database
try:
conn = mysql.connector.connect(
host="localhost",
user="root",
password="ajey@123",
database="timetablemanager"
)
except:
conn = mysql.connector.connect(
host="localhost",
user="root",
password="adi1416",
database="timetablemanager"
)
cursor = conn.cursor()
# check admin username and password:
def authenticate(username, password):
query = f"SELECT * FROM admin WHERE username = '{username}' AND password = '{password}'"
cursor.execute(query)
result = cursor.fetchone()
return result is not None
def admin_dashboard():
st.title("Admin Dashboard")
st.write("Welcome,", st.session_state.user[0])
# Add buttons
selected_option = st.radio("Select an option:", [
"Edit Student", "Edit Teacher","upload timetable"])
if selected_option == "Edit Student":
edit_students()
elif selected_option == "Edit Teacher":
edit_teachers()
elif selected_option == "upload timetable":
upload_timetable()
if st.button("Logout", key="logout", on_click=lambda: st.rerun()):
st.session_state.logged_in = False
st.session_state.page = "admin_login"
st.rerun()
def upload_timetable():
st.title("Upload Timetable")
st.write("Welcome,", st.session_state.user[0])
# Add buttons
selected_option = st.radio("Select an option:", [
"Upload Student", "Upload Teacher", "Upload Subject", "Upload Room", "Upload Timeslot"])
if selected_option == "Upload Student":
upload_student()
elif selected_option == "Upload Teacher":
upload_teacher()
elif selected_option == "Upload Subject":
upload_subject()
elif selected_option == "Upload Room":
upload_room()
elif selected_option == "Upload Timeslot":
upload_timeslot()
def upload_student():
# upload csv file which contains student details
st.title("Upload Student")
st.write("Welcome,", st.session_state.user[0])
# display example format df as in csv
df = pd.DataFrame(columns=['studentid', 'name', 'semester', 'section', 'batchid'])
df.loc[0] = ['1BM18CS001', 'Aditya', 5, 'A', '5A']
df.loc[1] = ['1BM18CS002', 'Ajey', 5, 'A', '5A']
st.dataframe(df)
# upload csv file
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
# print(df)
for index, row in df.iterrows():
# print(row['studentid'], row['name'], row['semester'], row['section'], row['batchid'])
query = f"INSERT INTO Student VALUES ('{row['studentid']}', '{row['name']}', {row['semester']}, '{row['section']}', '{row['batchid']}')"
# print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Student added successfully!")
def upload_teacher():
# upload csv file which contains teacher details
st.title("Upload Teacher")
st.write("Welcome,", st.session_state.user[0])
# display example format df as in csv
df = pd.DataFrame(columns=['teacherid', 'name', 'dept', 'email'])
df.loc[0] = ['1BM18CS001', 'Aditya', 'CSE', 'abc@gmail.com']
st.dataframe(df)
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
# print(df)
for index, row in df.iterrows():
# print(row['teacherid'], row['name'], row['dept'], row['email'])
query = f"INSERT INTO Teacher VALUES ('{row['teacherid']}', '{row['name']}', '{row['dept']}', '{row['email']}')"
# print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Teacher added successfully!")
def upload_subject():
# upload csv file which contains subject details
st.title("Upload Subject")
st.write("Welcome,", st.session_state.user[0])
# display example format df as in csv
df = pd.DataFrame(columns=['subjectcode', 'subjectname', 'semester'])
df.loc[0] = ['CS101', 'Computer Science', 5]
df.loc[1] = ['CS102', 'Computer Science', 5]
st.dataframe(df)
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
# print(df)
# NSERT INTO Subject (subjectcode, subjectname, semester
for index, row in df.iterrows():
# print(row['subjectcode'], row['subjectname'], row['semester'])
query = f"INSERT INTO Subject VALUES ('{row['subjectcode']}', '{row['subjectname']}', {row['semester']})"
# print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Subject added successfully!")
def upload_room():
# upload csv file which contains room details
st.title("Upload Room")
st.write("Welcome,", st.session_state.user[0])
# display example format df as in csv
df = pd.DataFrame(columns=['room_id', 'capacity'])
df.loc[0] = ['CS101', 40]
df.loc[1] = ['CS102', 40]
st.dataframe(df)
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
# print(df)
# INSERT INTO Classroom (room_id, capacity) VALUES
for index, row in df.iterrows():
# print(row['room_id'], row['capacity'])
query = f"INSERT INTO Classroom VALUES ('{row['room_id']}', {row['capacity']})"
# print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Room added successfully!")
def upload_timeslot():
# upload csv file which contains timeslot details
st.title("Upload Timeslot")
st.write("Welcome,", st.session_state.user[0])
# display example format df as in csv
df = pd.DataFrame(columns=['slot_id', 'room_id', 'batch_id', 'subjectcode', 'day', 'starttime', 'endtime'])
df.loc[0] = [1, 'CS101', '5A', 'CS101', 'Monday', '09:00:00', '10:00:00']
df.loc[1] = [2, 'CS102', '5A', 'CS102', 'Monday', '10:00:00', '11:00:00']
# upload teaches csv of teaches
# uploaded_teaches_file = st.file_uploader("upload teaches a file")
# st.dataframe(df)
uploaded_file = st.file_uploader("upload timeslot a file")
if uploaded_file is not None:
dft = pd.read_csv(uploaded_file)
# INSERT INTO Timeslot (slot_id, room_id, batch_id, subjectcode, day, starttime, endtime) VALUES
if st.button("upload") :
for index, row in dft.iterrows():
query = f"INSERT INTO Timeslot VALUES ({row['slot_id']}, '{row['room_id']}', '{row['batch_id']}', '{row['subjectcode']}', '{row['day']}', '{row['starttime']}', '{row['endtime']}')"
# print("SQL Query:", query)
query1 = f"INSERT INTO updatedtables VALUES ({row['slot_id']}, '{row['room_id']}', '{row['batch_id']}', '{row['subjectcode']}', '{row['day']}', '{row['starttime']}', '{row['endtime']}')"
cursor.execute(query)
conn.commit()
cursor.execute(query1)
conn.commit()
st.success("Timeslot added successfully!")
def edit_students():
st.subheader("Edit Students")
# Functionality Toggle Buttons
selected_option = st.radio("Select Action", [
"Add Student", "Remove Student", "Update Student", "View student details"])
if selected_option == "Add Student":
add_student()
elif selected_option == "Remove Student":
remove_student()
elif selected_option == "Update Student":
update_student()
elif selected_option == "View student details":
view_student()
def view_student():
st.title("View Student Details")
rollno = st.text_input("SRN")
if st.button("View Student Details"):
query = f"SELECT * FROM Student WHERE studentid = '{rollno}'"
cursor.execute(query)
result = cursor.fetchone()
if result:
# Create DataFrame directly
df = pd.DataFrame([result], columns=['studentid',
'studentname', 'semester', 'section', 'batchid'])
st.dataframe(df, hide_index=True)
else:
st.error("No entry found.")
def add_student():
st.title("Add Student")
name = st.text_input("Name")
id = st.text_input("SRN")
semester = st.number_input("Semester", min_value=1, max_value=10, step=1)
section = st.text_input("Section", max_chars=1)
batchid = str(semester) + section
if st.button("Add Student"):
try:
query = f"INSERT INTO Student VALUES ('{id}', '{name}', {semester}, '{section}', '{batchid}')"
print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Student added successfully!")
# Clear input values after successful addition
name = ""
id = ""
semester = 1
section = ""
except Exception as e:
st.error(f"An error occurred: {e}")
def remove_student():
st.title("Remove Student")
rollno = st.text_input("SRN")
if st.button("Remove Student"):
query = f"DELETE FROM Student WHERE studentid = '{rollno}'"
cursor.execute(query)
conn.commit()
st.success("Student removed successfully!")
def update_student():
st.title("Update Student")
rollno = st.text_input("SRN")
name = st.text_input("Name")
batchid = st.text_input("Batch ID")
try:
if st.button("Update Student"):
query = f"UPDATE Student SET name = '{name}',semester={int(batchid[0])},section='{batchid[1]}', batchid = '{batchid}' WHERE studentid = '{rollno}'"
cursor.execute(query)
conn.commit()
st.success("Student updated successfully!")
except Exception as error:
st.error(error)
# Add functionality to edit student information in the database
# You can use st.text_input, st.button, and other Streamlit components to create the form
def edit_teachers():
st.subheader("Edit Teachers")
# Add functionality to edit teacher information in the database
# You can use st.text_input, st.button, and other Streamlit components to create the form
operation = st.radio("Select Action", [
"Add Teacher", "Remove Teacher", "Update Teacher", "View teacher details", "leave"])
if operation == "Add Teacher":
add_teacher()
elif operation == "Remove Teacher":
remove_teacher()
elif operation == "Update Teacher":
update_teacher()
elif operation == "View teacher details":
view_teacher()
elif operation == "leave":
leave()
def leave():
st.title("Leave Management")
# Sidebar menu for selecting leave operation
leave_operation = st.radio("Select Leave Operation", [
"Add Leave", "Delete Leave"])
if leave_operation == "Add Leave":
add_leave()
elif leave_operation == "Delete Leave":
delete_leave()
def add_leave():
st.subheader("Add Leave")
teacher_id = st.text_input("Teacher ID")
# Menu dropdown for selecting the day on leave
days = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
]
wday = st.selectbox("Select the day on leave:", days)
# Button to confirm leave
if st.button("Confirm Leave"):
process_leave(teacher_id, wday)
def display_teachers_on_leave():
# Fetch data from LeaveTable to display teachers on leave
query = "SELECT teacher_id, leave_day FROM LeaveTable"
cursor.execute(query)
leave_data = cursor.fetchall()
if leave_data:
st.subheader("Teachers on Leave:")
leave_df = pd.DataFrame(leave_data, columns=[
"Teacher ID", "Leave Day"])
st.table(leave_df, hide_index=True)
else:
st.subheader("No teachers currently on leave.")
def delete_leave():
st.subheader("Delete Leave")
display_teachers_on_leave()
teacher_id = st.text_input("Teacher ID")
# Menu dropdown for selecting the day to delete leave
days = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
]
wday = st.selectbox("Select the day to delete leave:", days)
# Call the stored procedure
# Button to confirm leave deletion
if st.button("Confirm Leave Deletion"):
cursor.callproc("AfterLeaveInsertProcedure", (teacher_id, wday, '5A'))
conn.commit()
process_leave_deletion(teacher_id, wday)
def process_leave(teacher_id, wday):
# Insert the teacher into the 'LeaveTable'
insert_leave_query = f"""
INSERT INTO LeaveTable (teacher_id, leave_day)
VALUES ('{teacher_id}', '{wday}');
"""
cursor.execute(insert_leave_query)
conn.commit()
st.success(f"Leave for Teacher {teacher_id} on {wday} processed successfully.")
def process_leave_deletion(teacher_id, wday):
# Delete the teacher's leave from the 'LeaveTable'
delete_leave_query = f"""
DELETE FROM LeaveTable
WHERE teacher_id = '{teacher_id}' AND leave_day = '{wday}';
"""
cursor.execute(delete_leave_query)
conn.commit()
st.success(f"Leave deletion for Teacher {teacher_id} on {wday} processed successfully.")
def display_teachers_on_leave():
# Fetch data from LeaveTable to display teachers on leave
query = "SELECT teacher_id, leave_day FROM LeaveTable"
cursor.execute(query)
leave_data = cursor.fetchall()
if leave_data:
st.subheader("Teachers on Leave:")
leave_df = pd.DataFrame(leave_data, columns=[
"Teacher ID", "Leave Day"])
st.table(leave_df)
else:
st.subheader("No teachers currently on leave.")
def add_teacher():
st.title("Add Teacher")
name = st.text_input("Name")
teacher_id = st.text_input("Teacher ID")
department = st.text_input("Department")
email = st.text_input("Email")
if st.button("Add Teacher"):
try:
query = f"INSERT INTO Teacher VALUES ('{teacher_id}', '{name}', '{department}', '{email}')"
cursor.execute(query)
conn.commit()
st.success("Teacher added successfully!")
# Clear input values after successful addition
name = ""
teacher_id = ""
department = ""
email = ""
except Exception as e:
st.error(f"An error occurred: {e}")
def remove_teacher():
st.title("Remove Teacher")
teacher_id = st.text_input("Teacher ID")
if st.button("Remove Teacher"):
try:
query = f"DELETE FROM Teacher WHERE teacherid = '{teacher_id}'"
cursor.execute(query)
conn.commit()
st.success("Teacher removed successfully!")
except Exception as e:
st.error(f"An error occurred: {e}")
def update_teacher():
st.title("Update Teacher")
teacher_id = st.text_input("Teacher ID")
name = st.text_input("Name")
department = st.text_input("Department")
email = st.text_input("Email")
if st.button("Update Teacher"):
try:
query = f"UPDATE Teacher SET name = '{name}',dept = '{department}', email '{email}' WHERE teacherid = '{teacher_id}')"
cursor.execute(query)
conn.commit()
st.success("Teacher updated successfully!")
except Exception as e:
st.error(f"An error occurred: {e}")
def view_teacher():
st.title("View Teacher Details")
teacher_id = st.text_input("Teacher ID")
if st.button("View Teacher Details"):
query = f"SELECT * FROM Teacher WHERE teacherid = '{teacher_id}'"
cursor.execute(query)
result = cursor.fetchone()
if result:
# Create DataFrame directly
df = pd.DataFrame([result], columns=[
'teacherid', 'name', 'dept', 'email'])
st.dataframe(df, hide_index=True)
# group by:
query = f"SELECT teacherid,count(*) FROM Teaches group by teacherid having teacherid = '{teacher_id}'"
cursor.execute(query)
result = cursor.fetchone()
# Create DataFrame directly
# df = pd.DataFrame([result], columns=[
# 'teacherid', 'count of classes teaching'])
# s = pd.DataFrame
st.write(result[0], "teaches", str(result[1]), "batches")
else:
st.error("No entry found.")
def edit_batches():
st.subheader("Edit Batches")
# Add functionality to edit batch information in the database
# You can use st.text_input, st.button, and other Streamlit components to create the form
def view_timetable():
if st.session_state.logged_in:
# display all tables
st.title("View Tables")
# radio button to select table
selected_option = st.radio("Select Table", [
"Student", "Teacher", "Subject", "Room", "Timetable"])
if selected_option == "Student":
student_details()
elif selected_option == "Teacher":
# add drop down menu box for batch
# display student table according to batch
query = f"SELECT * FROM Teacher"
cursor.execute(query)
result = cursor.fetchall()
df = pd.DataFrame(result, columns=[ 'teacherid', 'name', 'dept', 'email'])
st.dataframe(df, hide_index=True)
elif selected_option == "Subject":
# add drop down menu box for batch
# display student table according to batch
query = f"SELECT * FROM Subject"
cursor.execute(query)
result = cursor.fetchall()
df = pd.DataFrame(result, columns=[ 'subjectcode', 'subjectname', 'semester'])
st.dataframe(df, hide_index=True)
elif selected_option == "Room":
# add drop down menu box for batch
# display student table according to batch
query = f"SELECT * FROM Classroom"
cursor.execute(query)
result = cursor.fetchall()
df = pd.DataFrame(result, columns=[ 'room_id', 'capacity'])
st.dataframe(df, hide_index=True)
elif selected_option == "Timetable":
uid = st.text_input("Enter Student ID or BatchID")
if uid:
st.session_state.timetable_id = uid
if st.session_state.timetable_id:
st.write("Timetable for ", st.session_state.timetable_id)
batch = False
# Original Timetable Query
if len(st.session_state.timetable_id) > 12:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM timeslot t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id IN
(SELECT batchid FROM Student WHERE studentid = '{st.session_state.timetable_id}')
"""
elif len(st.session_state.timetable_id) > 10:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM (SELECT * FROM timeslot
NATURAL JOIN teaches
WHERE timeslot.subjectcode = teaches.subjectcode
AND batchid = batch_id AND teacherid='{st.session_state.timetable_id}') AS t
NATURAL JOIN subject s
WHERE t.subjectcode = s.subjectcode
"""
else:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM timeslot t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id = '{st.session_state.timetable_id}'
"""
batch = st.session_state.timetable_id
cursor.execute(original_query)
original_result = cursor.fetchall()
updated_result = 0
# Updated Timetable Query
if len(st.session_state.timetable_id) > 12:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM UpdatedTables
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id IN
(SELECT batchid FROM Student WHERE studentid = '{st.session_state.timetable_id}')
"""
# NESTED AND JOIN
elif len(st.session_state.timetable_id) > 10:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM (SELECT * FROM UpdatedTables
NATURAL JOIN teaches
WHERE UpdatedTables.subjectcode = teaches.subjectcode
AND batchid = batch_id AND teacherid='{st.session_state.timetable_id}') AS t
NATURAL JOIN subject s
WHERE t.subjectcode = s.subjectcode
"""
else:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM UpdatedTables t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id = '{st.session_state.timetable_id}'
"""
cursor.execute(updated_query)
updated_result = cursor.fetchall()
print(updated_result)
if original_result or updated_result:
st.write("Original Timetable:")
updt = display_timetable(original_result, batch, "original")
if updated_result:
st.write("\n Updated Timetable:")
updt = display_timetable(updated_result, batch, "updated")
# updt = display_timetable(original_result, batch)
if st.button("update"):
print("\n\n\n\n\n-------\n\n\n")
# print(updt)
update_table(updt, batch)
else:
st.error("No entry found.")
else:
st.title("View TimeTable")
uid = st.text_input("Enter Student ID or BatchID")
if uid:
st.session_state.timetable_id = uid
if st.session_state.timetable_id:
st.write("Timetable for ", st.session_state.timetable_id)
batch = False
# Original Timetable Query
if len(st.session_state.timetable_id) > 12:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM timeslot t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id IN
(SELECT batchid FROM Student WHERE studentid = '{st.session_state.timetable_id}')
"""
elif len(st.session_state.timetable_id) > 10:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM (SELECT * FROM timeslot
NATURAL JOIN teaches
WHERE timeslot.subjectcode = teaches.subjectcode
AND batchid = batch_id AND teacherid='{st.session_state.timetable_id}') AS t
NATURAL JOIN subject s
WHERE t.subjectcode = s.subjectcode
"""
else:
original_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM timeslot t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id = '{st.session_state.timetable_id}'
"""
batch = st.session_state.timetable_id
cursor.execute(original_query)
original_result = cursor.fetchall()
updated_result = 0
# Updated Timetable Query
if len(st.session_state.timetable_id) > 12:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM UpdatedTables
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id IN
(SELECT batchid FROM Student WHERE studentid = '{st.session_state.timetable_id}')
"""
# NESTED AND JOIN
elif len(st.session_state.timetable_id) > 10:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM (SELECT * FROM UpdatedTables
NATURAL JOIN teaches
WHERE UpdatedTables.subjectcode = teaches.subjectcode
AND batchid = batch_id AND teacherid='{st.session_state.timetable_id}') AS t
NATURAL JOIN subject s
WHERE t.subjectcode = s.subjectcode
"""
else:
updated_query = f"""
SELECT t.day, t.starttime, t.endtime, t.subjectcode, t.room_id, s.subjectname, t.batch_id
FROM UpdatedTables t
JOIN subject s ON t.subjectcode = s.subjectcode
WHERE t.batch_id = '{st.session_state.timetable_id}'
"""
cursor.execute(updated_query)
updated_result = cursor.fetchall()
print(updated_result)
if original_result or updated_result:
st.write("Original Timetable:")
updt = display_timetable(original_result, batch, "original")
if updated_result:
st.write("\n Updated Timetable:")
updt = display_timetable(updated_result, batch, "updated")
# updt = display_timetable(original_result, batch)
if st.button("update"):
print("\n\n\n\n\n-------\n\n\n")
# print(updt)
update_table(updt, batch)
else:
st.error("No entry found.")
def student_details():
# add drop down menu box for batch
batches = st.selectbox("Select Batch", [
"5A", "5B", "5C", "5D", "5E", "5F"])
# display student table according to batch
query = f"SELECT * FROM Student WHERE batchid = '{batches}'"
cursor.execute(query)
result = cursor.fetchall()
df = pd.DataFrame(result, columns=[
'studentid', 'studentname', 'semester', 'section', 'batchid'])
st.dataframe(df, hide_index=True)
def update_table(updated_vals, batchid):
# # Assuming updated_vals is a list of dictionaries
# # Example: [{'day': 'Monday', 'timing': '09:00-10:00', 'alloted': 'Subject [Room] (Batch)'}]
# tuples_list = [tuple(x)
# for x in updated_vals.itertuples(index=False, name=None)]
# # column_names_tuple = tuple(updated_vals.columns)
# # timeslot = column_names_tuple[1:]
# # print(timeslot)
# # print(tuples_list)
# slotid = 1
# for day, *values in tuples_list:
# for subject in values:
# if subject:
# try:
# subjectNAME, room_id = subject.split(' [')
# room_id = room_id[0:-1]
# # print(tuple((slotid, subjectcode, room_id)))
# # print(subject.split('['))
# query = f"""
# SELECT subjectcode from subject where subject.subjectname = '{subjectNAME}'
# """
# # print(batchid)
# cursor.execute(query)
# # print(query)
# subjectcode = cursor.fetchone()[0]
# # conn.commit()
# print(subjectcode)
# query = f"""
# UPDATE UpdatedTables
# SET room_id = '{room_id}',
# subjectcode = '{subjectcode}'
# WHERE slot_id = {slotid} AND batch_id = '{batchid}';
# """
# # print(batchid)
# cursor.execute(query)
# # print(query)
# conn.commit()
# except Exception as error:
# st.error(error)
# slotid = slotid+1
# st.success("Student updated successfully!")
tuples_list = [tuple(x)
for x in updated_vals.itertuples(index=False, name=None)]
# column_names_tuple = tuple(updated_vals.columns)
# timeslot = column_names_tuple[1:]
# print(timeslot)
# print(tuples_list)
slotid = 1
for day, *values in tuples_list:
for subject in values:
if subject:
try:
subjectNAME, room_id = subject.split(' [')
room_id = room_id[0:-1]
# print(tuple((slotid, subjectcode, room_id)))
# print(subject.split('['))
query = f"""
SELECT subjectcode from subject where subject.subjectname = '{subjectNAME}'
"""
# print(batchid)
cursor.execute(query)
# print(query)
subjectcode = cursor.fetchone()[0]
# conn.commit()
# print(subjectcode)
query = f"""
Select slot_id from UpdatedTables
WHERE batch_id = '{batchid}';
"""
cursor.execute(query)
exists = cursor.fetchall()
existingid = [x[0] for x in exists]
print(slotid in existingid, slotid)
if not (slotid in existingid):
query = f"""
INSERT INTO UpdatedTables (slot_id, room_id, batch_id, subjectcode, day, starttime, endtime)
SELECT *
FROM Timeslot ts
WHERE ts.batch_id = '{batchid}' and ts.slot_id={slotid};
"""
cursor.execute(query)
conn.commit()
query = f"""
UPDATE UpdatedTables
SET room_id = '{room_id}',
subjectcode = '{subjectcode}'
WHERE slot_id = {slotid} AND batch_id = '{batchid}';
"""
# print(batchid)
cursor.execute(query)
# print(query)
conn.commit()
except Exception as error:
st.error(error)
slotid = slotid+1
st.success("Student updated successfully!")
def display_timetable(result, batch, s):
# Convert the result to a Pandas DataFrame
df = pd.DataFrame(result, columns=[
'day', 'starttime', 'endtime', 'subjectcode', 'room_id', 'subjectname', 'batch_id'])
# Display the DataFrame
df['starttime'] = df['starttime'].astype(str)
df['endtime'] = df['endtime'].astype(str)
df['Timing'] = df['starttime'].str[-8:-3] + '-' + df['endtime'].str[-8:-3]
day_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
df['day'] = pd.Categorical(
df['day'], categories=day_order, ordered=True)
if len(st.session_state.timetable_id) == 11:
df['alloted'] = df['subjectname'] + \
" [" + df['room_id'] + ']' + '(' + df['batch_id'] + ')'
else:
df['alloted'] = df['subjectname'] + " [" + df['room_id'] + ']'
timetable = df.pivot(index='day', columns='Timing', values='alloted')
timetable = timetable.reset_index()
# Increase row height using custom CSS
if not st.session_state.logged_in:
st.dataframe(timetable, hide_index=True)
else:
updated_vals = st.data_editor(
timetable, key='data_editor'+s, hide_index=True)
# st.write(updated_vals)
return updated_vals
def signup(username, password):
# conn = mysql.connector.connect(
# host="localhost",
# user="root",
# password="adi1416",
# database="timetablemanager"
# )
query = f"""
INSERT INTO admin (username, password, role) VALUES
('{username}','{password}', 'admin');')"""
# print("SQL Query:", query)
cursor.execute(query)
conn.commit()
st.success("Student added successfully!")
def main():
st.set_page_config(layout="wide")
st.sidebar.title("Navigation")
if 'user' not in st.session_state:
st.session_state.user = []
if 'timetable_id' not in st.session_state:
st.session_state.timetable_id = ""
if 'logged_in' not in st.session_state:
st.session_state.logged_in = False
if 'page' not in st.session_state:
st.session_state.page = "Home"
def set_page(page):
st.session_state.page = page
if st.session_state.page == 'admin_login':
st.title("Admin Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
col1, col2 = st.columns([0.1, 0.6])
with col2:
if st.button("Login"):
if authenticate(username, password):
st.session_state.logged_in = True
st.session_state.user.append(username)
st.session_state.page = "dashboard"
st.rerun()
else:
st.error("Invalid credentials. Please try again.")
with col1:
st.button("Signup", on_click=set_page, args=['signup'])
elif st.session_state.page == "signup":
st.title("User Signup")
username = st.text_input("New Username")
password = st.text_input("New Password", type="password")
secret_code = st.text_input("Secret Code", type="password")
if st.button("Signup"):
if secret_code == "secret":
# print("SQL Query:", query)
signup(username, password)
st.success("Signup successful! You can now login.")
st.session_state.signup_page = False
st.session_state.logged_in = False