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:
objectRich description of a single column in a rendered table.
Three fields describe the column.
descriptiondefaults to empty, so a quick prototype does not have to write a sentence for every column:id: stable, snake_case identifier used by--columnsto address the column, to key structured-format serializations, and to thread state throughclick_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,optionalandsortable.Note
Frozen: instances are immutable, so a tuple of
ColumnSpeccan be shared as a module-level constant (likeclick_extra.show_params.ShowParamsOption.TABLE_HEADERS).- 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_tableMyST 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--columnsprojection that drops or reorders columns.
- optional: bool = False¶
Whether the column is left out of the table until
--columnsasks 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.
- click_extra.columns.render_columns_markdown_table(columns)[source]¶
Render an iterable of
ColumnSpecas a 2-column Markdown table.Output shape:
| Column | Description | | :--- | :--- | | ``Label`` | description | ...
Suitable for inlining into MyST documents via
myst_substitutionsso the Available columns reference can be auto-generated from a single source of truth.- Return type:
- click_extra.columns.select_columns(columns, selected_ids)[source]¶
Filter and reorder
columnsaccording toselected_ids.Returns
columnsunchanged whenselected_idsis falsy (no projection). Otherwise yields the matchingColumnSpecin the orderselected_idsspecifies, SQL-SELECT-style. RaisesKeyErrorfor an unknown ID. A selection reaching here through--columnsis already checked against the registry byMultiChoice, so that guards a caller assemblingselected_idson its own.- Return type:
- click_extra.columns.select_row(row, selected_ids, canonical_ids)[source]¶
Build a positional row by reading cells from
rowin the selection order.Falls back to
canonical_idswhenselected_idsis empty / unset, so the row preserves its canonical column order in the absence of any user selection.- Return type: