Tables¶
- table(items_list, style={}, attr_list=[])¶
Generates an HTML <table> element from a list of rows. Supports headers, inline styling, and advanced cell configurations such as colspan, rowspan, and common_style.
- Parameters:
items_list (list) – A list of rows. Each row can be: - A tuple: treated as a header row using <th> - A list: treated as normal table rows - A dict: contains styling or attributes for the row
style (dict) – Optional dictionary of CSS styles applied to the <table> element.
attr_list (list of str) – Optional list of HTML attributes (as raw strings) applied to the <table> tag.
- Returns:
Writes HTML <table> code directly to the global file object f.
Row Behavior:
If the first row is a tuple, it is considered a header and rendered using <th>.
Rows can have individual styles if passed as dictionaries with keys like rowspan or using stylizer.
Cell Behavior:
Each cell can be:
A string: Rendered as <td>value</td>
A tuple (value,): Rendered as <th>value</th>
A dictionary with: -
colspan: Specifies how many columns the cell spans. -rowspan: Specifies how many rows the cell spans. -common_style: A style dictionary processed in a separate thread usingedit_file_4style.
Examples¶
Simple Table with Header
table([ ("Name", "Age", "City"), ["Alice", "25", "Mumbai"], ["Bob", "30", "Delhi"] ])
Table with `colspan` and `rowspan`
table([ ("Name", "Details"), ["Alice", {"colspan": 2, "style": {"color": "red"}}], ["Bob", {"rowspan": 2}], ["Bob Again", "Still Bob"] ])
Styled Table with Attributes
table( [ ("Fruit", "Color"), ["Apple", "Red"], ["Banana", "Yellow"] ], style={"border": "2px solid black", "width": "100%"}, attr_list=["border=1", "align=center"] )
Notes¶
common_style triggers a call to
edit_file_4stylein a separate thread.Do not pass only colspan/rowspan without some meaningful content or styling — it may render as an empty cell.
Ensure that cell types are consistent to maintain proper table formatting.