Spaces:
Sleeping
Sleeping
| # Import necessary libraries | |
| import streamlit as st | |
| import sqlite3 | |
| import random | |
| from datetime import datetime | |
| # Create a connection to the SQLite database | |
| conn = sqlite3.connect('student_exams.db') | |
| # Create a cursor object | |
| cur = conn.cursor() | |
| # Create tables if they do not exist | |
| cur.execute(''' | |
| CREATE TABLE IF NOT EXISTS students | |
| (id_number INTEGER PRIMARY KEY, name TEXT, year INTEGER, section TEXT) | |
| ''') | |
| cur.execute(''' | |
| CREATE TABLE IF NOT EXISTS exams | |
| (id INTEGER PRIMARY KEY, timestamp_create TIMESTAMP, subject TEXT, topic TEXT, type TEXT, num_items INTEGER) | |
| ''') | |
| cur.execute(''' | |
| CREATE TABLE IF NOT EXISTS attempts | |
| (id INTEGER PRIMARY KEY, date_taken TIMESTAMP, exam_id INTEGER, student_id INTEGER, answers TEXT, results TEXT, total_score INTEGER) | |
| ''') | |
| # Insert dummy data | |
| cur.execute("INSERT OR IGNORE INTO students (id_number, name, year, section) VALUES (1, 'John Doe', 1, 'A')") | |
| cur.execute("INSERT OR IGNORE INTO exams (id, timestamp_create, subject, topic, type, num_items) VALUES (1, '2022-01-01 00:00:00', 'Math', 'Algebra', 'Multiple Choice', 5)") | |
| # Commit the transaction | |
| conn.commit() | |
| # Function to get the exam questions | |
| def get_exam_questions(exam_id): | |
| questions = [] | |
| for i in range(5): # Assuming 5 questions for simplicity | |
| question = { | |
| 'question': f'Question {i+1}', | |
| 'options': [f'Option {j+1}' for j in range(4)], | |
| 'correct_answer': random.choice([f'Option {j+1}' for j in range(4)]) | |
| } | |
| questions.append(question) | |
| return questions | |
| # Function to evaluate the answers | |
| def evaluate_answers(questions, answers): | |
| score = 0 | |
| results = [] | |
| for i, question in enumerate(questions): | |
| if answers[i] == question['correct_answer']: | |
| score += 1 | |
| results.append('Correct') | |
| else: | |
| results.append('Incorrect') | |
| return score, results | |
| # Streamlit app | |
| st.title('Quiz Interface') | |
| # Get the exam questions | |
| exam_id = 1 | |
| questions = get_exam_questions(exam_id) | |
| # Initialize the answers | |
| answers = [] | |
| # Display the questions | |
| for i, question in enumerate(questions): | |
| st.write(question['question']) | |
| answer = st.selectbox('Select an answer', question['options'], key=i) | |
| answers.append(answer) | |
| # Evaluate the answers | |
| if st.button('Submit'): | |
| score, results = evaluate_answers(questions, answers) | |
| # Store the attempt data | |
| cur.execute("INSERT INTO attempts (date_taken, exam_id, student_id, answers, results, total_score) VALUES (?, ?, ?, ?, ?, ?)", | |
| (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), exam_id, 1, ', '.join(answers), ', '.join(results), score)) | |
| conn.commit() | |
| # Display the summary | |
| st.write('Summary:') | |
| st.write(f'Total Score: {score}/{len(questions)}') | |
| st.write('Results:') | |
| for i, result in enumerate(results): | |
| st.write(f'Question {i+1}: {result}') | |
| # Display the history of previous attempts | |
| st.write('History of Previous Attempts:') | |
| cur.execute("SELECT date_taken, total_score FROM attempts WHERE exam_id = ? AND student_id = ?", (exam_id, 1)) | |
| attempts = cur.fetchall() | |
| for attempt in attempts: | |
| st.write(f'Date: {attempt[0]}, Score: {attempt[1]}/{len(questions)}') | |
| # Provide an option to retake the quiz | |
| if st.button('Retake Quiz'): | |
| answers = [] | |
| for i, question in enumerate(questions): | |
| st.write(question['question']) | |
| answer = st.selectbox('Select an answer', question['options'], key=i) | |
| answers.append(answer) |