Great Tables Integration
Gridwell integrates with Great Tables to render GT objects to all 11 output formats from Python.
Installation
The Great Tables integration needs the optional gt extra (Great Tables 1.1.0 or newer, and pandas):
pip install 'gridwell[gt]'Without it, import gridwell still works. Calling gridwell.gt_to_ir then raises an ImportError that explains how to install the extra.
Quick Start
from great_tables import GT, exibble
import gridwell
gt = GT(exibble.head(5), rowname_col="row", groupname_col="group")
table = gridwell.Table(gt) # converts the GT to gridwell's IR
html = table.render_html()gridwell.Table() (or gridwell.Table.from_gt(), which takes only a GT) converts the table to IR. In a notebook (Jupyter, VS Code, Quarto) a Table shows as HTML, with styles scoped to that one table and marked so Quarto doesn’t add Bootstrap’s table styling; print(table) draws it as text, and repr(table) describes it:
>>> table
<gridwell.Table: 8 columns, 5 rows in 2 groups>Functions
gridwell.gt_to_ir(gt, emitter="auto")
Converts a Great Tables GT object to gridwell IR JSON string.
ir_json = gridwell.gt_to_ir(gt) # returns str (JSON)gridwell.gt_to_dict(gt, emitter="auto")
Converts a GT object to a Python dict (IR structure).
ir_dict = gridwell.gt_to_dict(gt) # returns dictWhich converter
The converter is moving into Great Tables, as GT.as_gridwell(), since it reads Great Tables’ internals and Great Tables can keep it in step with them. Until every Great Tables has it, gridwell keeps its own copy. emitter picks one:
"auto"(the default): Great Tables’ when the installed Great Tables hasGT.as_gridwell(), and gridwell’s otherwise. If Great Tables’ raises, or gives an IR major version this gridwell can’t read, gridwell’s is used with aUserWarningsaying why."upstream": Great Tables’, raising if there is none or it can’t be used."gridwell": gridwell’s.
Great Tables’ own needs no pandas; gridwell’s needs pandas, from the gt extra. Either converts a table on polars as it is. No released Great Tables has GT.as_gridwell() yet, so today "auto" always uses gridwell’s.
What Gets Converted
| GT Feature | IR Mapping |
|---|---|
| Column labels | thead rows |
Spanners (tab_spanner(), any number of levels) |
spanner_label rows above the column labels, drawn as Great Tables draws them |
| Row groups | tbody array with group labels |
| Stub column | Cells with is_stub: true and scope: "row" (row headers, as in Great Tables’ HTML), always aligned left |
Stub indents (tab_stub_indent()) |
The stub cell’s indent: its level times stub_indent_length (15px at level 3), as Great Tables indents it, under any tab_style() on the cell. In a stub of several columns, the innermost column is indented |
A stub of several columns (rowname_col=["region", "city"]) |
One stub column each, outermost first. Where an outer column’s value is the same in consecutive rows of a group (and so are the columns to its left), the rows share one cell spanning them (rowspan, scope: "rowgroup"), merged in the order the rows are shown |
Row group column (tab_options(row_group_as_column=True)) |
The first stub column: each group’s label is a stub cell spanning its rows (scope: "rowgroup"), and the group has no label row |
Stubhead label (tab_stubhead()) |
One head cell spanning the stub columns (blank without one); a list of labels (tab_stubhead(["A", "B"])) gives each row stub column its own, with a blank one above a row group column |
Summary rows (summary_rows(), grand_summary_rows()) |
A group’s summary_rows, and a last group without a label for the grand summary; a table without a stub gets an empty one for their labels, as in Great Tables. A group whose summary rows are all on its top side (side="top") has them at the top (summary_placement: "top"); one with rows on both sides has them all at the bottom, after its rows. Grand summary rows on the top side are a first group without a label, with its rows at the top. With a row group column, a summary row’s label spans the other stub columns, beside an empty cell under the group’s label (Great Tables’ HTML has the group’s cell span its summary rows instead) |
| Title/subtitle | header block |
| Source notes | footer.source_notes |
Footnotes (tab_footnote()) |
footer.footnotes, the ones Great Tables lists, in its order and with its marks; each mark is also on the cell or label it annotates, where Great Tables puts it (with placement="auto", before a number and after anything else) |
Styles (tab_style()) |
Each styled cell’s or label’s own style (style_id), as the CSS Great Tables writes for it, later declarations winning: text, fill, borders and the style.css() declarations the IR has. A font size without a unit (style.text(size=12)) is in pixels; properties the IR has no equivalent for (font-stretch, letter-spacing) are dropped |
Table options (tab_options(), opt_stylize(), opt_row_striping()) |
The options the IR can express, where changed from Great Tables’ defaults: the fills (with the text color Great Tables gives them) and font weights of the column labels, row group labels and stub, the summary rows’ fills, no lines between body rows, striping (a conditional style on even rows, of the stub, the body or both), and the table’s borders (below). Other options (fonts, sizes, padding, vertical lines between body cells) aren’t converted yet |
md() and html() labels |
The content of the HTML Great Tables makes of them: text with bold, italic, underline, strike-through and code runs, line breaks, superscripts and subscripts in Unicode characters where every character has one (m²), images (<img>, their src copied, never read) and SVG as raw HTML (other markup is dropped and its text kept) |
| Column alignment | column_spec[].align |
| Column widths | column_spec[].width |
Table width (tab_options(table_width=)) |
config.table_width, for a length (600px, 80%, 30em); the default "auto" and widths the IR has no equivalent for (calc(...)) are left to gridwell |
| Formatted values | Cell content: the content of the HTML Great Tables puts in the cell, read as labels are, so fmt_*() output that is HTML (fmt_markdown(), fmt_units(), fmt_scientific(), fmt_image(), nanoplots) is drawn as formatted, and other values are their text. |
Merged columns (cols_merge()) and text_transform() |
Cells as Great Tables shows them: merged (the columns merged in are hidden) and transformed |
| Typed values | Cell typed_value (below) |
A cell’s typed value is what a spreadsheet holds, so XLSX keeps numbers, booleans, dates and times as values, each shown as the table shows it:
- A number is the number displayed (
0.11for0.1111formatted to two decimals), as the R package does for gt tables: in any locale’s marks (1234.5for1.234,50and1 234,50), with its currency or unit left out (49.95for$49.95,1234.5for1,234.50 kg), a sign or accounting parentheses (-5for($5.00)), as a fraction for a percentage or per mille (0.451for45.1%), and scaled for scientific and compact notation (0.000123for1.23 × 10⁻⁴,1230000for1.23M). A display with one mark (1,234) is read as the data’s value rounds to it (a thousand and more, or a little more than one); one read only one way is read so even when the value doesn’t round to it (a value scaled withscale_by=). A display that isn’t one number (<0.001,46 (37, 60),4/9) has none, so the cell stays text. - Booleans, and
datetime.date,datetime.datetime(or pandasTimestamp) anddatetime.timevalues are typed; a date or time is written without its time zone, as the wall-clock time the table shows. Dates held as strings stay strings. - A column’s type decides where it has one (a polars
DateorInt64, a pandasdatetime64); a column of mixed objects is typed by each value. A duration (timedelta) stays text.
Language and direction
A table given a locale (GT(df, locale="ar_EG")) is in that language: the IR’s config.locale and config.lang are the locale as a language tag (ar-EG). When the language is written right to left (Arabic, Hebrew, Persian, Urdu and the like, or any language in a right-to-left script such as pa_Arab), config.dir is rtl, so the columns and text run right to left. Great Tables’ own HTML stays left to right. A table without a locale gets neither lang nor dir, as before.
The R package does the same for gt’s locale (gt(df, locale = "ar")).
Borders
The border options (table_border_*, heading_border_bottom_*, column_labels_border_*, row_group_border_*, table_body_border_*, table_body_hlines_*, stub_border_*, summary_row_border_* and grand_summary_row_border_*) become the table’s rules, where changed from Great Tables’ defaults. Each rule takes the style, width and color of the options that draw it in Great Tables’ HTML. Where two options draw one rule, the outer one’s changes win: with a heading, table_border_top_* is the rule above it (title) and heading_border_bottom_* and column_labels_border_top_* the one under it (top); without one, table_border_top_* is top. The same goes for table_border_bottom_*, which is the rule under the footer (notes), or the table’s bottom when there is no footer. Styles the IR has no rule for (groove, ridge, inset, outset) are left out.
HTML ids
The HTML writer gives the header cells the ids Great Tables gives them, and each body cell the same headers (except that stub cells are numbered in the order the rows are shown, where Great Tables numbers them by data row, which differs when row groups reorder the rows). Great Tables’ GT(id=) is the table’s config.id, which the HTML writer uses as its table_id, so the ids are prefixed as Great Tables prefixes them:
gt = GT(exibble, rowname_col="row", groupname_col="group", id="sales")
html = gridwell.Table(gt).render_html() # ids start "sales-"The writer’s table_id option, when set, is used instead. An id that isn’t an HTML id (empty, or with whitespace) is left out.
Full Pipeline Example
from great_tables import GT, exibble
import gridwell
gt = (
GT(exibble, rowname_col="row", groupname_col="group")
.tab_header(title="Example Table", subtitle="From exibble")
.tab_source_note(source_note="Source: great_tables package")
)
# Convert and validate
table = gridwell.Table(gt)
errors = table.validate()
assert errors == []
# Render all text formats
for fmt in ["html", "latex", "typst", "rtf", "svg", "ansi", "pandoc", "quarto"]:
output = table.render(fmt)
print(f"{fmt}: {len(output)} chars")
# Render binary formats
for fmt in ["docx", "xlsx", "pptx"]:
data = table.render_binary(fmt)
print(f"{fmt}: {len(data)} bytes")