DigitalTransG2 commited on
Commit
e527dca
·
verified ·
1 Parent(s): efc5ba7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -52
app.py CHANGED
@@ -45,20 +45,36 @@ if role == "Quiz Creator":
45
 
46
  quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")
47
 
48
- # Tạo 10 câu hỏi
 
 
49
  questions = []
50
  for i in range(1, 11):
51
  st.markdown(f"### Câu hỏi {i}")
52
  q_text = st.text_input(f"Nội dung câu hỏi {i}", key=f"q{i}")
 
53
  options = []
54
  for j in range(1, 5):
55
  opt = st.text_input(f"Đáp án {j}", key=f"q{i}_opt{j}")
56
  options.append(opt)
57
- correct = st.selectbox(f"Đáp án đúng của câu {i}", ["1", "2", "3", "4"], key=f"q{i}_correct")
58
- questions.append({"question": q_text, "options": options, "correct": int(correct) - 1})
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  if st.button("Lưu Quiz"):
61
  valid = all(q["question"] and all(q["options"]) for q in questions)
 
62
  if not quiz_name:
63
  st.error("Nhập tên quiz")
64
  elif not valid:
@@ -74,52 +90,63 @@ if role == "Quiz Creator":
74
  if role == "Student Evaluation":
75
  st.subheader("Bảng đánh giá học sinh")
76
 
77
- # Lấy tất cả học sinh từ Firebase
78
- res = requests.get(f"{FIREBASE_URL}/student_quizzes.json?auth={FIREBASE_AUTH}")
79
- students = res.json() if res.status_code == 200 else {}
80
-
81
- if not students:
82
- st.info("Chưa có học sinh nào.")
83
- else:
84
- # Tạo danh sách các dòng cho DataFrame
85
- rows = []
86
- for student_id, data in students.items():
87
- name = data.get("Name", "")
88
- class_ = data.get("Class", "")
89
- for key, quiz_data in data.items():
90
- if key.startswith("Quiz"):
91
- # Kiểm tra kiểu dữ liệu quiz_data
92
- if isinstance(quiz_data, str):
93
- # Dữ liệu cũ, chỉ có Score
94
- quiz_data = {"Score": quiz_data, "Time": ""}
95
- score = quiz_data.get("Score", "")
96
- time = quiz_data.get("Time", "")
97
- rows.append({
98
- "ID": student_id,
99
- "Name": name,
100
- "Class": class_,
101
- "Quiz": key,
102
- "Score": score,
103
- "Time": time
104
- })
105
-
106
- df = pd.DataFrame(rows)
107
- st.subheader("Danh sách học sinh và quiz")
108
- st.dataframe(df)
109
-
110
- # Nhập Feedback
111
- st.subheader("Nhập Feedback cho từng quiz")
112
- for student_id, data in students.items():
113
- for key, quiz_data in data.items():
114
- if key.startswith("Quiz"):
115
- # Chuyển str -> dict nếu dữ liệu cũ
116
- if isinstance(quiz_data, str):
117
- quiz_data = {"Score": quiz_data, "Time": ""}
118
- st.markdown(f"**ID: {student_id} - {data.get('Name','')} - {key}**")
119
- # Lấy feedback hiện có nếu có
120
- existing_fb = quiz_data.get("feedback", "")
121
- feedback = st.text_area("Feedback", value=existing_fb, key=f"fb_{student_id}_{key}")
122
- if st.button(f"Gửi Feedback cho {student_id} - {key}", key=f"submit_fb_{student_id}_{key}"):
123
- url_fb = f"{FIREBASE_URL}/student_quizzes/{student_id}/{key}/feedback.json?auth={FIREBASE_AUTH}"
124
- requests.put(url_fb, json=feedback)
125
- st.success(f"Đã gửi feedback cho {student_id} - {key}")
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")
47
 
48
+ # ==========================
49
+ # 10 CÂU HỎI — ĐÁP ÁN ĐÚNG THEO TEXT
50
+ # ==========================
51
  questions = []
52
  for i in range(1, 11):
53
  st.markdown(f"### Câu hỏi {i}")
54
  q_text = st.text_input(f"Nội dung câu hỏi {i}", key=f"q{i}")
55
+
56
  options = []
57
  for j in range(1, 5):
58
  opt = st.text_input(f"Đáp án {j}", key=f"q{i}_opt{j}")
59
  options.append(opt)
 
 
60
 
61
+ # Chọn đáp án đúng bằng TEXT
62
+ correct = st.selectbox(
63
+ f"Đáp án đúng của câu {i} (theo nội dung text)",
64
+ options,
65
+ key=f"q{i}_correct"
66
+ )
67
+
68
+ questions.append({
69
+ "question": q_text,
70
+ "options": options,
71
+ "correct": correct
72
+ })
73
+
74
+ # Lưu quiz
75
  if st.button("Lưu Quiz"):
76
  valid = all(q["question"] and all(q["options"]) for q in questions)
77
+
78
  if not quiz_name:
79
  st.error("Nhập tên quiz")
80
  elif not valid:
 
90
  if role == "Student Evaluation":
91
  st.subheader("Bảng đánh giá học sinh")
92
 
93
+ # Lấy all student quizzes
94
+ res = requests.get(f"{FIREBASE_URL}/student_quizzes.json?auth={FIREBASE_AUTH}")
95
+ students = res.json() if res.status_code == 200 else {}
96
+
97
+ if not students:
98
+ st.info("Chưa có học sinh nào.")
99
+ else:
100
+ rows = []
101
+
102
+ # ==========================
103
+ # TẠO BẢNG DATAFRAME
104
+ # ==========================
105
+ for student_id, data in students.items():
106
+ name = data.get("Name", "")
107
+ class_ = data.get("Class", "")
108
+
109
+ for key, quiz_data in data.items():
110
+ if key.startswith("Quiz"):
111
+ # Xử lý dữ liệu cũ dạng string
112
+ if isinstance(quiz_data, str):
113
+ quiz_data = {"Score": quiz_data, "Time": ""}
114
+
115
+ score = quiz_data.get("Score", "")
116
+ time = quiz_data.get("Time", "")
117
+
118
+ rows.append({
119
+ "ID": student_id,
120
+ "Name": name,
121
+ "Class": class_,
122
+ "Quiz": key,
123
+ "Score": score,
124
+ "Time": time
125
+ })
126
+
127
+ df = pd.DataFrame(rows)
128
+ st.subheader("Danh sách h���c sinh và quiz")
129
+ st.dataframe(df)
130
+
131
+ # ==========================
132
+ # NHẬP FEEDBACK
133
+ # ==========================
134
+ st.subheader("Nhập Feedback cho từng quiz")
135
+
136
+ for student_id, data in students.items():
137
+ for key, quiz_data in data.items():
138
+ if key.startswith("Quiz"):
139
+
140
+ # Chuyển dữ liệu cũ
141
+ if isinstance(quiz_data, str):
142
+ quiz_data = {"Score": quiz_data, "Time": ""}
143
+
144
+ st.markdown(f"**ID: {student_id} - {data.get('Name','')} - {key}**")
145
+
146
+ existing_fb = quiz_data.get("feedback", "")
147
+ feedback = st.text_area("Feedback", value=existing_fb, key=f"fb_{student_id}_{key}")
148
+
149
+ if st.button(f"Gửi Feedback cho {student_id} - {key}", key=f"submit_fb_{student_id}_{key}"):
150
+ url_fb = f"{FIREBASE_URL}/student_quizzes/{student_id}/{key}/feedback.json?auth={FIREBASE_AUTH}"
151
+ requests.put(url_fb, json=feedback)
152
+ st.success(f"Đã gửi feedback cho {student_id} - {key}")