Skip to content

Commit

Permalink
Improved table interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudy committed Mar 22, 2024
1 parent eb3e0ee commit 3568f43
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/bootlace/table/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(self, decorated_classes: Iterable[str] | None = None) -> None:
else:
self.decorated_classes = set(decorated_classes)

def render(self, items: list[Any]) -> tags.html_tag:
def __call__(self, items: list[Any]) -> tags.html_tag:
table = tags.table(cls="table")
table.classes.add(*self.decorated_classes)
thead = tags.thead()
Expand All @@ -127,8 +127,8 @@ def render(self, items: list[Any]) -> tags.html_tag:
id = getattr(item, "id", None)
tr = tags.tr(id=f"item-{id}" if id else None)
for column in self.columns.values():
td = column.cell(item)
tr.add(td)
cell = column.cell(item)
tr.add(tags.td(cell))
tbody.add(tr)
table.add(tbody)
return table
11 changes: 6 additions & 5 deletions src/bootlace/table/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import attrs
from dominate import tags
from dominate.util import text
from flask import url_for

from bootlace.icon import Icon
Expand All @@ -13,7 +14,7 @@
class Column(ColumnBase):

def cell(self, value: Any) -> tags.html_tag:
return tags.td(getattr(value, self.attribute))
return text(str(getattr(value, self.attribute)))


@attrs.define
Expand All @@ -22,7 +23,7 @@ class EditColumn(ColumnBase):

def cell(self, value: Any) -> tags.html_tag:
id = getattr(value, "id", None)
return tags.td(tags.a(getattr(value, self.attribute), href=url_for(self.endpoint, id=id)))
return tags.a(getattr(value, self.attribute), href=url_for(self.endpoint, id=id))


@attrs.define
Expand All @@ -33,12 +34,12 @@ class CheckColumn(ColumnBase):

def cell(self, value: Any) -> tags.html_tag:
if getattr(value, self.attribute):
return tags.td(as_tag(self.yes))
return tags.td(as_tag(self.no))
return as_tag(self.yes)
return as_tag(self.no)


@attrs.define
class Datetime(ColumnBase):

def cell(self, value: Any) -> tags.html_tag:
return tags.td(getattr(value, self.attribute).isoformat())
return text(getattr(value, self.attribute).isoformat())
4 changes: 2 additions & 2 deletions tests/table/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_basic_table(items: list[Item], table: type[Table]) -> None:
assert len(table.columns) == 2

table = table()
rendered = table.render(items).render()
rendered = table(items).render()

print(rendered)

Expand Down Expand Up @@ -60,7 +60,7 @@ def test_basic_table(items: list[Item], table: type[Table]) -> None:
def test_table_with_decorated_classes(items: list[Item], table: type[Table]) -> None:

table = table(decorated_classes=["table-striped", "table-bordered"])
rendered = table.render(items).render()
rendered = table(items).render()

print(rendered)

Expand Down

0 comments on commit 3568f43

Please sign in to comment.