Test / app.py
Zheng Qu
mv db to /data
28d0fc7
raw
history blame
4.24 kB
from app_utils import (load_problems, export_submissions,
get_all_students, get_student_submissions, check_password,
refresh_submissions)
from student_tab import create_student_tab
from dump_all import create_markdown_zip
import gradio as gr
import sys
def create_app():
problems = load_problems()
with gr.Blocks() as app:
with gr.Tabs():
# Student Tab
create_student_tab(problems)
# Admin Tab
with gr.Tab("Admin"):
password = gr.Textbox(type="password", label="Admin Password")
login_button = gr.Button("Login")
login_status = gr.Markdown()
with gr.Column(visible=False) as admin_panel:
with gr.Row():
students = gr.Dropdown(label="Select Student", choices=[])
refresh_button = gr.Button("πŸ”„ Refresh")
submissions = gr.JSON(label="Submissions")
with gr.Row():
download_btn = gr.Button("Download All Submissions (JSON)")
download_markdown_btn = gr.Button("Download All Submissions (Markdown)")
with gr.Row():
download_output = gr.File(label="Download JSON")
download_markdown_output = gr.File(label="Download Markdown")
def refresh_data():
return {
students: gr.update(choices=get_all_students()),
submissions: refresh_submissions()
}
refresh_button.click(
fn=refresh_data,
outputs=[students, submissions]
)
def login(pwd):
if check_password(pwd):
student_list = get_all_students()
return {
admin_panel: gr.update(visible=True),
students: gr.update(choices=student_list),
login_status: "βœ… Logged in successfully!",
submissions: refresh_submissions()
}
return {
admin_panel: gr.update(visible=False),
login_status: "❌ Invalid password",
students: gr.update(choices=[]),
submissions: None
}
def show_submissions(name):
if not name:
return None
return get_student_submissions(name)
def download_all():
file_path = export_submissions()
if file_path:
return file_path
return None
def download_markdown():
file_path = create_markdown_zip()
if file_path:
return file_path
return None
# Update event handlers
login_button.click(
fn=login,
inputs=[password],
outputs=[admin_panel, students, login_status, submissions]
)
students.change(
fn=show_submissions,
inputs=[students],
outputs=[submissions]
)
download_btn.click(
fn=download_all,
outputs=[download_output]
)
download_markdown_btn.click(
fn=download_markdown,
outputs=[download_markdown_output]
)
return app
if __name__ == "__main__":
app = create_app()
SHARE = (sys.platform == 'darwin')
app.launch(share=SHARE)