data table as widget

This commit is contained in:
Will McGugan
2022-06-25 14:55:19 +01:00
parent 4f02f17a38
commit 82358fdb7b
4 changed files with 43 additions and 21 deletions

View File

@@ -6,7 +6,7 @@ from rich.text import Text
from textual.app import App
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static
from textual.widgets import Static, DataTable
CODE = '''
class Offset(NamedTuple):
@@ -101,6 +101,7 @@ class BasicApp(App, css_path="basic.css"):
def on_mount(self):
"""Build layout here."""
table = DataTable()
self.scroll_to_target = Tweet(TweetBody())
self.mount(
header=Static(
@@ -114,6 +115,7 @@ class BasicApp(App, css_path="basic.css"):
Static(Syntax(CODE, "python"), classes="code"),
classes="scrollable",
),
table,
Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(),
@@ -135,6 +137,17 @@ class BasicApp(App, css_path="basic.css"):
Widget(classes="content"),
),
)
table.add_column("Foo", width=80)
table.add_column("Bar", width=50)
table.add_column("Baz", width=40)
table.zebra_stripes = True
for n in range(100):
table.add_row(
f"{n} This is an example of a [b]DataTable widget[/b] within a larger [bold magenta]Textual UI",
"Cells may contain just about any kind of data",
"Where there is a Will there is a Way",
height=1,
)
async def on_key(self, event) -> None:
await self.dispatch_key(event)