Load a CSV into a table
Render a .csv file as a table without typing a row — the header
line becomes the columns. A focused excerpt of A report with live data.
Step 1 — Point a source at the file
Any program that prints CSV works; cat is the simplest. Wire it in wit.sources.json.
{
"sources": {
"downloads": { "run": ["cat", "data/downloads.csv"], "format": "csv" }
}
}Step 2 — Load and render it
@load downloads load@ @table(rows: @downloads)
@load downloads load@ binds the alias's data to @downloads. Bind it under another name with @load(as: dl) downloads load@.
Step 3 — Build
wit build report.wit --sources wit.sources.json --allow-exec --fragmentResult. For a file whose header is platform,downloads,change:
<table><thead><tr><th>platform</th><th>downloads</th><th>change</th></tr></thead> <tbody><tr><td>macOS</td><td>1200</td><td>+4%</td></tr>…</tbody></table>
Pick and label the columns
schema maps each field to its header text, in the order you
want the columns. Fields left out are not shown.
source
@table(schema: { platform: Platform, downloads: Downloads }, rows: @downloads)
result
When you don't have a file
Inline rows
The first row is the header.
source
@table(caption: Corpus splits, rows: [ [Group, A, B] [Teens, 8, 6.5] ])
result
| Group | A | B |
|---|---|---|
| Teens | 8 | 6.5 |
A collection of records
Define the data once, then label its columns with schema.
source
#corpus: [ { split: Train, docs: 40k, tokens: 12.4M } { split: Dev, docs: 5k, tokens: 1.5M } ] !! @table(schema: { split: Split, docs: Documents, tokens: Tokens }, rows: @corpus)
result
| Split | Documents | Tokens |
|---|---|---|
| Train | 40k | 12.4M |
| Dev | 5k | 1.5M |
This renders a header of Split, Documents, Tokens and one row per record.
Pitfalls
- Don't freeze the table. A raw
@@tableis verbatim text; write a plain@table(…)sorows: @downloadsis looked up. - Commas in inline rows. A comma always separates cells in an
inline row, even inside quotes. Put such a value in a CSV file, or build the
table from child
@row/@colnodes. - No --allow-exec, no data. Without the flag the program does
not run and the build fails at the
@loadline.
See also
Tables covers every table form; A report with live data shows the full data flow.