File size: 3,378 Bytes
f7c75d0
53f9c43
 
 
 
 
f7c75d0
 
 
53f9c43
f7c75d0
 
2b130d1
 
 
 
 
 
 
a709932
 
 
 
 
2b130d1
 
 
 
 
 
 
 
 
 
 
f7c75d0
 
2b130d1
f7c75d0
 
 
53f9c43
2b130d1
 
 
f7c75d0
 
 
53f9c43
2b130d1
 
 
 
 
 
 
 
 
 
 
 
 
 
f7c75d0
 
 
 
53f9c43
f7c75d0
 
 
 
 
 
53f9c43
 
 
 
 
 
2b130d1
53f9c43
 
2b130d1
 
f7c75d0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import streamlit as st
from settings import BASE_DIR, NUMBER_OF_TECHNICAL_QUESTIONS, NUMBER_OF_COMMON_QUESTIONS
from core.slack_notifier import SlackNotifier
from core.questions_loader_local import QuestionLoaderLocal
from presentation.layout import Layout

# Slack Webhook
SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]
layout = Layout()

def call():
    # Check if questions are already loaded
    if 'questions' not in st.session_state:
        # Define questions
        questions = QuestionLoaderLocal(
            os.path.join(BASE_DIR, "questions", st.session_state['technology'].lower(), "questions.csv"),
            NUMBER_OF_TECHNICAL_QUESTIONS
        ).fetch_questions()
        
        if not questions:
            st.markdown("Work in progress!!")
            return

        common_questions = QuestionLoaderLocal(
            os.path.join(BASE_DIR, "questions", "common", "questions.csv"),
            NUMBER_OF_COMMON_QUESTIONS
        ).fetch_questions()
        questions.extend(common_questions)
        
        # Store questions in session state to persist across interactions
        st.session_state['questions'] = questions

    # Retrieve the questions from session state
    questions = st.session_state['questions']
    score = 0
    total_questions = len(questions)
    answered_all = True

    for idx, question in enumerate(questions):
        # Section for each question with styling
        selected_option = layout.render_test_question(question, idx)
        if not selected_option:
            answered_all = False
        # Checking for correct answer and assigning points based on difficulty
        if selected_option == question['answer']:
            score += 1

    if st.button("Submit Test", use_container_width=True, type="primary"):
        if answered_all:
            st.session_state['test_started'] = False
            layout.render_completion_message(score, total_questions)
            result = (score / total_questions) * 100
            SlackNotifier(SLACK_WEBHOOK_URL).send_candidate_info(
                st.session_state['name'], 
                st.session_state['email'], 
                st.session_state['experience'], 
                st.session_state['technology'], 
                f"{result:.2f}%"
            )
        else:
            # Show a message asking the user to answer all questions
            st.warning("Please answer all questions before submitting.")

def main():
    # Set page config with custom title and layout
    st.set_page_config(page_title="Candidate MCQ Platform", layout="wide")
    layout.render_header()

    if 'test_started' not in st.session_state:
        st.session_state['test_started'] = False

    if not st.session_state['test_started']:
        st.title("Welcome to the Candidate Assessment Platform")
        name, email, experience, technology, submit = layout.render_signup_form()
        if name and email:
            st.session_state['name'] = name
            st.session_state['email'] = email
            st.session_state['experience'] = experience
            st.session_state['technology'] = technology
            st.session_state['test_wip'] = True
        if submit:
            st.session_state['test_started'] = True
            st.rerun()
        layout.render_instructions()
    else:
        call()

if __name__ == "__main__":
    main()