{ "cells": [ { "cell_type": "code", "execution_count": 9, "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 - - [07/Jan/2025 11:04:58] \"GET /result HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:04:58] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:03] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:03] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:18] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:18] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:21] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:21] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:31] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:31] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:38] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:38] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:52] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:05:52] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:23] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:23] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:27] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:27] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:42] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:42] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:49] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:06:49] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:02] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:02] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:15] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:15] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:18] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:18] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:36] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:36] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:50] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:50] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:58] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:07:58] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:15] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:15] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:18] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:18] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:34] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:34] \"GET / HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:56] \"POST /submit_answer HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:56] \"GET / HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:08:56] \"GET /result HTTP/1.1\" 200 -\n", "127.0.0.1 - - [07/Jan/2025 11:09:58] \"GET /restart HTTP/1.1\" 302 -\n", "127.0.0.1 - - [07/Jan/2025 11:09:58] \"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_path, file_path2):\n", " with open(file_path, 'r', encoding='utf-8') as file:\n", " content = file.read().split(\"\\n\\n\") # Split content by empty lines between questions\n", "\n", " with open(file_path2, 'r', encoding='utf-8') as file:\n", " content = file.read().split(\"\\n\\n\") # Split content by empty lines between questions\n", "\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')\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 }