How to remove a column from a DataTable? #2897
-
The docstring for
textual/src/textual/widgets/_data_table.py Line 1274 in c2f0771 But, it's not clear how to actually remove columns, if it's even possible. A very naive approach is just to from textual.app import App, ComposeResult
from textual.widgets import 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 TableApp(App):
def compose(self) -> ComposeResult:
yield DataTable()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_columns(*ROWS[0])
table.add_rows(ROWS[1:])
def on_data_table_header_selected(self, event:DataTable.HeaderSelected):
table = self.query_one(DataTable)
del table.columns[event.column_key] # <- `del` the clicked column
app = TableApp()
if __name__ == "__main__":
app.run() This sort-of works for the right-most column, but sometimes results in visual artefacts. For central columns then we get an exception, I assume because
Is there a more robust way to remove columns? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A |
Beta Was this translation helpful? Give feedback.
A
DataTable.remove_column
method has been added in the latest version announced today!