DuckDB to HTML

Directly convert DuckDB result set to HTML table string

Useful to write HTML tables without pandas

Databases
HTML
Author

LiqC

Published

Sat, Sep 16 2023 00:00

Write HTML tables from DuckDB directly

Feature request: https://github.com/duckdb/duckdb/discussions/8961

Monkey Patching DuckDBPyConnection

from duckdb import DuckDBPyConnection

def to_html(
    self,
    classes: str | None = None,
    table_id: str | None = None,
    indent: int = 2,
):
    _indent = " "*indent
    if classes:
        _classes = f' class="{classes}"'
    else:
        _classes = ""

    if table_id:
        _table_id = f' id="{table_id}"'
    else:
        _table_id = ""

    html_table = f"<table{_classes}{_table_id}>\n"
    
    html_table += f"{_indent}<thead>\n"
    html_table += f"{_indent}<tr>\n"
    for col_name in self.description:
        html_table += f"{_indent}{_indent}<th>{col_name[0]}</th>\n"
    html_table += f"{_indent}</tr>\n"
    html_table += f"{_indent}</thead>\n"
    
    for row in self.fetchall():
        html_table += f"{_indent}<tr>\n"
        for value in row:
            html_table += f"{_indent}{_indent}<td>{str(value)}</td>\n"
        html_table += f"{_indent}</tr>\n"
    
    html_table += "</table>"
    return html_table

setattr(DuckDBPyConnection, "to_html", to_html)

import duckdb
con = duckdb.connect()
con.execute("CREATE TABLE items(item VARCHAR, value DECIMAL(10, 2), count INTEGER)")
con.execute("INSERT INTO items VALUES ('jeans', 20.0, 1), ('hammer', 42.2, 2)")

con.execute("SELECT * FROM items")
html = con.to_html(classes="table table-hover", table_id="ducktable")
print(html[:104])
<table class="table table-hover" id="ducktable">
  <thead>
  <tr>
    <th>item</th>
    <th>value</th>
 
from IPython.display import HTML
display(HTML(html))
item value count
jeans 20.00 1
hammer 42.20 2