From c3cce425598201845c4fa28c180a11d0a8a61aa9 Mon Sep 17 00:00:00 2001 From: AndreeaRadoescu Date: Wed, 16 Oct 2024 21:38:33 +0200 Subject: refactored functionality for init and showing card in a separate function, added the test functionality, --- gui-project/cards.py | 5 ---- gui-project/main_ui.py | 71 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 33 deletions(-) (limited to 'gui-project') diff --git a/gui-project/cards.py b/gui-project/cards.py index 1bb4320..ac921b3 100644 --- a/gui-project/cards.py +++ b/gui-project/cards.py @@ -19,10 +19,6 @@ class CardComponent(): def get_back(self): return self.back - -# ui.colors(back="#c4bcdb") - - class CardUI(): def __init__(self, parent_ui, card : CardComponent): self.parent_ui = parent_ui @@ -69,7 +65,6 @@ class CardUI(): self.show_back() def user_clicked_correct(self): - ui.notify("You got this") self.correctly_answered = True self.is_resolved.set() diff --git a/gui-project/main_ui.py b/gui-project/main_ui.py index 8f3371c..de3d770 100644 --- a/gui-project/main_ui.py +++ b/gui-project/main_ui.py @@ -1,4 +1,5 @@ from typing import final +from warnings import showwarning from nicegui import ui import os @@ -6,7 +7,9 @@ import random import sys from cards import CardUI, CardComponent from flashcards import Session, SCHEDULERS + FLASHCARDS_ROOT="/home/andreear/git/schwiizertuutsch/flashcards" +STATE_FILE="/home/andreear/git/flashcards/state.txt" class MainUI(): def __init__(self): @@ -46,7 +49,7 @@ class MainUI(): async def prepare_session(self, run_mode : str): self.header.clear() - + self.run_mode = run_mode if run_mode == "practice": self.header.classes("bg-positive") else: @@ -69,43 +72,55 @@ class MainUI(): async def start_session(self): ui.notify("Started session") - print("My config") + # initialize cards paths cards_paths = [os.path.join(FLASHCARDS_ROOT, x) for x in self.fcards] - session = Session("brutal", cards_paths, - "/home/andreear/git/flashcards/state.txt") - + # initialize session + session = Session("brutal", cards_paths, STATE_FILE) + # initialize final score final_score = 0 + # set ui.colors to be used ui.colors(frontc='#bcdbc6', back="#c4bcdb", final="#9cfcfc") - for i, card in enumerate(session.practice(self.nb_questions)): - self.header_element.set_content(f"Currently in **practice** mode\n\nQuestion {i+1}/{int(self.nb_questions_ui.value)}") - # print(self.header_element.content) - prompt_type_binary = self.prompt_type.lower() - if self.prompt_type.lower() == "random": - random_nb = random.randint(0, sys.maxsize) - if random_nb % 2 == 0: - prompt_type_binary = "front" - else: - prompt_type_binary = "back" - - if prompt_type_binary == "front": - card_component = CardComponent(card.front, card.back) - else: - card_component = CardComponent(card.back, card.front) - - card_ui = CardUI(self.root_ui, card_component) - - card_ui.show_front() - await card_ui.is_answered() - if card_ui.correctly_answered: - final_score += 1 - card_ui.hide_card() + # Start session + if self.run_mode == "practice": + for i, card in enumerate(session.practice(self.nb_questions)): + correctly_answered = await self.showCard(i, card) + final_score += 1 if correctly_answered else 0 + elif self.run_mode == "test": + for i, (card, correct) in enumerate(session.test(self.nb_questions)): + correctly_answered = await self.showCard(i, card) + correct(correctly_answered) + final_score += 1 if correctly_answered else 0 + + # Wrap up session with self.root_ui: with ui.card(align_items="center").classes("bg-final"): ui.markdown("## Finished") ui.label(f"Your final score is: {final_score}/{self.nb_questions}") ui.label("To start again, refresh the page") + async def showCard(self, idx : int, card): + self.header_element.set_content( + f"Currently in **{self.run_mode}** mode\n\nQuestion {idx + 1}/{int(self.nb_questions_ui.value)}") + prompt_type_binary = self.prompt_type.lower() + if self.prompt_type.lower() == "random": + random_nb = random.randint(0, sys.maxsize) + if random_nb % 2 == 0: + prompt_type_binary = "front" + else: + prompt_type_binary = "back" + + if prompt_type_binary == "front": + card_component = CardComponent(card.front, card.back) + else: + card_component = CardComponent(card.back, card.front) + + card_ui = CardUI(self.root_ui, card_component) + + card_ui.show_front() + await card_ui.is_answered() + card_ui.hide_card() + return card_ui.correctly_answered -- cgit v1.2.3