Basic TUI using textual

This commit is contained in:
3wc 2023-10-27 01:13:08 +01:00
parent 98aee02ee8
commit b5e486020e
2 changed files with 66 additions and 0 deletions

View File

@ -700,6 +700,13 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
output_file.close()
@cli.command()
def app():
from .app import HamsterToolsApp
app = HamsterToolsApp(db_cursor=c)
app.run()
@cli.command()
def hamster():
click.echo('🐹')

59
hamstertools/app.py Normal file
View File

@ -0,0 +1,59 @@
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()