click_extra.table moduleΒΆ

        classDiagram
  Enum <|-- TableFormat
    

Render tables for a terminal, a markup document or a serialization format.

Todo

Drop the github patch of _setup_tabulate once a python-tabulate release ships astanin/python-tabulate#410: github then marks column alignment in its separator row, as pipe does.

Todo

Delete _pad_gfm_separator once a python-tabulate release ships astanin/python-tabulate#426: the separator rows of pipe and github then carry the padding mdformat writes.

click_extra.table.THeader: TypeAliasΒΆ

One entry of a table’s headers: a plain label, or a ColumnSpec.

click_extra.table.TColumnDef: TypeAliasΒΆ

One column definition of SortByOption or column_sort_key().

A ColumnSpec, or a bare column ID.

class click_extra.table.TableFormat(*values)[source]ΒΆ

Bases: Enum

Enumeration of supported table formats.

Hard-coded to be in alphabetical order. Content of this enum is checked in unit tests.

Warning

The youtrack format is missing in action from any official JetBrains documentation. It will be removed in python-tabulate v0.11.

ALIGNED = 'aligned'ΒΆ
ASCIIDOC = 'asciidoc'ΒΆ
COLON_GRID = 'colon-grid'ΒΆ
CSV = 'csv'ΒΆ
CSV_EXCEL = 'csv-excel'ΒΆ
CSV_EXCEL_TAB = 'csv-excel-tab'ΒΆ
CSV_UNIX = 'csv-unix'ΒΆ
DOUBLE_GRID = 'double-grid'ΒΆ
DOUBLE_OUTLINE = 'double-outline'ΒΆ
FANCY_GRID = 'fancy-grid'ΒΆ
FANCY_OUTLINE = 'fancy-outline'ΒΆ
GITHUB = 'github'ΒΆ
GRID = 'grid'ΒΆ
HEAVY_GRID = 'heavy-grid'ΒΆ
HEAVY_OUTLINE = 'heavy-outline'ΒΆ
HJSON = 'hjson'ΒΆ
HTML = 'html'ΒΆ
JIRA = 'jira'ΒΆ
JSON = 'json'ΒΆ
JSON5 = 'json5'ΒΆ
JSONC = 'jsonc'ΒΆ
LATEX = 'latex'ΒΆ
LATEX_BOOKTABS = 'latex-booktabs'ΒΆ
LATEX_LONGTABLE = 'latex-longtable'ΒΆ
LATEX_RAW = 'latex-raw'ΒΆ
MEDIAWIKI = 'mediawiki'ΒΆ
MIXED_GRID = 'mixed-grid'ΒΆ
MIXED_OUTLINE = 'mixed-outline'ΒΆ
MOINMOIN = 'moinmoin'ΒΆ
ORGTBL = 'orgtbl'ΒΆ
OUTLINE = 'outline'ΒΆ
PIPE = 'pipe'ΒΆ
PLAIN = 'plain'ΒΆ
PRESTO = 'presto'ΒΆ
PRETTY = 'pretty'ΒΆ
PSQL = 'psql'ΒΆ
ROUNDED_GRID = 'rounded-grid'ΒΆ
ROUNDED_OUTLINE = 'rounded-outline'ΒΆ
RST = 'rst'ΒΆ
SIMPLE = 'simple'ΒΆ
SIMPLE_GRID = 'simple-grid'ΒΆ
SIMPLE_OUTLINE = 'simple-outline'ΒΆ
TEXTILE = 'textile'ΒΆ
TOML = 'toml'ΒΆ
TSV = 'tsv'ΒΆ
UNSAFEHTML = 'unsafehtml'ΒΆ
VERTICAL = 'vertical'ΒΆ
XML = 'xml'ΒΆ
YAML = 'yaml'ΒΆ
YOUTRACK = 'youtrack'ΒΆ
property is_markup: boolΒΆ

Whether this format is a markup rendering.

ANSI codes never reach a markup rendering raw: they are either translated to the format’s native styling (see supports_styling) or stripped from cell values. Forcing --color on the command line preserves them as-is in the markup formats without styling support.

property supports_styling: boolΒΆ

Whether ANSI codes are translated to this format’s native styling.

See STYLED_FORMATS for the registry.

property is_wrappable: boolΒΆ

Whether this format renders a cell wrapped onto several lines.

See WRAPPABLE_FORMATS for the registry, and the rationale behind each excluded format.

click_extra.table.MARKUP_FORMATS = frozenset({TableFormat.ASCIIDOC, TableFormat.CSV, TableFormat.CSV_EXCEL, TableFormat.CSV_EXCEL_TAB, TableFormat.CSV_UNIX, TableFormat.GITHUB, TableFormat.HJSON, TableFormat.HTML, TableFormat.JIRA, TableFormat.JSON, TableFormat.JSON5, TableFormat.JSONC, TableFormat.LATEX, TableFormat.LATEX_BOOKTABS, TableFormat.LATEX_LONGTABLE, TableFormat.LATEX_RAW, TableFormat.MEDIAWIKI, TableFormat.MOINMOIN, TableFormat.ORGTBL, TableFormat.PIPE, TableFormat.RST, TableFormat.TEXTILE, TableFormat.TOML, TableFormat.TSV, TableFormat.UNSAFEHTML, TableFormat.XML, TableFormat.YAML, TableFormat.YOUTRACK})ΒΆ

Subset of table formats that are considered as markup rendering.

click_extra.table.STYLED_FORMATS: dict[TableFormat, Callable[[str], str]] = {TableFormat.HTML: <function ansi_to_html>, TableFormat.JIRA: <function ansi_to_jira>, TableFormat.LATEX: <function ansi_to_latex>, TableFormat.LATEX_BOOKTABS: <function ansi_to_latex>, TableFormat.LATEX_LONGTABLE: <function ansi_to_latex>, TableFormat.LATEX_RAW: <function ansi_to_latex>, TableFormat.MEDIAWIKI: <function ansi_to_html>, TableFormat.TEXTILE: <function ansi_to_textile>, TableFormat.UNSAFEHTML: <function ansi_to_html>}ΒΆ

Markup formats able to express styles natively, mapped to their ANSI translator.

print_table() runs the rendered output of these formats through their translator, converting the ANSI codes carried by cells and headers into the format’s own styling markup: inline-CSS HTML <span>s for the HTML pair and MediaWiki (which accepts embedded HTML), Textile %{...} spans, Jira {color:...} macros, and xcolor-based LaTeX macros.

Note

Translation happens on the rendered output, not on cell values, on purpose. tabulate escapes cell content for some formats (html escapes HTML entities, non-raw latex variants escape TeX specials) while ANSI sequences pass through unscathed, so pre-render translation would get its markup mangled by those escaping rules. Post-render injection also keeps column-width computation on the ANSI text, which tabulate measures correctly.

Every markup format absent from this registry strips ANSI codes from its cells before rendering. The Colors and styles section of Table gives the reason, format by format.

click_extra.table.DEFAULT_FORMAT = TableFormat.ROUNDED_OUTLINEΒΆ

Default table format, if none is specified.

click_extra.table.RECORD_KEY = 'record'ΒΆ

Key used for each record in structured formats that require named containers (TOML [[record]], XML <record>).

click_extra.table.XML_ROOT_KEY = 'records'ΒΆ

Root element name for XML table output.

click_extra.table.CSV_FORMATS = frozenset({TableFormat.CSV, TableFormat.CSV_EXCEL, TableFormat.CSV_EXCEL_TAB, TableFormat.CSV_UNIX})ΒΆ

Formats rendered by Python’s csv module, each named after its dialect.

click_extra.table.SERIALIZATION_FORMATS = frozenset({TableFormat.HJSON, TableFormat.JSON, TableFormat.JSON5, TableFormat.JSONC, TableFormat.TOML, TableFormat.XML, TableFormat.YAML})ΒΆ

Structured serialization formats whose renderers escape raw ESC bytes, making post-render strip_ansi() ineffective.

click_extra.table.AUTO_WIDTH: Final = 'auto'ΒΆ

Sentinel asking for a column width derived from the space left on the terminal.

Annotated Final so it narrows to Literal["auto"] instead of str, which is what lets it stand in for the raw string anywhere a ColumnWidth is expected.

click_extra.table.MIN_COLUMN_WIDTH = 8ΒΆ

Floor applied to an AUTO_WIDTH column, so a crowded table still renders a usable column instead of collapsing it to nothing.

click_extra.table.WRAPPABLE_FORMATS = frozenset({TableFormat.COLON_GRID, TableFormat.DOUBLE_GRID, TableFormat.DOUBLE_OUTLINE, TableFormat.FANCY_GRID, TableFormat.FANCY_OUTLINE, TableFormat.GRID, TableFormat.HEAVY_GRID, TableFormat.HEAVY_OUTLINE, TableFormat.MIXED_GRID, TableFormat.MIXED_OUTLINE, TableFormat.OUTLINE, TableFormat.PLAIN, TableFormat.PRESTO, TableFormat.PRETTY, TableFormat.PSQL, TableFormat.ROUNDED_GRID, TableFormat.ROUNDED_OUTLINE, TableFormat.RST, TableFormat.SIMPLE, TableFormat.SIMPLE_GRID, TableFormat.SIMPLE_OUTLINE, TableFormat.VERTICAL})ΒΆ

Formats laying a wrapped cell over several lines while keeping it one cell.

Column widths are a presentation hint: they are honored by the formats listed here and silently dropped by every other one, the same way styling degrades per format in STYLED_FORMATS. Dropping is the point, as a width forced onto a format below would corrupt its output.

Important

Every format absent from this registry renders max_column_widths unusable, for one of four reasons:

  • aligned: this module’s own zero-padding format. tabulate does not indent its continuation lines, so a wrapped cell escapes its column.

  • github, jira, orgtbl, pipe: continuation lines are emitted as additional table rows. They look right in a terminal, but a Markdown, Jira or Org renderer reads them as extra records.

  • asciidoc, html, latex, latex-booktabs, latex-longtable, latex-raw, mediawiki, moinmoin, textile, unsafehtml, youtrack: the line break lands raw inside the cell markup, where the target renderer either collapses it back to a space or breaks the row. These formats delegate wrapping to whatever displays them.

  • CSV_FORMATS, tsv, and the SERIALIZATION_FORMATS: data interchange, where a line break inside a field changes the record rather than its presentation.

click_extra.table.EMOJI_PRESENTATION_SELECTOR = '️'¢

Unicode VARIATION SELECTOR-16, asking for the emoji form of what precedes it.

click_extra.table.EMOJI_PRESENTATION_RE = re.compile('.️')¢

Matches one emoji-presentation sequence: a character and the selector.

click_extra.table.CORNER_GLYPH = '↴'ΒΆ

The arrow corner_header() places after the row axis.

The Sphinx extension looks for it to find a table with a corner label: see click_extra.sphinx.tables.

click_extra.table.corner_header(rows, columns)[source]ΒΆ

Name both axes of a two-axis table in its top-left header cell.

rows says what the first column lists, read downward, and columns what the other headers list, read rightward: corner_header("City", "Month") returns City ↴ \ Month β†’.

Return type:

str

click_extra.table.render_table(table_data, headers=None, table_format=None, sort_key=None, max_column_widths=None, **kwargs)[source]ΒΆ

Render a table and return it as a string.

headers entries carrying a column ID (ColumnSpec instances) plug the table into the active --sort-by selection: when no explicit sort_key is given, rows sort by the selected columns this table carries, and keep their original order when it carries none. See column_sort_key() for the exact semantics.

Parameters:
  • sort_key (Callable[[Sequence[str | None]], Any] | None) – Optional callable passed to sorted() as the key argument. When provided, rows are sorted before rendering.

  • max_column_widths (Sequence[int | Literal['auto'] | None] | int | Literal['auto'] | None) – Width limits, as one entry per column or a single value for all of them. Each entry is a character count, "auto" to absorb the width left on the terminal, or None for no limit. Defaults to the max_width declared by ColumnSpec headers. Silently dropped by formats outside WRAPPABLE_FORMATS.

Return type:

str

click_extra.table.print_table(table_data, headers=None, table_format=None, sort_key=None, max_column_widths=None, **kwargs)[source]ΒΆ

Render a table and print it to the console.

headers entries carrying a column ID (ColumnSpec instances) plug the table into the active --sort-by selection: when no explicit sort_key is given, rows sort by the selected columns this table carries, and keep their original order when it carries none. See column_sort_key() for the exact semantics.

ANSI codes carried by cell values and headers depend on the format:

  • Markup formats with native styling support (see STYLED_FORMATS) get them translated to the format’s own styling markup, unless color output is disabled (--no-color, NO_COLOR, …).

  • Other markup formats get them stripped from cell values before rendering, unless --color is explicitly forced on the command line.

  • Plain-text formats keep them raw, and defer to echo()’s sensitivity to the global colorization settings.

Parameters:
  • sort_key (Callable[[Sequence[str | None]], Any] | None) – Optional callable passed to sorted() as the key argument. When provided, rows are sorted before rendering.

  • max_column_widths (Sequence[int | Literal['auto'] | None] | int | Literal['auto'] | None) – Width limits, as one entry per column or a single value for all of them. Each entry is a character count, "auto" to absorb the width left on the terminal, or None for no limit. Defaults to the max_width declared by ColumnSpec headers. Silently dropped by formats outside WRAPPABLE_FORMATS.

Raises:

MissingExtraError – the renderer of table_format is not installed.

Return type:

None

click_extra.table.serialize_data(data, table_format, *, default=None, root_element='records', **kwargs)[source]ΒΆ

Serialize arbitrary Python data to a structured format.

Unlike render_table() which expects tabular rows and headers, this function accepts any JSON-compatible data structure (dicts, lists, nested combinations) and serializes it to the requested format.

Only formats in SERIALIZATION_FORMATS are supported.

Parameters:
  • data (Any) – Arbitrary data to serialize (dicts, lists, scalars).

  • table_format (TableFormat) – Target serialization format.

  • default (Callable | None) – Fallback serializer for types not natively supported. Defaults to str, so Path and similar types are stringified automatically. Set to a custom callable for different behavior.

  • root_element (str) – Root element name for XML output.

  • kwargs – Extra keyword arguments forwarded to the underlying serializer (like sort_keys or indent for JSON).

Raises:

ValueError – If the format is not a serialization format.

Return type:

str

click_extra.table.print_data(data, table_format, *, default=None, root_element='records', package='click-extra', **kwargs)[source]ΒΆ

Serialize arbitrary Python data and print it to the console.

Wraps serialize_data() with user-friendly error handling for missing optional dependencies.

Parameters:
  • data (Any) – Arbitrary data to serialize.

  • table_format (TableFormat) – Target serialization format.

  • default (Callable | None) – Fallback serializer for custom types. Defaults to str.

  • root_element (str) – Root element name for XML output.

  • package (str) – Package name for install instructions in error messages.

  • kwargs – Extra keyword arguments forwarded to the underlying serializer.

Raises:

MissingExtraError – the serializer of table_format is not installed.

Return type:

None