Update row style from DataTable when pressing a given key #2184
-
|
Hi! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
This recent discussion might be helpful. You can apply styles programmatically with rich renderables, but not with CSS as this works at the widget level. Here's a quick example, though there might be a more efficient way: from textual.app import App, ComposeResult
from textual.coordinate import Coordinate
from textual.widgets import DataTable, Footer
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 TableApp(App):
BINDINGS = [
("a", "style_highlighted_row", "Apply styling to highlighted row"),
]
def compose(self) -> ComposeResult:
yield DataTable()
yield Footer()
def on_mount(self) -> None:
table = self.query_one(DataTable)
rows = iter(ROWS)
table.add_columns(*next(rows))
table.add_rows(rows)
def action_style_highlighted_row(self) -> None:
table = self.query_one(DataTable)
row_idx: int = table.cursor_row
row_cells = table.get_row_at(row_idx)
for col_idx, cell_value in enumerate(row_cells):
cell_coordinate = Coordinate(row_idx, col_idx)
table.update_cell_at(
cell_coordinate,
f"[red]{cell_value}[/red]",
)
app = TableApp()
if __name__ == "__main__":
app.run()
Sorry but I'm not sure I understand what you mean here - could you provide an example? [EDIT: Forgot to add the footer!] |
Beta Was this translation helpful? Give feedback.
This recent discussion might be helpful. You can apply styles programmatically with rich renderables, but not with CSS as this works at the widget level.
Here's a quick example, though there might be a more efficient way: