File: data_table_style_order.py

package info (click to toggle)
textual-fastdatatable 0.12.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,856 kB
  • sloc: python: 3,461; makefile: 24
file content (65 lines) | stat: -rw-r--r-- 1,835 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations

from textual.app import App, ComposeResult
from textual.widgets import Label
from textual_fastdatatable import ArrowBackend, DataTable
from typing_extensions import Literal

data = [
    "Severance",
    "Foundation",
    "Dark",
]


def make_datatable(
    foreground_priority: Literal["css", "renderable"],
    background_priority: Literal["css", "renderable"],
) -> DataTable:
    backend = ArrowBackend.from_pydict(
        {"Movies": [f"[red on blue]{row}" for row in data]}
    )
    table = DataTable(
        backend=backend,
        cursor_foreground_priority=foreground_priority,
        cursor_background_priority=background_priority,
    )
    table.zebra_stripes = True
    return table


class DataTableCursorStyles(App):
    """Regression test snapshot app which ensures that styles
    are layered on top of each other correctly in the DataTable.
    In this example, the colour of the text in the cells under
    the cursor should not be red, because the CSS should be applied
    on top."""

    CSS = """
    DataTable {margin-bottom: 1;}
    DataTable > .datatable--cursor {
        color: $secondary;
        background: $success;
        text-style: bold italic;
    }
"""

    def compose(self) -> ComposeResult:
        priorities: list[
            tuple[Literal["css", "renderable"], Literal["css", "renderable"]]
        ] = [
            ("css", "css"),
            ("css", "renderable"),
            ("renderable", "renderable"),
            ("renderable", "css"),
        ]
        for foreground, background in priorities:
            yield Label(f"Foreground is {foreground!r}, background is {background!r}:")
            table = make_datatable(foreground, background)
            yield table


app = DataTableCursorStyles()

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