Spaces:
Runtime error
Runtime error
Update streamlit_app.py
Browse files- streamlit_app.py +112 -0
streamlit_app.py
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# ==========================
|
| 7 |
+
# Firebase Realtime Database
|
| 8 |
+
# ==========================
|
| 9 |
+
FIREBASE_URL = "https://smartedu-3d5bf-default-rtdb.asia-southeast1.firebasedatabase.app" # adjust if needed
|
| 10 |
+
|
| 11 |
+
# --------------------------
|
| 12 |
+
def write_student_info(student_id, name, classroom):
|
| 13 |
+
url = f"{FIREBASE_URL}/{student_id}.json"
|
| 14 |
+
data = {
|
| 15 |
+
"name": name,
|
| 16 |
+
"class": classroom,
|
| 17 |
+
}
|
| 18 |
+
requests.patch(url, json=data)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def write_quiz_score(student_id, quiz_name, score):
|
| 22 |
+
url = f"{FIREBASE_URL}/{student_id}/{quiz_name}.json"
|
| 23 |
+
requests.put(url, json=score)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_student_data(student_id):
|
| 27 |
+
url = f"{FIREBASE_URL}/{student_id}.json"
|
| 28 |
+
res = requests.get(url)
|
| 29 |
+
if res.status_code == 200:
|
| 30 |
+
return res.json()
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
# ==========================
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
# ==========================
|
| 36 |
+
st.title("SmartEdu Quiz System")
|
| 37 |
+
|
| 38 |
+
role = st.selectbox("Chọn vai trò", ["Master", "Học sinh"])
|
| 39 |
+
|
| 40 |
+
# ==========================
|
| 41 |
+
# MASTER LOGIN
|
| 42 |
+
# ==========================
|
| 43 |
+
if role == "Master":
|
| 44 |
+
st.subheader("Đăng nhập Master")
|
| 45 |
+
username = st.text_input("Tài khoản")
|
| 46 |
+
password = st.text_input("Mật khẩu", type="password")
|
| 47 |
+
|
| 48 |
+
if st.button("Đăng nhập"):
|
| 49 |
+
if username == "master" and password == "master":
|
| 50 |
+
st.success("Đăng nhập thành công!")
|
| 51 |
+
|
| 52 |
+
st.subheader("Tạo Quiz mới")
|
| 53 |
+
quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")
|
| 54 |
+
question = st.text_area("Câu hỏi")
|
| 55 |
+
correct_answer = st.text_input("Đáp án đúng")
|
| 56 |
+
|
| 57 |
+
if st.button("Lưu Quiz"):
|
| 58 |
+
# Store quiz definitions in firebase
|
| 59 |
+
url = f"{FIREBASE_URL}/quiz_definitions/{quiz_name}.json"
|
| 60 |
+
requests.put(url, json={
|
| 61 |
+
"question": question,
|
| 62 |
+
"answer": correct_answer,
|
| 63 |
+
})
|
| 64 |
+
st.success("Đã lưu quiz!")
|
| 65 |
+
else:
|
| 66 |
+
st.error("Sai tài khoản hoặc mật khẩu")
|
| 67 |
+
|
| 68 |
+
# ==========================
|
| 69 |
+
# STUDENT SECTION
|
| 70 |
+
# ==========================
|
| 71 |
+
if role == "Học sinh":
|
| 72 |
+
st.subheader("Thông tin học sinh")
|
| 73 |
+
student_id = st.text_input("ID học sinh")
|
| 74 |
+
student_name = st.text_input("Tên")
|
| 75 |
+
student_class = st.text_input("Lớp")
|
| 76 |
+
|
| 77 |
+
if st.button("Gửi thông tin"):
|
| 78 |
+
if student_id and student_name and student_class:
|
| 79 |
+
write_student_info(student_id, student_name, student_class)
|
| 80 |
+
st.success("Đã lưu thông tin học sinh!")
|
| 81 |
+
else:
|
| 82 |
+
st.error("Vui lòng nhập đầy đủ thông tin!")
|
| 83 |
+
|
| 84 |
+
# Load quiz definitions
|
| 85 |
+
st.subheader("Làm Quiz")
|
| 86 |
+
res = requests.get(f"{FIREBASE_URL}/quiz_definitions.json")
|
| 87 |
+
quizzes = res.json() if res.status_code == 200 else {}
|
| 88 |
+
|
| 89 |
+
if student_id:
|
| 90 |
+
student_data = get_student_data(student_id)
|
| 91 |
+
|
| 92 |
+
if quizzes:
|
| 93 |
+
for quiz_name, content in quizzes.items():
|
| 94 |
+
st.markdown(f"### {quiz_name}")
|
| 95 |
+
|
| 96 |
+
# Check if student already completed
|
| 97 |
+
if student_data and quiz_name in student_data:
|
| 98 |
+
st.info(f"Bạn đã làm Quiz này rồi. Điểm: {student_data[quiz_name]}")
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
st.write(content["question"])
|
| 102 |
+
answer = st.text_input(f"Câu trả lời của bạn cho {quiz_name}")
|
| 103 |
+
|
| 104 |
+
if st.button(f"Nộp {quiz_name}"):
|
| 105 |
+
if answer.strip().lower() == content["answer"].strip().lower():
|
| 106 |
+
score = "10/10"
|
| 107 |
+
else:
|
| 108 |
+
score = "0/10"
|
| 109 |
+
write_quiz_score(student_id, quiz_name, score)
|
| 110 |
+
st.success(f"Đã nộp! Điểm: {score}")
|
| 111 |
+
else:
|
| 112 |
+
st.info("Hiện chưa có quiz nào.")
|