Spaces:
Sleeping
Sleeping
first full save
Browse files- app.py +105 -0
- requirements.txt +1 -0
- student_exams.db +0 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
import sqlite3
|
4 |
+
import random
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# Create a connection to the SQLite database
|
8 |
+
conn = sqlite3.connect('student_exams.db')
|
9 |
+
|
10 |
+
# Create a cursor object
|
11 |
+
cur = conn.cursor()
|
12 |
+
|
13 |
+
# Create tables if they do not exist
|
14 |
+
cur.execute('''
|
15 |
+
CREATE TABLE IF NOT EXISTS students
|
16 |
+
(id_number INTEGER PRIMARY KEY, name TEXT, year INTEGER, section TEXT)
|
17 |
+
''')
|
18 |
+
|
19 |
+
cur.execute('''
|
20 |
+
CREATE TABLE IF NOT EXISTS exams
|
21 |
+
(id INTEGER PRIMARY KEY, timestamp_create TIMESTAMP, subject TEXT, topic TEXT, type TEXT, num_items INTEGER)
|
22 |
+
''')
|
23 |
+
|
24 |
+
cur.execute('''
|
25 |
+
CREATE TABLE IF NOT EXISTS attempts
|
26 |
+
(id INTEGER PRIMARY KEY, date_taken TIMESTAMP, exam_id INTEGER, student_id INTEGER, answers TEXT, results TEXT, total_score INTEGER)
|
27 |
+
''')
|
28 |
+
|
29 |
+
# Insert dummy data
|
30 |
+
cur.execute("INSERT OR IGNORE INTO students (id_number, name, year, section) VALUES (1, 'John Doe', 1, 'A')")
|
31 |
+
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)")
|
32 |
+
|
33 |
+
# Commit the transaction
|
34 |
+
conn.commit()
|
35 |
+
|
36 |
+
# Function to get the exam questions
|
37 |
+
def get_exam_questions(exam_id):
|
38 |
+
questions = []
|
39 |
+
for i in range(5): # Assuming 5 questions for simplicity
|
40 |
+
question = {
|
41 |
+
'question': f'Question {i+1}',
|
42 |
+
'options': [f'Option {j+1}' for j in range(4)],
|
43 |
+
'correct_answer': random.choice([f'Option {j+1}' for j in range(4)])
|
44 |
+
}
|
45 |
+
questions.append(question)
|
46 |
+
return questions
|
47 |
+
|
48 |
+
# Function to evaluate the answers
|
49 |
+
def evaluate_answers(questions, answers):
|
50 |
+
score = 0
|
51 |
+
results = []
|
52 |
+
for i, question in enumerate(questions):
|
53 |
+
if answers[i] == question['correct_answer']:
|
54 |
+
score += 1
|
55 |
+
results.append('Correct')
|
56 |
+
else:
|
57 |
+
results.append('Incorrect')
|
58 |
+
return score, results
|
59 |
+
|
60 |
+
# Streamlit app
|
61 |
+
st.title('Quiz Interface')
|
62 |
+
|
63 |
+
# Get the exam questions
|
64 |
+
exam_id = 1
|
65 |
+
questions = get_exam_questions(exam_id)
|
66 |
+
|
67 |
+
# Initialize the answers
|
68 |
+
answers = []
|
69 |
+
|
70 |
+
# Display the questions
|
71 |
+
for i, question in enumerate(questions):
|
72 |
+
st.write(question['question'])
|
73 |
+
answer = st.selectbox('Select an answer', question['options'], key=i)
|
74 |
+
answers.append(answer)
|
75 |
+
|
76 |
+
# Evaluate the answers
|
77 |
+
if st.button('Submit'):
|
78 |
+
score, results = evaluate_answers(questions, answers)
|
79 |
+
|
80 |
+
# Store the attempt data
|
81 |
+
cur.execute("INSERT INTO attempts (date_taken, exam_id, student_id, answers, results, total_score) VALUES (?, ?, ?, ?, ?, ?)",
|
82 |
+
(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), exam_id, 1, ', '.join(answers), ', '.join(results), score))
|
83 |
+
conn.commit()
|
84 |
+
|
85 |
+
# Display the summary
|
86 |
+
st.write('Summary:')
|
87 |
+
st.write(f'Total Score: {score}/{len(questions)}')
|
88 |
+
st.write('Results:')
|
89 |
+
for i, result in enumerate(results):
|
90 |
+
st.write(f'Question {i+1}: {result}')
|
91 |
+
|
92 |
+
# Display the history of previous attempts
|
93 |
+
st.write('History of Previous Attempts:')
|
94 |
+
cur.execute("SELECT date_taken, total_score FROM attempts WHERE exam_id = ? AND student_id = ?", (exam_id, 1))
|
95 |
+
attempts = cur.fetchall()
|
96 |
+
for attempt in attempts:
|
97 |
+
st.write(f'Date: {attempt[0]}, Score: {attempt[1]}/{len(questions)}')
|
98 |
+
|
99 |
+
# Provide an option to retake the quiz
|
100 |
+
if st.button('Retake Quiz'):
|
101 |
+
answers = []
|
102 |
+
for i, question in enumerate(questions):
|
103 |
+
st.write(question['question'])
|
104 |
+
answer = st.selectbox('Select an answer', question['options'], key=i)
|
105 |
+
answers.append(answer)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
streamlit
|
student_exams.db
ADDED
Binary file (16.4 kB). View file
|
|