import asyncio from nicegui import ui class CardComponent(): def __init__(self, front : str, back : str): if front is None or front == "": raise Exception("Cannot have empty front on the card") if back is None or back == "": raise Exception("Cannot have empty back on the card") self.front = front self.back = back def get_front(self): return self.front def get_back(self): return self.back 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: self.row_parent = ui.row() self.init_front() self.init_back() def init_front(self): with self.row_parent: with ui.card().classes('bg-frontc w-[600px] mx-auto') as self.front: ui.markdown("**Front**") ui.separator() 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 w-[600px] mx-auto') as self.back: ui.markdown("_Back_") ui.separator() 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()) self.back.set_visibility(False) def show_front(self): self.back.set_visibility(False) self.front.set_visibility(True) def show_back(self): self.front.set_visibility(False) self.back.set_visibility(True) def revert_card(self): self.show_back() def user_clicked_correct(self): self.correctly_answered = True self.is_resolved.set() def user_clicked_incorrect(self): self.is_resolved.set() async def is_answered(self): await self.is_resolved.wait() def hide_card(self): self.row_parent.delete()