Skip to content

Commit cc23308

Browse files
committed
Enhance table HTML header and input validation in PyRevitOutputWindow
- Added border_style parameter to table_html_header method for customizable table header styling. - Updated docstrings for table_html_header and table_check_input_lists methods to include examples and clarify argument types. - Improved input validation in table_check_input_lists to ensure consistent list lengths and provide detailed error messages.
1 parent af3d76b commit cc23308

File tree

1 file changed

+90
-24
lines changed

1 file changed

+90
-24
lines changed

pyrevitlib/pyrevit/output/__init__.py

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,46 @@ def table_html_header(self, columns, table_uid, border_style):
549549
"""Helper method for print_table() method
550550
551551
Return html <thead><tr><th> row for the table header
552+
552553
Args:
553554
columns (list[str]): list of column names
554555
table_uid (str): a unique ID for this table's CSS classes
556+
border_style (str): CSS border style string for table cells
555557
558+
Returns:
559+
str: HTML string containing the table header row
560+
561+
Examples:
562+
```python
563+
output = pyrevit.output.get_output()
564+
565+
# Basic usage - called internally by print_table()
566+
columns = ["Name", "Age", "City"]
567+
table_uid = 1
568+
border_style = "border: 1px solid black;"
569+
header_html = output.table_html_header(
570+
columns, table_uid, border_style)
571+
# Returns: "<thead><tr style='border: 1px solid black;'>" \
572+
# "<th class='head_title-1-0' align='left'>Name</th>" \
573+
# "<th class='head_title-1-1' align='left'>Age</th>" \
574+
# "<th class='head_title-1-2' align='left'>City</th>" \
575+
# "</tr></thead>"
576+
577+
# Without border style
578+
header_html = output.table_html_header(
579+
columns, table_uid, "")
580+
# Returns: "<thead><tr>" \
581+
# "<th class='head_title-1-0' align='left'>Name</th>" \
582+
# "<th class='head_title-1-1' align='left'>Age</th>" \
583+
# "<th class='head_title-1-2' align='left'>City</th>" \
584+
# "</tr></thead>"
585+
```
556586
"""
557587
html_head = "<thead><tr {}>".format(border_style)
558588
for i, c in enumerate(columns):
559-
html_head += "<th class='head_title-{}-{}' align='left'>{}</th>".format(table_uid, i, c)
589+
html_head += \
590+
"<th class='head_title-{}-{}' align='left'>{}</th>".format(
591+
table_uid, i, c)
560592
# pyRevit original print_table uses align='left'.
561593
# This is now overridden by CSS if specified
562594
html_head += "</tr></thead>"
@@ -565,25 +597,59 @@ def table_html_header(self, columns, table_uid, border_style):
565597

566598

567599
def table_check_input_lists(self,
568-
table_data,
569-
columns,
570-
formats,
571-
input_kwargs):
600+
table_data,
601+
columns,
602+
formats,
603+
input_kwargs):
572604
"""Helper method for print_table() method
573605
574606
Check that the table_data is present and is a list
575607
Check that table_data rows are of the same length
576-
Check that all print_table() kwargs of type list are of the right length
608+
Check that all print_table() kwargs of type list are of correct length
577609
578610
Args:
579-
table_data(iterable[Any]]): The whole table data
580-
columns (list[str]): columns
581-
formats (list[str]): formats
582-
input_kwargs (iterable[Any]]): list of arg lists
611+
table_data (list[list[Any]]): The whole table data as 2D list
612+
columns (list[str]): list of column names
613+
formats (list[str]): list of format strings for each column
614+
input_kwargs (list[list[Any]]): list of additional argument lists
583615
584-
Return:
585-
True if input lists are OK to proceed to build the table
586-
False if not, with a relevant warning message
616+
Returns:
617+
tuple: (bool, str) - (True/False, message) indicating result
618+
619+
Examples:
620+
```python
621+
output = pyrevit.output.get_output()
622+
623+
# Valid table data
624+
table_data = [["John", 25, "NYC"], ["Jane", 30, "LA"]]
625+
columns = ["Name", "Age", "City"]
626+
formats = ["", "{} years", ""]
627+
input_kwargs = [["left", "center", "right"],
628+
["100px", "80px", "120px"]]
629+
630+
is_valid, message = output.table_check_input_lists(
631+
table_data, columns, formats, input_kwargs)
632+
# Returns: (True, "Inputs OK")
633+
634+
# Invalid - mismatched column count
635+
table_data = [["John", 25], ["Jane", 30, "LA"]] # Inconsistent
636+
is_valid, message = output.table_check_input_lists(
637+
table_data, columns, formats, input_kwargs)
638+
# Returns: (False, "Not all rows of table_data are of "
639+
# "equal length")
640+
641+
# Invalid - wrong number of columns
642+
columns = ["Name", "Age"] # Only 2 columns but data has 3
643+
is_valid, message = output.table_check_input_lists(
644+
table_data, columns, formats, input_kwargs)
645+
# Returns: (False, "Column head list length not equal "
646+
# "to data row")
647+
648+
# Invalid - empty table data
649+
is_valid, message = output.table_check_input_lists(
650+
[], columns, formats, input_kwargs)
651+
# Returns: (False, "No table_data list")
652+
```
587653
"""
588654

589655
# First check positional and named keyword args
@@ -607,24 +673,24 @@ def table_check_input_lists(self,
607673

608674
# Next check **kwargs
609675
# Loop through the lists and return if not a list or len not equal
610-
for l in input_kwargs:
611-
if not l: # No kwarg is OK beacause they are optional
676+
for kwarg_list in input_kwargs:
677+
if not kwarg_list: # No kwarg is OK beacause they are optional
612678
continue
613-
if not isinstance(l, list):
614-
return False, "One of the print_table kwargs that should be a list is not a list ({})".format(l)
615-
if len(l) != len_data_row:
679+
if not isinstance(kwarg_list, list):
680+
return False, "One of the print_table kwargs that should be a list is not a list ({})".format(kwarg_list)
681+
if len(kwarg_list) != len_data_row:
616682
return False, "print_table kwarg list length problem (should match {} columns)".format(len_data_row)
617683

618684
return True, "Inputs OK"
619685

620686

621687
def print_table(self,
622-
table_data,
623-
columns=None,
624-
formats=None,
625-
title='',
626-
last_line_style='',
627-
**kwargs):
688+
table_data,
689+
columns=None,
690+
formats=None,
691+
title='',
692+
last_line_style='',
693+
**kwargs):
628694
"""Print provided data in a HTML table in output window.
629695
The same window can output several tables, each with their own formatting options.
630696

0 commit comments

Comments
 (0)