@@ -72,35 +72,50 @@ def add_rows(self, rows: list[dict[str, str | int | float | bool]]):
7272 for row in rows :
7373 self .add_row (row )
7474
75- def add_row (self , row : dict [str , str | int | float | bool ]):
75+ def add_row (self , row : dict [str , str | int | float | bool ] | list [ str ] ):
7676 """Add a row to the table.
7777
7878 Args:
79- row (dict[str, str | int | float | bool]): Row to add
79+ row (dict[str, str | int | float | bool], list[str] ): Row to add
8080 """
8181
82- # Check that all the keys in the row are in the headers
83- for key in row .keys ():
84- if key not in self .headers :
85- if self .flexible_headers :
86- self .headers .append (key )
87- else :
88- raise ValueError (
89- f"Key { key } not in headers and flexible_headers is False"
90- )
82+ # If row is a list, convert it to a dict, using the headers as keys
83+ if isinstance (row , list ):
84+ if len (row ) != len (self .headers ):
85+ raise ValueError (
86+ f"Row length ({ len (row )} ) does not match header length ({ len (self .headers )} )"
87+ )
88+ row = dict (zip (self .headers , row ))
89+ # If row is a dict, check that all the keys are in the headers, if not, raise error
90+ elif isinstance (row , dict ):
91+ for key in row .keys ():
92+ if key not in self .headers :
93+ if self .flexible_headers :
94+ self .headers .append (key )
95+ else :
96+ raise ValueError (
97+ f"Key { key } not in headers and flexible_headers is False"
98+ )
9199 # Check that all the headers are in the row
92100 for header in self .headers :
93101 if header not in row .keys ():
94102 row [header ] = ""
95103
96104 self .rows .append (row )
97105
98- def sort_table (self ):
106+ def sort_table (self , disable_convert : bool = False ):
99107 """Sort the table by the sort_key."""
100108 if self .sort_key :
101109 # If multiple sort keys are provided, prioritize the first one, then the second, etc.
102110 sort_keys = self .sort_key .split ("," )
103111 for sort_key in sort_keys :
112+ if disable_convert :
113+ self .rows = sorted (
114+ self .rows ,
115+ key = lambda row : row .get (sort_key , "" ), # pylint: disable=cell-var-from-loop
116+ reverse = self .sort_reverse ,
117+ )
118+ break
104119 if all ( # pylint: disable=use-a-generator
105120 [
106121 row .get (sort_key , "" )
@@ -322,7 +337,7 @@ def __init__(self, title: Header | str, **kwargs):
322337 self .title = title
323338 self .content = kwargs .get ("content" , "" )
324339
325- def add (self , content : str | Table | List | Image | Link ):
340+ def add (self , content : str | Table | List | Image | Link | Header ):
326341 """Add content to the section.
327342
328343 Args:
0 commit comments