diff options
author | AndreeaRadoescu <randreea23@gmail.com> | 2024-10-14 23:07:09 +0200 |
---|---|---|
committer | AndreeaRadoescu <randreea23@gmail.com> | 2024-10-14 23:07:09 +0200 |
commit | 2d385a869488c54513c58e95cd78bb3053a41208 (patch) | |
tree | 2f46b4dfbd270886460f49cb041b4958a74dffc2 /gui-project | |
parent | 29d891f67252382d976a025d5c42488161d08f0f (diff) |
fix ui colors for cards, moved all functionality in MainUI class, added session configuration UI
Diffstat (limited to 'gui-project')
-rw-r--r-- | gui-project/cards.py | 6 | ||||
-rw-r--r-- | gui-project/main.py | 24 | ||||
-rw-r--r-- | gui-project/main_ui.py | 96 |
3 files changed, 103 insertions, 23 deletions
diff --git a/gui-project/cards.py b/gui-project/cards.py index f175aa1..42a08b0 100644 --- a/gui-project/cards.py +++ b/gui-project/cards.py @@ -20,6 +20,9 @@ class CardComponent(): return self.back +# ui.colors(back="#c4bcdb") + + class CardUI(): def __init__(self, parent_ui, card : CardComponent): self.parent_ui = parent_ui @@ -35,7 +38,6 @@ class CardUI(): def init_front(self): with self.row_parent: - ui.colors(frontc='#bcdbc6') with ui.card().classes('bg-frontc') as self.front: ui.markdown("**Front**") ui.separator() @@ -45,7 +47,6 @@ class CardUI(): def init_back(self): with self.row_parent: - ui.colors(back="#c4bcdb") with ui.card().classes('bg-back') as self.back: ui.markdown("_Back_") ui.separator() @@ -55,7 +56,6 @@ class CardUI(): ui.button("Ain't got it", on_click=lambda: self.user_clicked_incorrect()) self.back.set_visibility(False) - def show_front(self): self.back.set_visibility(False) self.front.set_visibility(True) diff --git a/gui-project/main.py b/gui-project/main.py index 7e52c77..d98b2c7 100644 --- a/gui-project/main.py +++ b/gui-project/main.py @@ -1,26 +1,10 @@ from nicegui import ui -from cards import CardUI, CardComponent -from flashcards import Session, SCHEDULERS +from main_ui import MainUI + +FLASHCARDS_ROOT="/home/andreear/git/schwiizertuutsch/flashcards" @ui.page("/") def main_page(): - with ui.row() as main_row: - start_button = ui.button("Start", on_click=lambda: start_session(main_row, start_button)) - - -async def start_session(parent_ui, start_button): - ui.notify("Started session") - session = Session("brutal", ["/home/andreear/git/schwiizertuutsch/flashcards/diverse.fcard"], "/home/andreear/git/flashcards/state.txt") - - start_button.set_visibility(False) - for i, card in enumerate(session.practice(20)): - # print(f"Showing card id {i}") - # print(f"Card front {card.front}") - # print(f"Card back {card.back}") - card_component = CardComponent(card.front, card.back) - card_ui = CardUI(parent_ui, card_component) - card_ui.show_front() - await card_ui.is_answered() - card_ui.hide_card() + MainUI() ui.run(port=3011)
\ No newline at end of file diff --git a/gui-project/main_ui.py b/gui-project/main_ui.py new file mode 100644 index 0000000..f6351b0 --- /dev/null +++ b/gui-project/main_ui.py @@ -0,0 +1,96 @@ +from nicegui import ui +import os +import random +import sys +from cards import CardUI, CardComponent +from flashcards import Session, SCHEDULERS +FLASHCARDS_ROOT="/home/andreear/git/schwiizertuutsch/flashcards" + +class MainUI(): + def __init__(self): + # ===== default value for a session ======== + self.nb_questions = 1 + self.prompt_type = "Front" + self.fcards = ["saetze-allgemein.fcard"] + self.run_mode = "practice" + + # ========= start initializing UI =========== + with ui.header() as self.header: + self.header_element = ui.label("Session Configuration") + self.root_ui = ui.row().classes("fixed-center") + with self.root_ui: + with ui.card(align_items="baseline") as self.session_configuration: + self.initialize_session_ui() + + def initialize_session_ui(self): + all_fcards = os.listdir(FLASHCARDS_ROOT) + + with ui.row(align_items="center"): + ui.label("Number of questions:") + self.nb_questions_ui = ui.number(placeholder='e.g.: 20', min=1, step=1, precision=0, value=15).props( + 'rounded outlined dense') + with ui.row(align_items="center"): + ui.label("Prompt type:") + self.prompt_sel_ui = ui.select(["Front", "Back", "Random"], value="Back") + with ui.row(align_items="center"): + ui.label("Select cards to play from:") + self.cards_sel_ui = ui.select(all_fcards, multiple=True, value=all_fcards[:2]) \ + .classes('w-64').props('use-chips') + with ui.button_group(): + ui.button("Practice mode", color="positive", + on_click=lambda: self.prepare_session("practice")) + ui.button("Test mode", color="negative", + on_click=lambda: self.prepare_session("test")) + + async def prepare_session(self, run_mode : str): + self.header.clear() + + 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.run_mode = run_mode + # read values from UI and set session configuration + self.nb_questions = int(self.nb_questions_ui.value) + self.prompt_type = self.prompt_sel_ui.value + self.fcards = self.cards_sel_ui.value + + assert type(self.fcards) is list + assert type(self.nb_questions) is int + assert type(self.prompt_type) is str + + self.session_configuration.set_visibility(False) + await self.start_session() + + async def start_session(self): + ui.notify("Started session") + print("My config") + 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) + 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() + |