|
|
|
import gradio as gr |
|
from app_utils import (save_submission, save_all_submissions, |
|
logger, SESSION_ID) |
|
|
|
def create_student_tab(problems): |
|
|
|
|
|
with gr.Tab("Student Submission"): |
|
gr.Markdown("# Student Problem Submission") |
|
name = gr.Textbox(label="Name") |
|
email = gr.Textbox(label="Email") |
|
|
|
|
|
code_boxes = {} |
|
hint_states = {} |
|
|
|
for problem in problems: |
|
with gr.Group(): |
|
gr.Markdown("---") |
|
logger.info(f"Creating problem {problem['id']}") |
|
gr.Markdown(problem["Problem_markdown"]) |
|
|
|
hint_state = gr.State(value=False) |
|
hint_states[problem['id']] = hint_state |
|
|
|
def show_hint_dialog(state, prob_id): |
|
if state: |
|
return state, gr.update(visible=False) |
|
return gr.update(visible=True), gr.update(visible=True) |
|
|
|
with gr.Group(visible=False) as hint_dialog: |
|
gr.Markdown("By reading hint, you agree to apply a factor of 0.97 on grades of this problem.") |
|
with gr.Row(): |
|
confirm_btn = gr.Button("I Agree", size="sm") |
|
cancel_btn = gr.Button("Cancel", size="sm") |
|
|
|
hint_button = gr.Button("Show Hint") |
|
hint_text = gr.Markdown(visible=False, value=problem["Hint"]) |
|
|
|
code_box = gr.Code(language="python", value=problem["Template_Code"]) |
|
|
|
code_boxes[problem['id']] = code_box |
|
|
|
save_button = gr.Button("Save") |
|
save_status = gr.Markdown() |
|
|
|
gr.Markdown(f"```python\n{problem['Testing_Code']}\n```") |
|
|
|
def on_hint_confirm(state): |
|
return True, gr.update(visible=False), gr.update(visible=True) |
|
|
|
def on_hint_cancel(state): |
|
return False, gr.update(visible=False), gr.update(visible=False) |
|
|
|
def save_code(name, email, code, hint_shown, prob_id=problem['id']): |
|
if not name or not email: |
|
return "❌Please enter your name and email before saving." |
|
try: |
|
save_submission(SESSION_ID, name, email, prob_id, code, hint_shown) |
|
return "✅ Saved successfully!" |
|
except Exception as e: |
|
return f"❌ Error saving: {str(e)}" |
|
|
|
hint_button.click( |
|
show_hint_dialog, |
|
[hint_state, gr.State(problem['id'])], |
|
[hint_dialog, hint_text] |
|
) |
|
|
|
confirm_btn.click( |
|
on_hint_confirm, |
|
[hint_state], |
|
[hint_state, hint_dialog, hint_text] |
|
) |
|
|
|
cancel_btn.click( |
|
on_hint_cancel, |
|
[hint_state], |
|
[hint_state, hint_dialog, hint_text] |
|
) |
|
|
|
save_button.click( |
|
save_code, |
|
[name, email, code_box, hint_state], |
|
save_status |
|
) |
|
|
|
submit_all = gr.Button("Submit All") |
|
submit_status = gr.Markdown() |
|
|
|
|
|
|
|
def submit_all_problems(values): |
|
|
|
num_problems = len(code_boxes) |
|
name, email = values[0], values[1] |
|
code_values = values[2:2+num_problems] |
|
hint_values = values[2+num_problems:] |
|
|
|
if not name or not email: |
|
return "Please enter your name and email before submitting." |
|
|
|
|
|
codes = {prob_id: code for prob_id, code in zip(code_boxes.keys(), code_values)} |
|
hints = {prob_id: hint for prob_id, hint in zip(hint_states.keys(), hint_values)} |
|
|
|
return save_all_submissions(name, email, codes, hints) |
|
|
|
|
|
all_inputs = [name, email] + list(code_boxes.values()) + list(hint_states.values()) |
|
|
|
submit_all.click( |
|
fn=lambda *args: submit_all_problems(args), |
|
inputs=all_inputs, |
|
outputs=submit_status |
|
) |
|
|
|
|