mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Default cell formatter fixes
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import csv
|
||||
import io
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.widgets import DataTable
|
||||
|
||||
CSV = """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"""
|
||||
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):
|
||||
@@ -21,7 +21,7 @@ class TableApp(App):
|
||||
|
||||
def on_mount(self) -> None:
|
||||
table = self.query_one(DataTable)
|
||||
rows = csv.reader(io.StringIO(CSV))
|
||||
rows = iter(ROWS)
|
||||
table.add_columns(*next(rows))
|
||||
table.add_rows(rows)
|
||||
|
||||
|
||||
@@ -88,19 +88,21 @@ class CellKey(NamedTuple):
|
||||
column_key: ColumnKey | str
|
||||
|
||||
|
||||
def default_cell_formatter(obj: object) -> RenderableType | None:
|
||||
def default_cell_formatter(obj: object) -> RenderableType:
|
||||
"""Format a cell in to a renderable.
|
||||
|
||||
Args:
|
||||
obj: Data for a cell.
|
||||
|
||||
Returns:
|
||||
A renderable or None if the object could not be rendered.
|
||||
A renderable to be displayed which represents the data.
|
||||
"""
|
||||
if isinstance(obj, str):
|
||||
return Text.from_markup(obj)
|
||||
if isinstance(obj, float):
|
||||
return f"{obj:.2f}"
|
||||
if not is_renderable(obj):
|
||||
return None
|
||||
return str(obj)
|
||||
return cast(RenderableType, obj)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user