← All ivy-nodes
IVYXSTUDIO · IVY NODE
M
Markdown Table
ivy.node.markdown-table · v0.1.0
ivyx✓
Formats rows as a Markdown table, with the columns in the order given or as they first appear. Numbers are right-aligned and a pipe in a value is escaped.
#markdown#report#table#format
Inputs
| Field | Type | Description |
|---|---|---|
| rowsrequired | array | The rows to format. |
| columns | array | The columns, in order. Every column, in first-seen order, when omitted. |
Outputs
| Field | Type | Description |
|---|---|---|
| markdownrequired | string | The Markdown table. |
| row_countrequired | integer | How many rows the table has. |
Source
python
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
rows = inp["rows"]
columns = inp.get("columns")
if not columns:
columns = []
for row in rows:
for key in row:
if key not in columns:
columns.append(key)
def cell(value):
if value is None:
return ""
if isinstance(value, float):
value = f"{value:,.2f}"
return str(value).replace("|", "\\|").replace("\n", " ")
numeric = {c: all(isinstance(r.get(c), (int, float)) and not isinstance(r.get(c), bool) for r in rows if r.get(c) is not None) and any(r.get(c) is not None for r in rows) for c in columns}
header = "| " + " | ".join(columns) + " |"
rule = "| " + " | ".join("---:" if numeric[c] else "---" for c in columns) + " |"
body = ["| " + " | ".join(cell(r.get(c)) for c in columns) + " |" for r in rows]
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["markdown"] = "\n".join([header, rule] + body) if columns else ""
out["row_count"] = len(rows)Tests
Requires: python:3.9
- table
Rows become a table with the number column right-aligned.
- escapes
A pipe inside a value does not break the table.
- empty
No rows and no columns is an empty string.