from textual.app import App, ComposeResult from textual.widgets import Header, Footer, DataTable ROWS = [ ("lane", "swimmer", "country", "time"), (4, "Joseph Schooling", "Singapore", 50.39), (2, "Michael Phelps", "United States", 51.14), (5, "Chad le Clos", "South Africa", 51.14), (6, "László Cseh", "Hungary", 51.14), (3, "Li Zhuhao", "China", 51.26), (8, "Mehdy Metella", "France", 51.58), (7, "Tom Shields", "United States", 51.73), (1, "Aleksandr Sadovnikov", "Russia", 51.84), (10, "Darren Burns", "Scotland", 51.84), ] class HamsterToolsApp(App): """A Textual app to manage stopwatches.""" BINDINGS = [ ("q", "quit", "Quit"), ("m", "mark", "Mark"), ] def __init__(self, db_cursor): self.db_cursor = db_cursor super().__init__() def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() yield DataTable() yield Footer() def on_mount(self) -> None: table = self.query_one(DataTable) sql = ''' SELECT categories.id, categories.name, activities.id, activities.name FROM activities LEFT JOIN categories ON activities.category_id = categories.id ''' results = self.db_cursor.execute(sql) results = [[cell or "" for cell in row] for row in self.db_cursor.fetchall()] columns = table.add_columns("category id","category","activity ID","activity") table.add_rows(results) table.sort(columns[1], columns[3]) table.cursor_type = "row" def action_quit(self) -> None: self.exit()