You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm playing around with textual and try to implement a sortable rich Table widget where column headers can be clicked to sort by that column ascending/descending.
Since Column headers can be any renderable, my naive approach was:
from rich.console import RenderableType
from rich.table import Column, Table
from textual.app import App
from textual.widget import Widget
from textual.widgets import Button, ButtonPressed
class SortableTable(Widget):
"""rich table but with sortable columns"""
table: Table = None
def __init__(self, *headers, label=None, **kwargs):
super().__init__(**kwargs)
# list of columns with buttons to pass to Table() as headers
button_columns: list[Column] = []
# create buttons from headers
for header in headers:
# create button
button = Button(label=header)
# create column with button as renderable header
column = Column(header=button)
# append to list
button_columns += [ column ]
# create table
self.table = Table(*button_columns, title=label, **kwargs)
def handle_button_pressed(self, message: ButtonPressed) -> None:
"""A message sent by the button widget"""
self.app.log(message)
def render(self) -> RenderableType:
return self.table
def add_row(self, *args, **kwargs):
return self.table.add_row(*args, **kwargs)
class MyApp(App):
async def on_mount(self) -> None:
table = SortableTable("col 1", "col 2", "col 3")
table.add_row("foo", "bar", "baz")
table.add_row("bla", "blubb", "blubber")
await self.view.dock(table)
if __name__ == "__main__":
MyApp.run()
This renders correctly but of course handle_button_pressed is never called upon click since the buttons were not mounted anywhere. on_click works as expected.
What would be the best practice way to have SortableTable receive ButtonPressed events?
Or am I doing it all wrong and there's a better approach to combine a rich element with a textual element in general?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm playing around with textual and try to implement a sortable rich Table widget where column headers can be clicked to sort by that column ascending/descending.
Since Column headers can be any renderable, my naive approach was:
This renders correctly but of course
handle_button_pressed
is never called upon click since the buttons were not mounted anywhere.on_click
works as expected.What would be the best practice way to have SortableTable receive ButtonPressed events?
Or am I doing it all wrong and there's a better approach to combine a rich element with a textual element in general?
Beta Was this translation helpful? Give feedback.
All reactions