187 lines
7.0 KiB
Plaintext
187 lines
7.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" * Serving Flask app '__main__'\n",
|
|
" * Debug mode: off\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
|
|
" * Running on http://127.0.0.1:5000\n",
|
|
"Press CTRL+C to quit\n",
|
|
"127.0.0.1 - - [08/Jan/2025 11:10:20] \"GET / HTTP/1.1\" 200 -\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from flask import Flask, render_template, request, redirect, url_for, session\n",
|
|
"import random\n",
|
|
"\n",
|
|
"app = Flask(__name__)\n",
|
|
"app.secret_key = 'your_secret_key' # You must have a secret key for sessions to work\n",
|
|
"\n",
|
|
"# Function to parse the questions from the file\n",
|
|
"def parse_questions(file_paths):\n",
|
|
" content = []\n",
|
|
" for file_path in file_paths:\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as file:\n",
|
|
" content.extend(file.read().split(\"\\n\\n\")) # Split content by empty lines between questions\n",
|
|
"\n",
|
|
" questions = []\n",
|
|
"\n",
|
|
" for block in content:\n",
|
|
" lines = block.strip().split(\"\\n\")\n",
|
|
" question = lines[0].replace(\"Kérdés: \", \"\").strip() # Remove 'Kérdés:' from the start\n",
|
|
" answers = []\n",
|
|
" correct_answers = []\n",
|
|
"\n",
|
|
" # Parse the answers and separate correct answers (those starting with '!')\n",
|
|
" for line in lines[1:]:\n",
|
|
" answer = line.strip()\n",
|
|
" if answer.startswith('!'):\n",
|
|
" correct_answers.append(answer[1:].strip()) # Remove the '!' and save the correct answer\n",
|
|
" else:\n",
|
|
" answers.append(answer)\n",
|
|
"\n",
|
|
" # Add the parsed question, answers, and correct answers to the list\n",
|
|
" if correct_answers:\n",
|
|
" questions.append({\n",
|
|
" 'question': question,\n",
|
|
" 'answers': answers + correct_answers, # Append correct answers at the end\n",
|
|
" 'correct_answers': correct_answers\n",
|
|
" })\n",
|
|
"\n",
|
|
" return questions\n",
|
|
"\n",
|
|
"# Parse questions from the file\n",
|
|
"questions = parse_questions(['kerdessor1.txt', 'kerdessor2.txt', 'kerdessor3.txt'])\n",
|
|
"\n",
|
|
"@app.route('/')\n",
|
|
"def index():\n",
|
|
" # Initialize the quiz session\n",
|
|
" if 'selected_questions' not in session:\n",
|
|
" selected_questions = random.sample(questions, 20)\n",
|
|
" session['selected_questions'] = selected_questions\n",
|
|
" session['question_index'] = 0\n",
|
|
" session['answers'] = [] # Clear answers to start fresh\n",
|
|
"\n",
|
|
" else:\n",
|
|
" selected_questions = session['selected_questions']\n",
|
|
"\n",
|
|
" question_index = session.get('question_index', 0)\n",
|
|
"\n",
|
|
" # If all questions are answered, redirect to the results page\n",
|
|
" if question_index >= len(selected_questions):\n",
|
|
" return redirect(url_for('result'))\n",
|
|
"\n",
|
|
" # Get the current question\n",
|
|
" current_question = selected_questions[question_index]\n",
|
|
"\n",
|
|
" # Shuffle the answers to randomize their order\n",
|
|
" random.shuffle(current_question['answers'])\n",
|
|
"\n",
|
|
" return render_template('quiz.html', question=current_question, question_index=question_index)\n",
|
|
"\n",
|
|
"@app.route('/submit_answer', methods=['POST'])\n",
|
|
"def submit_answer():\n",
|
|
" # Get the user's answer and the current question index\n",
|
|
" user_answers = request.form.getlist('answer') # Use getlist for checkboxes (multiple answers)\n",
|
|
" question_index = int(request.form.get('question_index'))\n",
|
|
" \n",
|
|
" # Store the answer in the session\n",
|
|
" answers = session.get('answers', [])\n",
|
|
" answers.append(user_answers)\n",
|
|
" session['answers'] = answers\n",
|
|
"\n",
|
|
" # Move to the next question\n",
|
|
" question_index += 1\n",
|
|
" session['question_index'] = question_index\n",
|
|
" \n",
|
|
" return redirect(url_for('index'))\n",
|
|
"\n",
|
|
"\n",
|
|
"@app.route('/result')\n",
|
|
"def result():\n",
|
|
" # Ensure 'selected_questions' exists in the session\n",
|
|
" selected_questions = session.get('selected_questions', None)\n",
|
|
" answers = session.get('answers', None)\n",
|
|
"\n",
|
|
" if not selected_questions or not answers:\n",
|
|
" return redirect(url_for('index')) # Redirect back to the quiz if selected_questions or answers are missing\n",
|
|
"\n",
|
|
" # Calculate the score and track mistakes\n",
|
|
" score = 0\n",
|
|
" mistakes = []\n",
|
|
"\n",
|
|
" for i, user_answer in enumerate(answers):\n",
|
|
" correct_answers = selected_questions[i]['correct_answers']\n",
|
|
" \n",
|
|
" # Check if the selected answers exactly match the correct answers (order doesn't matter)\n",
|
|
" if set(user_answer) == set(correct_answers):\n",
|
|
" score += 1\n",
|
|
" else:\n",
|
|
" mistakes.append({\n",
|
|
" 'question': selected_questions[i]['question'],\n",
|
|
" 'correct_answer': correct_answers,\n",
|
|
" 'user_answer': user_answer\n",
|
|
" })\n",
|
|
"\n",
|
|
" # Prepare zipped data to pass to the template (question, user answer pair)\n",
|
|
" questions_with_answers = zip(selected_questions, answers)\n",
|
|
"\n",
|
|
" # Reset session data so the user can restart the quiz or continue\n",
|
|
" session.pop('question_index', None) # Clear the current question index\n",
|
|
" session.pop('answers', None) # Clear stored answers\n",
|
|
" session.pop('selected_questions', None) # Clear selected questions\n",
|
|
"\n",
|
|
" return render_template('result.html', score=score, total=len(selected_questions), mistakes=mistakes, questions_with_answers=questions_with_answers)\n",
|
|
"\n",
|
|
"\n",
|
|
"@app.route('/restart')\n",
|
|
"def restart():\n",
|
|
" # Clear session data to restart the quiz\n",
|
|
" session.pop('question_index', None)\n",
|
|
" session.pop('answers', None)\n",
|
|
" session.pop('selected_questions', None)\n",
|
|
" \n",
|
|
" return redirect(url_for('index')) # Redirect to start the quiz again\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" app.run()\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "venv",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|