click_extra.columns module

Describe the columns of a table, and select among them.

A ColumnSpec names a column, labels it and documents it, so one declaration feeds the rendered header, the --columns and --sort-by options and the documentation table. click_extra.table renders the columns and click_extra.show_params declares the --params table’s: a home below both is what lets each import the other’s half without a cycle.

class click_extra.columns.ColumnSpec(id, label, description='', max_width=None, optional=False, sortable=True)[source]

Bases: object

Rich description of a single column in a rendered table.

Three fields describe the column. description defaults to empty, so a quick prototype does not have to write a sentence for every column:

  • id: stable, snake_case identifier used by --columns to address the column, to key structured-format serializations, and to thread state through click_extra.context.COLUMNS.

  • label: the human-readable header shown at the top of the rendered table.

  • description: a MyST/Markdown blurb describing what the column represents. Used to auto-generate the column reference in the documentation.

The other fields tune presentation and sorting: max_width, optional and sortable.

Note

Frozen: instances are immutable, so a tuple of ColumnSpec can be shared as a module-level constant (like click_extra.show_params.ShowParamsOption.TABLE_HEADERS).

id: str

Stable, snake_case identifier addressing this column from CLI flags and code.

label: str

Human-readable header label rendered at the top of the table.

description: str = ''

MyST/Markdown description of what the column carries.

Used to auto-generate the Available columns section in the docs via the show_params_columns_table MyST substitution. Plain text without inline markup is fine: links and emphasis are optional sugar.

max_width: int | Literal['auto'] | None = None

Width limit of this column, as a character count or AUTO_WIDTH.

Cells longer than the limit wrap onto several lines, in the formats able to render that (see WRAPPABLE_FORMATS). None, the default, lets the column take whatever width its widest cell needs.

Declaring the width here rather than passing a positional list to render_table() keeps it attached to its column, so it survives a --columns projection that drops or reorders columns.

optional: bool = False

Whether the column is left out of the table until --columns asks for it.

A column carrying long free-form prose costs every other column its width once it joins the default projection, which is a poor trade for a reader who did not ask for it. Marking it optional keeps it out of the unprojected table while leaving it addressable by ID, so a consumer that wants it (a structured-format export feeding a machine, typically) selects it explicitly.

sortable: bool = True

Whether --sort-by offers this column and a sort selection matches it.

A column that only annotates its row (free-form notes, a path) still needs a place in the table layout, so a sort key knows where the sortable columns sit, without becoming a sort choice itself.

click_extra.columns.render_columns_markdown_table(columns)[source]

Render an iterable of ColumnSpec as a 2-column Markdown table.

Output shape:

| Column | Description |
| :--- | :--- |
| ``Label`` | description |
...

Suitable for inlining into MyST documents via myst_substitutions so the Available columns reference can be auto-generated from a single source of truth.

Return type:

str

click_extra.columns.select_columns(columns, selected_ids)[source]

Filter and reorder columns according to selected_ids.

Returns columns unchanged when selected_ids is falsy (no projection). Otherwise yields the matching ColumnSpec in the order selected_ids specifies, SQL-SELECT-style. Raises KeyError for an unknown ID. A selection reaching here through --columns is already checked against the registry by MultiChoice, so that guards a caller assembling selected_ids on its own.

Return type:

tuple[ColumnSpec, ...]

click_extra.columns.select_row(row, selected_ids, canonical_ids)[source]

Build a positional row by reading cells from row in the selection order.

Falls back to canonical_ids when selected_ids is empty / unset, so the row preserves its canonical column order in the absence of any user selection.

Return type:

tuple