Spaces:
Runtime error
Runtime error
Update streamlit_app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import streamlit as stimport requestsimport jsonfrom datetime import datetime# ==========================# Firebase Realtime Database# ==========================FIREBASE_URL = "https://smartedu-3d5bf-default-rtdb.asia-southeast1.firebasedatabase.app" # adjust if needed# --------------------------def write_student_info(student_id, name, classroom):url = f"{FIREBASE_URL}/{student_id}.json"data = {"name": name,"class": classroom,}requests.patch(url, json=data)def write_quiz_score(student_id, quiz_name, score):url = f"{FIREBASE_URL}/{student_id}/{quiz_name}.json"requests.put(url, json=score)def get_student_data(student_id):url = f"{FIREBASE_URL}/{student_id}.json"res = requests.get(url)if res.status_code == 200:return res.json()return None# ==========================# Streamlit UI# ==========================st.title("SmartEdu Quiz System")role = st.selectbox("Chọn vai trò", ["Master", "Học sinh"])# ==========================# MASTER LOGIN# ==========================if role == "Master":st.subheader("Đăng nhập Master")username = st.text_input("Tài khoản")password = st.text_input("Mật khẩu", type="password")if st.button("Đăng nhập"):if username == "master" and password == "master":st.success("Đăng nhập thành công!")st.subheader("Tạo Quiz mới")quiz_name = st.text_input("Tên Quiz (ví dụ: Quiz1)")question = st.text_area("Câu hỏi")correct_answer = st.text_input("Đáp án đúng")if st.button("Lưu Quiz"):# Store quiz definitions in firebaseurl = f"{FIREBASE_URL}/quiz_definitions/{quiz_name}.json"requests.put(url, json={"question": question,"answer": correct_answer,})st.success("Đã lưu quiz!")else:st.error("Sai tài khoản hoặc mật khẩu")# ==========================# STUDENT SECTION# ==========================if role == "Học sinh":st.subheader("Thông tin học sinh")student_id = st.text_input("ID học sinh")student_name = st.text_input("Tên")student_class = st.text_input("Lớp")if st.button("Gửi thông tin"):if student_id and student_name and student_class:write_student_info(student_id, student_name, student_class)st.success("Đã lưu thông tin học sinh!")else:st.error("Vui lòng nhập đầy đủ thông tin!")# Load quiz definitionsst.subheader("Làm Quiz")res = requests.get(f"{FIREBASE_URL}/quiz_definitions.json")quizzes = res.json() if res.status_code == 200 else {}if student_id:student_data = get_student_data(student_id)if quizzes:for quiz_name, content in quizzes.items():st.markdown(f"### {quiz_name}")# Check if student already completedif student_data and quiz_name in student_data:st.info(f"Bạn đã làm Quiz này rồi. Điểm: {student_data[quiz_name]}")continuest.write(content["question"])answer = st.text_input(f"Câu trả lời của bạn cho {quiz_name}")if st.button(f"Nộp {quiz_name}"):if answer.strip().lower() == content["answer"].strip().lower():score = "10/10"else:score = "0/10"write_quiz_score(student_id, quiz_name, score)st.success(f"Đã nộp! Điểm: {score}")else:st.info("Hiện chưa có quiz nào.")
streamlit_app.py+112