Ludic Logo
Edit

Tables

These components located in ludic.catalog.tables are in an experimental mode. There is the possibility to automatically create tables even containing form fields and actions from annotations, but it is far from production-ready.

Creating a Table

#

The Table component accepts a TableHead as the first item and any number of TableRow items.

First NameLast NameAge
JohnDoe42
JaneSmith23
1   from ludic.catalog.tables import Table, TableHead, TableRow
2   
3   Table(
4       TableHead("First Name", "Last Name","Age"),
5       TableRow("John", "Doe", 42),
6       TableRow("Jane", "Smith", 23),
7       classes=["text-align-center"],
8   )

You can also specify different types of header and body. It might happen that you created your own component representing the table head and body:

1   from ludic.catalog.tables import Table
2   
3   from your_app.components import PersonHead, PersonRow
4   
5   Table[PersonHead, PersonRow](
6       PersonHead("Name", "Age"),
7       PersonRow("John", 42),
8       PersonRow("Jane", 23),
9   )

Note that the type specification in the square brackets is only for the type-checkers to pass.

Generating Table Rows

#

The create_rows function can be used to generate rows in the table based on given specification. Here is an example:

 1   from typing import Annotated
 2   
 3   from ludic.attrs import Attrs
 4   from ludic.catalog.tables import ColumnMeta, Table, create_rows
 5   
 6   class PersonAttrs(Attrs):
 7       id: Annotated[int, ColumnMeta(label="ID")]
 8       name: Annotated[str, ColumnMeta(label="Full Name")]
 9       email: Annotated[str, ColumnMeta(label="Email")]
10   
11   people: list[PersonAttrs] = [
12       {"id": 1, "name": "John Doe", "email": "john@j.com"},
13       {"id": 2, "name": "Jane Smith", "email": "jane@s.com"},
14   ]
15   
16   rows = create_rows(people, spec=PersonAttrs)
17   table = Table(*rows)

This would render the following table:

IDFull NameEmail
1John Doejohn@j.com
2Jane Smithjane@s.com

Table With Form Fields

#

There is also an experimental support for tables containing form fields.The way it works is that you wrap your table in a form and use the create_rows function to generate the rows. Here is an example:

IdFull NameEmailActive
1John Doejohn@j.com
2Jane Smithjane@s.com
 1   from typing import Annotated
 2   
 3   from ludic.attrs import Attrs
 4   from ludic.catalog.tables import ColumnMeta, Table, create_rows
 5   
 6   class ContactAttrs(Attrs):
 7       id: Annotated[int, ColumnMeta(identifier=True)]
 8       name: Annotated[str, ColumnMeta(label="Full Name")]
 9       email: Annotated[str, ColumnMeta(label="Email")]
10       active: Annotated[
11           bool,
12           ColumnMeta(
13               label="Active", kind=FieldMeta(kind="checkbox", label=None)
14           )
15       ]
16   
17   contacts: list[ContactAttrs] = [
18       {"id": 1, "name": "John Doe", "email": "john@j.com", "active": True},
19       {"id": 2, "name": "Jane Smith", "email": "jane@s.com", "active": False},
20   ]
21   
22   rows = create_rows(contacts, spec=ContactAttrs)
23   table = Table(*rows)

The role of the identifier column is to be able to parse the form data using the ListParser function as documented in the Parsers section of the documentation.

Made with Ludic and HTMX and 🐍 • DiscordGitHub