summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gui-project/cards.py17
-rw-r--r--gui-project/main_ui.py79
2 files changed, 59 insertions, 37 deletions
diff --git a/gui-project/cards.py b/gui-project/cards.py
index 42a08b0..ededff9 100644
--- a/gui-project/cards.py
+++ b/gui-project/cards.py
@@ -19,16 +19,13 @@ 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
self.card = card
self.is_resolved = asyncio.Event()
self.init_card()
+ self.correctly_answered = False
def init_card(self):
with self.parent_ui:
@@ -38,19 +35,19 @@ class CardUI():
def init_front(self):
with self.row_parent:
- with ui.card().classes('bg-frontc') as self.front:
+ with ui.card().classes('bg-frontc w-[600px] mx-auto') as self.front:
ui.markdown("**Front**")
ui.separator()
- ui.markdown(self.card.get_front())
+ ui.markdown(self.card.get_front()).style('white-space: pre-wrap')
ui.button("Revert", on_click=lambda: self.revert_card())
self.front.set_visibility(False)
def init_back(self):
with self.row_parent:
- with ui.card().classes('bg-back') as self.back:
+ with ui.card().classes('bg-back w-[600px] mx-auto') as self.back:
ui.markdown("_Back_")
ui.separator()
- ui.markdown(self.card.get_back())
+ ui.markdown(self.card.get_back()).style('white-space: pre-wrap')
with ui.button_group():
ui.button("Got it", on_click=lambda: self.user_clicked_correct())
ui.button("Ain't got it", on_click=lambda: self.user_clicked_incorrect())
@@ -65,15 +62,13 @@ class CardUI():
self.back.set_visibility(True)
def revert_card(self):
- ui.notify("Showing back")
self.show_back()
def user_clicked_correct(self):
- ui.notify("You got this")
+ self.correctly_answered = True
self.is_resolved.set()
def user_clicked_incorrect(self):
- ui.notify("Keep at it!")
self.is_resolved.set()
async def is_answered(self):
diff --git a/gui-project/main_ui.py b/gui-project/main_ui.py
index f6351b0..fcb1f5a 100644
--- a/gui-project/main_ui.py
+++ b/gui-project/main_ui.py
@@ -3,8 +3,10 @@ import os
import random
import sys
from cards import CardUI, CardComponent
-from flashcards import Session, SCHEDULERS
+from flashcards import Session
+
FLASHCARDS_ROOT="/home/andreear/git/schwiizertuutsch/flashcards"
+STATE_FILE="/home/andreear/git/flashcards/state.txt"
class MainUI():
def __init__(self):
@@ -16,7 +18,7 @@ class MainUI():
# ========= start initializing UI ===========
with ui.header() as self.header:
- self.header_element = ui.label("Session Configuration")
+ self.header_element = ui.markdown("Session Configuration")
self.root_ui = ui.row().classes("fixed-center")
with self.root_ui:
with ui.card(align_items="baseline") as self.session_configuration:
@@ -44,15 +46,14 @@ 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:
self.header.classes("bg-negative")
with self.header:
- self.header_element = ui.markdown(f"Currently in **{run_mode}** mode")
-
+ self.header_element = ui.markdown(f"Currently in **{run_mode}** mode\nQuestion 1/{self.nb_questions_ui.value}")
self.run_mode = run_mode
# read values from UI and set session configuration
self.nb_questions = int(self.nb_questions_ui.value)
@@ -68,29 +69,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")
-
- ui.colors(frontc='#bcdbc6', back="#c4bcdb")
- for i, card in enumerate(session.practice(self.nb_questions)):
- 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)
+ # 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")
+
+ # 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:
- card_component = CardComponent(card.back, card.front)
+ 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
- card_ui = CardUI(self.root_ui, card_component)
- card_ui.show_front()
- await card_ui.is_answered()
- card_ui.hide_card()