81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
from bs4 import BeautifulSoup
|
|
|
|
MAIN_STYLE = "display: flex; gap: 50px;"
|
|
|
|
COLUMN_STYLE = "display: flex; flex-direction: column; gap: 12px;"
|
|
|
|
ARRAY_BLOCK_STYLE = "display: flex; align-items: center; gap: 8px;"
|
|
|
|
ARRAY_TABLE_TEMPLATE = "border-collapse: collapse; "
|
|
|
|
LABEL_STYLE = "font-size: 1.2em; font-weight: bold; width: 20px; text-align: center;"
|
|
|
|
TD_STYLE_TEMPLATE = "border: 1px solid black; width: 1.2em; height: 1.2rem; text-align: center; font-size: 0.8em;"
|
|
|
|
TH_STYLE_TEMPLATE = "font-weight: normal; border: none; width: 1.2em; height: 0.3rem; text-align: center; font-size: 0.8em;"
|
|
|
|
def td_style_with_gray(style):
|
|
return style + "background-color: lightgray;"
|
|
|
|
def td_style_with_no_right_border(style):
|
|
return style + "border-right: none;"
|
|
|
|
def create_array_block(soup: BeautifulSoup, label: str, head: list[int] | None, data: list[int]):
|
|
block = soup.new_tag("div", attrs = {'style': ARRAY_BLOCK_STYLE})
|
|
|
|
if label:
|
|
block.append(
|
|
soup.new_tag("span", string=label, attrs={"style": LABEL_STYLE})
|
|
)
|
|
|
|
table = soup.new_tag("table",
|
|
attrs = {"style": ARRAY_TABLE_TEMPLATE}
|
|
)
|
|
if head:
|
|
upper = soup.new_tag("thead")
|
|
tr = soup.new_tag("tr")
|
|
for h in head:
|
|
tr.append(soup.new_tag("th", string=str(h), attrs={"style": TH_STYLE_TEMPLATE}))
|
|
upper.append(tr)
|
|
table.append(upper)
|
|
body = soup.new_tag("tbody");
|
|
tr = soup.new_tag("tr")
|
|
for d in data:
|
|
tr.append(soup.new_tag("td", string=str(d), attrs={"style": TD_STYLE_TEMPLATE}))
|
|
body.append(tr)
|
|
table.append(body)
|
|
block.append(table)
|
|
|
|
return block
|
|
|
|
def create_main(soup: BeautifulSoup):
|
|
main = soup.new_tag("div", attrs={"style": MAIN_STYLE})
|
|
return main
|
|
|
|
def create_col(soup: BeautifulSoup):
|
|
col = soup.new_tag("div", attrs={"style": COLUMN_STYLE})
|
|
return col
|
|
|
|
|
|
|
|
def create_radix_table(soup: BeautifulSoup,
|
|
data = list[str],
|
|
highlighted: None | list[int] = None):
|
|
table = soup.new_tag("table")
|
|
tbody = soup.new_tag("tbody")
|
|
|
|
for word in data:
|
|
tr = soup.new_tag("tr")
|
|
for i, char in enumerate(word):
|
|
style = TD_STYLE_TEMPLATE
|
|
if i != len(word) - 1:
|
|
style = td_style_with_no_right_border(style)
|
|
if highlighted and i in highlighted:
|
|
style = td_style_with_gray(style)
|
|
td = soup.new_tag("td", string=char, attrs= {"style": style})
|
|
tr.append(td)
|
|
tbody.append(tr)
|
|
table.append(tbody)
|
|
|
|
return table
|
|
|