Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import random
|
| 4 |
+
import gspread
|
| 5 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
| 6 |
+
|
| 7 |
+
# Google Sheets setup
|
| 8 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 9 |
+
credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread_credentials.json', scope)
|
| 10 |
+
client = gspread.authorize(credentials)
|
| 11 |
+
responses_sheet = client.open("QuizResponses").sheet1
|
| 12 |
+
|
| 13 |
+
# Load questions from a Google Sheet
|
| 14 |
+
questions_url = 'https://docs.google.com/spreadsheets/d/1w5zZRg5drriTFrCAaT47e7H7ujYALIG5D7VTQsTpPxY/export?format=csv'
|
| 15 |
+
questions_df = pd.read_csv(questions_url)
|
| 16 |
+
|
| 17 |
+
# Fetch existing PRNs from the sheet
|
| 18 |
+
submitted_prns = set(responses_sheet.col_values(1))
|
| 19 |
+
|
| 20 |
+
# Function to shuffle questions and options
|
| 21 |
+
def get_random_questions(df, num_questions=20):
|
| 22 |
+
questions = df.sample(n=num_questions).to_dict('records')
|
| 23 |
+
for q in questions:
|
| 24 |
+
options = [q['Option1'], q['Option2'], q['Option3'], q['Option4']]
|
| 25 |
+
random.shuffle(options)
|
| 26 |
+
q['options'] = options
|
| 27 |
+
return questions
|
| 28 |
+
|
| 29 |
+
# Initialize session state
|
| 30 |
+
if 'student_info' not in st.session_state:
|
| 31 |
+
st.session_state['student_info'] = None
|
| 32 |
+
if 'questions' not in st.session_state:
|
| 33 |
+
st.session_state['questions'] = []
|
| 34 |
+
if 'current_question' not in st.session_state:
|
| 35 |
+
st.session_state['current_question'] = 0
|
| 36 |
+
if 'responses' not in st.session_state:
|
| 37 |
+
st.session_state['responses'] = []
|
| 38 |
+
if 'completed' not in st.session_state:
|
| 39 |
+
st.session_state['completed'] = False
|
| 40 |
+
|
| 41 |
+
# Page 1: Student Information
|
| 42 |
+
if st.session_state['student_info'] is None:
|
| 43 |
+
st.title("Student Quiz App")
|
| 44 |
+
st.subheader("Enter your details")
|
| 45 |
+
prn = st.text_input("PRN")
|
| 46 |
+
name = st.text_input("Name")
|
| 47 |
+
division = st.selectbox("Division", ['A', 'B', 'C', 'D'])
|
| 48 |
+
submit_info = st.button("Start Quiz")
|
| 49 |
+
|
| 50 |
+
if submit_info and prn and name and division:
|
| 51 |
+
if prn in submitted_prns:
|
| 52 |
+
st.error("You have already submitted the quiz.")
|
| 53 |
+
else:
|
| 54 |
+
st.session_state['student_info'] = {'prn': prn, 'name': name, 'division': division}
|
| 55 |
+
st.session_state['questions'] = get_random_questions(questions_df)
|
| 56 |
+
st.experimental_rerun()
|
| 57 |
+
|
| 58 |
+
# Page 2: Quiz
|
| 59 |
+
elif not st.session_state['completed']:
|
| 60 |
+
st.title("Quiz Time!")
|
| 61 |
+
question = st.session_state['questions'][st.session_state['current_question']]
|
| 62 |
+
st.write(f"Question {st.session_state['current_question'] + 1}:")
|
| 63 |
+
st.write(question['Question'])
|
| 64 |
+
choice = st.radio("Select your answer:", question['options'], index=None)
|
| 65 |
+
|
| 66 |
+
# Disable copy-paste with custom CSS
|
| 67 |
+
st.markdown(
|
| 68 |
+
"""
|
| 69 |
+
<style>
|
| 70 |
+
input, textarea { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
|
| 71 |
+
</style>
|
| 72 |
+
""",
|
| 73 |
+
unsafe_allow_html=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
next_question = st.button("Next")
|
| 77 |
+
|
| 78 |
+
if next_question and choice is not None:
|
| 79 |
+
st.session_state['responses'].append({'question': question['Question'], 'answer': choice})
|
| 80 |
+
st.session_state['current_question'] += 1
|
| 81 |
+
|
| 82 |
+
if st.session_state['current_question'] >= len(st.session_state['questions']):
|
| 83 |
+
st.session_state['completed'] = True
|
| 84 |
+
|
| 85 |
+
st.experimental_rerun()
|
| 86 |
+
|
| 87 |
+
# Submission Page
|
| 88 |
+
if st.session_state['completed']:
|
| 89 |
+
st.title("Quiz Submitted!")
|
| 90 |
+
st.write("Thank you for participating.")
|
| 91 |
+
st.write("Your responses have been recorded.")
|
| 92 |
+
|
| 93 |
+
# Record the PRN to prevent re-submission
|
| 94 |
+
submitted_prns.add(st.session_state['student_info']['prn'])
|
| 95 |
+
responses_sheet.append_row([st.session_state['student_info']['prn'],
|
| 96 |
+
st.session_state['student_info']['name'],
|
| 97 |
+
st.session_state['student_info']['division'],
|
| 98 |
+
str(st.session_state['responses'])])
|
| 99 |
+
|
| 100 |
+
# Display responses (optional - for testing)
|
| 101 |
+
st.write(st.session_state['student_info'])
|
| 102 |
+
st.write(st.session_state['responses'])
|