Outsource nested HTML table creation to helper function

This commit is contained in:
Daniel Rojas 2020-07-05 20:36:02 +02:00
parent 0252476248
commit e1e665583f
2 changed files with 16 additions and 9 deletions

View File

@ -4,7 +4,7 @@
from wireviz.DataClasses import Connector, Cable
from graphviz import Graph
from wireviz import wv_colors
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, nested, flatten2d, index_if_list, html_line_breaks, graphviz_line_breaks, remove_line_breaks
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, nested, nested_html_table, flatten2d, index_if_list, html_line_breaks, graphviz_line_breaks, remove_line_breaks
from collections import Counter
from typing import List
@ -67,14 +67,7 @@ class Harness:
[html_line_breaks(connector.notes)]]
rows = [list(filter(None, row)) for row in rows] # remove missing attributes
html = '<table border="0" cellspacing="0" cellpadding="0">'
for row in rows:
if len(row) > 0:
html = f'{html}<tr><td><table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr>'
for cell in row:
html = f'{html}<td balign="left">{cell}</td>'
html = f'{html}</tr></table></td></tr>'
html = f'{html}</table>'
html = nested_html_table(rows)
if connector.color: # add color bar next to color info, if present
colorbar = f' bgcolor="{wv_colors.translate_color(connector.color, "HEX")}" width="4"></td>' # leave out '<td' from string to preserve any existing attributes of the <td> tag

View File

@ -44,6 +44,20 @@ def nested(inp):
l.append(str(x))
return '|'.join(l)
def nested_html_table(rows):
# input: list of lists
# output: a parent table with one child table per parent list item
# purpose: create the appearance of one table, where cell widths are independent between rows
html = '<table border="0" cellspacing="0" cellpadding="0">'
for row in rows:
if len(row) > 0:
html = f'{html}<tr><td><table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr>'
for cell in row:
html = f'{html}<td balign="left">{cell}</td>'
html = f'{html}</tr></table></td></tr>'
html = f'{html}</table>'
return html
def int2tuple(inp):
if isinstance(inp, tuple):