Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
# ==========================
|
| 6 |
-
#
|
| 7 |
# ==========================
|
| 8 |
FIREBASE_URL = "https://smartedu-3d5bf-default-rtdb.asia-southeast1.firebasedatabase.app"
|
| 9 |
|
| 10 |
# ==========================
|
| 11 |
-
#
|
| 12 |
# ==========================
|
| 13 |
if "master_logged_in" not in st.session_state:
|
| 14 |
st.session_state.master_logged_in = False
|
|
@@ -24,7 +23,7 @@ st.title("SmartEdu Quiz System")
|
|
| 24 |
role = st.selectbox("Chọn vai trò", ["Master", "Học sinh"])
|
| 25 |
|
| 26 |
# ==========================
|
| 27 |
-
# MASTER
|
| 28 |
# ==========================
|
| 29 |
if role == "Master":
|
| 30 |
st.subheader("Đăng nhập Master")
|
|
@@ -32,29 +31,29 @@ if role == "Master":
|
|
| 32 |
if not st.session_state.master_logged_in:
|
| 33 |
username = st.text_input("Tài khoản")
|
| 34 |
password = st.text_input("Mật khẩu", type="password")
|
| 35 |
-
|
| 36 |
if st.button("Đăng nhập"):
|
| 37 |
if username == "master" and password == "master":
|
| 38 |
st.session_state.master_logged_in = True
|
| 39 |
st.success("Đăng nhập thành công!")
|
| 40 |
-
st.stop() #
|
| 41 |
else:
|
| 42 |
st.error("Sai tài khoản hoặc mật khẩu")
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
# Dashboard for Master
|
| 46 |
st.success("Bạn đang đăng nhập với quyền Master")
|
| 47 |
-
|
| 48 |
st.subheader("Tạo Quiz mới")
|
|
|
|
| 49 |
quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")
|
| 50 |
question = st.text_area("Câu hỏi")
|
| 51 |
correct_answer = st.text_input("Đáp án đúng")
|
| 52 |
|
| 53 |
if st.button("Lưu Quiz"):
|
| 54 |
if quiz_name and question and correct_answer:
|
| 55 |
-
url = f"{FIREBASE_URL}/
|
| 56 |
requests.put(url, json={"question": question, "answer": correct_answer})
|
| 57 |
-
st.success("Đã lưu quiz!")
|
| 58 |
else:
|
| 59 |
st.error("Vui lòng nhập đầy đủ thông tin quiz")
|
| 60 |
|
|
@@ -78,7 +77,8 @@ if role == "Học sinh":
|
|
| 78 |
st.error("Vui lòng nhập đầy đủ thông tin!")
|
| 79 |
|
| 80 |
st.subheader("Làm Quiz")
|
| 81 |
-
|
|
|
|
| 82 |
quizzes = res.json() if res.status_code == 200 else {}
|
| 83 |
|
| 84 |
if student_id:
|
|
@@ -87,20 +87,25 @@ if role == "Học sinh":
|
|
| 87 |
else:
|
| 88 |
student_data = {}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
# ==========================
|
| 5 |
+
# Config Firebase
|
| 6 |
# ==========================
|
| 7 |
FIREBASE_URL = "https://smartedu-3d5bf-default-rtdb.asia-southeast1.firebasedatabase.app"
|
| 8 |
|
| 9 |
# ==========================
|
| 10 |
+
# Init session state
|
| 11 |
# ==========================
|
| 12 |
if "master_logged_in" not in st.session_state:
|
| 13 |
st.session_state.master_logged_in = False
|
|
|
|
| 23 |
role = st.selectbox("Chọn vai trò", ["Master", "Học sinh"])
|
| 24 |
|
| 25 |
# ==========================
|
| 26 |
+
# MASTER SECTION
|
| 27 |
# ==========================
|
| 28 |
if role == "Master":
|
| 29 |
st.subheader("Đăng nhập Master")
|
|
|
|
| 31 |
if not st.session_state.master_logged_in:
|
| 32 |
username = st.text_input("Tài khoản")
|
| 33 |
password = st.text_input("Mật khẩu", type="password")
|
| 34 |
+
|
| 35 |
if st.button("Đăng nhập"):
|
| 36 |
if username == "master" and password == "master":
|
| 37 |
st.session_state.master_logged_in = True
|
| 38 |
st.success("Đăng nhập thành công!")
|
| 39 |
+
st.stop() # giữ state
|
| 40 |
else:
|
| 41 |
st.error("Sai tài khoản hoặc mật khẩu")
|
| 42 |
+
st.stop()
|
| 43 |
|
| 44 |
+
# Master đã đăng nhập → tạo quiz
|
|
|
|
| 45 |
st.success("Bạn đang đăng nhập với quyền Master")
|
|
|
|
| 46 |
st.subheader("Tạo Quiz mới")
|
| 47 |
+
|
| 48 |
quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")
|
| 49 |
question = st.text_area("Câu hỏi")
|
| 50 |
correct_answer = st.text_input("Đáp án đúng")
|
| 51 |
|
| 52 |
if st.button("Lưu Quiz"):
|
| 53 |
if quiz_name and question and correct_answer:
|
| 54 |
+
url = f"{FIREBASE_URL}/master_quizzes/{quiz_name}.json"
|
| 55 |
requests.put(url, json={"question": question, "answer": correct_answer})
|
| 56 |
+
st.success(f"Đã lưu quiz '{quiz_name}' dưới Master!")
|
| 57 |
else:
|
| 58 |
st.error("Vui lòng nhập đầy đủ thông tin quiz")
|
| 59 |
|
|
|
|
| 77 |
st.error("Vui lòng nhập đầy đủ thông tin!")
|
| 78 |
|
| 79 |
st.subheader("Làm Quiz")
|
| 80 |
+
# Lấy tất cả quiz từ Master node
|
| 81 |
+
res = requests.get(f"{FIREBASE_URL}/master_quizzes.json")
|
| 82 |
quizzes = res.json() if res.status_code == 200 else {}
|
| 83 |
|
| 84 |
if student_id:
|
|
|
|
| 87 |
else:
|
| 88 |
student_data = {}
|
| 89 |
|
| 90 |
+
if quizzes:
|
| 91 |
+
for quiz_name, content in quizzes.items():
|
| 92 |
+
st.markdown(f"### {quiz_name}")
|
| 93 |
|
| 94 |
+
# Kiểm tra học sinh đã làm quiz chưa
|
| 95 |
+
if student_data and quiz_name in student_data:
|
| 96 |
+
st.info(f"Bạn đã làm Quiz này rồi. Điểm: {student_data[quiz_name]}")
|
| 97 |
+
continue
|
| 98 |
|
| 99 |
+
answer = st.text_input(f"Câu trả lời của bạn cho {quiz_name}")
|
| 100 |
|
| 101 |
+
if st.button(f"Nộp {quiz_name}"):
|
| 102 |
+
if answer.strip().lower() == content["answer"].strip().lower():
|
| 103 |
+
score = "10/10"
|
| 104 |
+
else:
|
| 105 |
+
score = "0/10"
|
| 106 |
+
|
| 107 |
+
url_score = f"{FIREBASE_URL}/{student_id}/{quiz_name}.json"
|
| 108 |
+
requests.put(url_score, json=score)
|
| 109 |
+
st.success(f"Đã nộp! Điểm: {score}")
|
| 110 |
+
else:
|
| 111 |
+
st.info("Hiện chưa có quiz nào.")
|