Генератор HTML-таблиц

Генерируйте разметку HTML-таблиц с настраиваемыми строками, столбцами и параметрами стиля.

Cell Content

HTML Output

Table Preview

Header 1 Header 2 Header 3
Cell 1,1 Cell 1,2 Cell 1,3
Cell 2,1 Cell 2,2 Cell 2,3
Cell 3,1 Cell 3,2 Cell 3,3

Часто задаваемые вопросы

Реализация кода

# Generate an HTML table from a list of dicts
def generate_html_table(headers, rows, border=True, stripe=False):
    border_attr = ' border="1" style="border-collapse:collapse"' if border else ""
    lines = [f"<table{border_attr}>", "  <thead><tr>"]
    for h in headers:
        lines.append(f"    <th>{h}</th>")
    lines.append("  </tr></thead>", "  <tbody>")
    for i, row in enumerate(rows):
        bg = ' style="background:#f2f2f2"' if stripe and i % 2 == 0 else ""
        lines.append(f"  <tr{bg}>")
        for cell in row:
            lines.append(f"    <td>{cell}</td>")
        lines.append("  </tr>")
    lines.append("  </tbody>", "</table>")
    return "\n".join(lines)

headers = ["Name", "Age", "City"]
rows = [["Alice", 30, "New York"], ["Bob", 25, "London"]]
print(generate_html_table(headers, rows, border=True, stripe=True))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.