File: data_table_sort.py

package info (click to toggle)
textual-fastdatatable 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,176 kB
  • sloc: python: 3,466; makefile: 29
file content (41 lines) | stat: -rw-r--r-- 1,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from textual.app import App, ComposeResult
from textual.binding import Binding

from textual_fastdatatable import ArrowBackend, DataTable

# Shuffled around a bit to exercise sorting.
ROWS = [
    ("lane", "swimmer", "country", "time"),
    (5, "Chad le Clos", "South Africa", 51.14),
    (4, "Joseph Schooling", "Singapore", 50.39),
    (2, "Michael Phelps", "United States", 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),
    (10, "Darren Burns", "Scotland", 51.84),
    (1, "Aleksandr Sadovnikov", "Russia", 51.84),
]


class TableApp(App):
    BINDINGS = [
        Binding("s", "sort", "Sort"),
    ]

    def compose(self) -> ComposeResult:
        backend = ArrowBackend.from_records(ROWS, has_header=True)
        yield DataTable(backend=backend)

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.focus()

    def action_sort(self):
        table = self.query_one(DataTable)
        table.sort([("time", "ascending"), ("lane", "ascending")])


app = TableApp()
if __name__ == "__main__":
    app.run()