Tableยถ

Click Extra provides a way to render tables in the terminal.

Here how to use the standalone table rendering option decorator:

from click_extra import command, pass_context, table_format_option, style, Color

@command
@table_format_option
@pass_context
def table_command(ctx):
    headers = ("Day", "Temperature")
    data = (
        (1, 42.9),
        (2, None),
        (style("Friday", fg=Color.blue), style("Hot ๐Ÿฅต", fg=Color.red, bold=True)),
    )
    ctx.print_table(data, headers)

As you can see above, this option adds a ready-to-use print_table() method to the context object.

The default help message for this option list all available table formats:

$ table --help
Usage: table [OPTIONS]

Options:
  --table-format [asciidoc|csv|csv-excel|csv-excel-tab|csv-unix|double-grid|double-outline|fancy-grid|fancy-outline|github|grid|heavy-grid|heavy-outline|html|jira|latex|latex-booktabs|latex-longtable|latex-raw|mediawiki|mixed-grid|mixed-outline|moinmoin|orgtbl|outline|pipe|plain|presto|pretty|psql|rounded-grid|rounded-outline|rst|simple|simple-grid|simple-outline|textile|tsv|unsafehtml|vertical|youtrack]
          Rendering style of tables.
  --help  Show this message and exit.

So you can use the --table-format option to change the table format:

$ table --table-format fancy-outline
โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ Day    โ”‚ Temperature โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”‚ 2      โ”‚             โ”‚
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•›
$ table --table-format asciidoc
[cols="8<,13<",options="header"]
|====
| Day    | Temperature 
| 1      | 42.9        
| 2      |             
| Friday | Hot ๐Ÿฅต      
|====

Tip

This example has been selected so you can see how print_table() handles:

  • Mixed data types (integers, floats, None, strings)

  • ANSI color codes (added with the click_extra.style() function)

  • Unicode characters (like the emojis)

Table formatsยถ

Table formats are aggregated from 3 sources:

Theyโ€™re divided in 2 categories:

  • Formats that produce plain text output (like ASCII tables, grid tables, etc.) and are often composed of Unicode box-drawing characters, to be displayed in a terminal.

  • Formats that produce markup language output (like HTML, Markdown, LaTeX, etc.) and are expected to be rendered by a supporting viewer. This category also includes CSV and TSV formats, which are plain text but meant to be processed by other tools.

Format ID

Description

Implementation

Markup

asciidoc

AsciiDoc table

python-tabulate

โœ…

csv

Comma-separated values

csv

โœ…

csv-excel

CSV with Excel dialect

csv

โœ…

csv-excel-tab

CSV with Excel tab dialect

csv

โœ…

csv-unix

CSV with Unix dialect

csv

โœ…

double-grid

Double-line grid table

python-tabulate

โŒ

double-outline

Double-line outline table

python-tabulate

โŒ

fancy-grid

Grid with Unicode box-drawing characters

python-tabulate

โŒ

fancy-outline

Outline with Unicode box-drawing characters

python-tabulate

โŒ

github

GitHub-flavored Markdown table

python-tabulate

โœ…

grid

Grid table with ASCII characters, also supported by Pandoc and reStructuredText

python-tabulate

โŒ

heavy-grid

Heavy-line grid table

python-tabulate

โŒ

heavy-outline

Heavy-line outline table

python-tabulate

โŒ

html

HTML table

python-tabulate

โœ…

jira

Jira-style markup

python-tabulate

โœ…

latex

LaTeX table

python-tabulate

โœ…

latex-booktabs

LaTeX table with booktabs package

python-tabulate

โœ…

latex-longtable

LaTeX longtable environment

python-tabulate

โœ…

latex-raw

LaTeX table without escaping

python-tabulate

โœ…

mediawiki

MediaWiki markup

python-tabulate

โœ…

mixed-grid

Mixed-line grid table

python-tabulate

โŒ

mixed-outline

Mixed-line outline table

python-tabulate

โŒ

moinmoin

MoinMoin wiki markup

python-tabulate

โœ…

orgtbl

Emacs org-mode table

python-tabulate

โœ…

outline

Simple outline table

python-tabulate

โŒ

pipe

PHP Markdown Extra pipes, also supported by Pandoc

python-tabulate

โœ…

plain

Plain text, no formatting

python-tabulate

โŒ

presto

Presto SQL output style

python-tabulate

โŒ

pretty

Pretty ASCII table

python-tabulate

โŒ

psql

PostgreSQL output style

python-tabulate

โŒ

rounded-grid

Rounded grid table

python-tabulate

โŒ

rounded-outline

Rounded outline table

python-tabulate

โŒ

rst

reStructuredText simple table

python-tabulate

โœ…

simple

Simple table with spaces, also supported by Pandoc

python-tabulate

โŒ

simple-grid

Simple grid table

python-tabulate

โŒ

simple-outline

Simple outline table

python-tabulate

โŒ

textile

Textile markup

python-tabulate

โœ…

tsv

Tab-separated values

python-tabulate

โœ…

unsafehtml

HTML table without escaping

python-tabulate

โœ…

vertical

Vertical table layout

cli-helpers

โŒ

youtrack

YouTrack markup

python-tabulate

โœ…

Todo

Explain extra parameters supported by print_table() for each category of formats.

$ table --table-format asciidoc
[cols="8<,13<",options="header"]
|====
| Day    | Temperature 
| 1      | 42.9        
| 2      |             
| Friday | Hot ๐Ÿฅต      
|====
$ table --table-format csv
Day,Temperature
1,42.9
2,
Friday,Hot ๐Ÿฅต
$ table --table-format csv-excel
Day,Temperature
1,42.9
2,
Friday,Hot ๐Ÿฅต
$ table --table-format csv-excel-tab
Day	Temperature
1	42.9
2	
Friday	Hot ๐Ÿฅต
$ table --table-format csv-unix
"Day","Temperature"
"1","42.9"
"2",""
"Friday","Hot ๐Ÿฅต"
$ table --table-format double-grid
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ Day    โ•‘ Temperature โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘ 1      โ•‘ 42.9        โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘ 2      โ•‘             โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘ Friday โ•‘ Hot ๐Ÿฅต      โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
$ table --table-format double-outline
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ Day    โ•‘ Temperature โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘ 1      โ•‘ 42.9        โ•‘
โ•‘ 2      โ•‘             โ•‘
โ•‘ Friday โ•‘ Hot ๐Ÿฅต      โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
$ table --table-format fancy-grid
โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ Day    โ”‚ Temperature โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2      โ”‚             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•›
$ table --table-format fancy-outline
โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ Day    โ”‚ Temperature โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”‚ 2      โ”‚             โ”‚
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•›
$ table --table-format github
| Day    | Temperature |
| ------ | ----------- |
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
$ table --table-format grid
+--------+-------------+
| Day    | Temperature |
+========+=============+
| 1      | 42.9        |
+--------+-------------+
| 2      |             |
+--------+-------------+
| Friday | Hot ๐Ÿฅต      |
+--------+-------------+
$ table --table-format heavy-grid
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Day    โ”ƒ Temperature โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ 1      โ”ƒ 42.9        โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ 2      โ”ƒ             โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ Friday โ”ƒ Hot ๐Ÿฅต      โ”ƒ
โ”—โ”โ”โ”โ”โ”โ”โ”โ”โ”ปโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”›
$ table --table-format heavy-outline
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Day    โ”ƒ Temperature โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ 1      โ”ƒ 42.9        โ”ƒ
โ”ƒ 2      โ”ƒ             โ”ƒ
โ”ƒ Friday โ”ƒ Hot ๐Ÿฅต      โ”ƒ
โ”—โ”โ”โ”โ”โ”โ”โ”โ”โ”ปโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”›
$ table --table-format html
<table>
<thead>
<tr><th>Day   </th><th>Temperature</th></tr>
</thead>
<tbody>
<tr><td>1     </td><td>42.9       </td></tr>
<tr><td>2     </td><td>           </td></tr>
<tr><td>Friday</td><td>Hot ๐Ÿฅต     </td></tr>
</tbody>
</table>
$ table --table-format jira
|| Day    || Temperature ||
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
$ table --table-format latex
\begin{tabular}{ll}
\hline
 Day    & Temperature \\
\hline
 1      & 42.9        \\
 2      &             \\
 Friday & Hot ๐Ÿฅต      \\
\hline
\end{tabular}
$ table --table-format latex-booktabs
\begin{tabular}{ll}
\toprule
 Day    & Temperature \\
\midrule
 1      & 42.9        \\
 2      &             \\
 Friday & Hot ๐Ÿฅต      \\
\bottomrule
\end{tabular}
$ table --table-format latex-longtable
\begin{longtable}{ll}
\hline
 Day    & Temperature \\
\hline
\endhead
 1      & 42.9        \\
 2      &             \\
 Friday & Hot ๐Ÿฅต      \\
\hline
\end{longtable}
$ table --table-format latex-raw
\begin{tabular}{ll}
\hline
 Day    & Temperature \\
\hline
 1      & 42.9        \\
 2      &             \\
 Friday & Hot ๐Ÿฅต      \\
\hline
\end{tabular}
$ table --table-format mediawiki
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! Day    !! Temperature
|-
| 1      || 42.9
|-
| 2      ||
|-
| Friday || Hot ๐Ÿฅต
|}
$ table --table-format mixed-grid
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฏโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”‘
โ”‚ Day    โ”‚ Temperature โ”‚
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฟโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฅ
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2      โ”‚             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ”•โ”โ”โ”โ”โ”โ”โ”โ”โ”ทโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”™
$ table --table-format mixed-outline
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฏโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”‘
โ”‚ Day    โ”‚ Temperature โ”‚
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฟโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฅ
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”‚ 2      โ”‚             โ”‚
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ”•โ”โ”โ”โ”โ”โ”โ”โ”โ”ทโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”™
$ table --table-format moinmoin
|| ''' Day    ''' || ''' Temperature ''' ||
||  1       ||  42.9         ||
||  2       ||               ||
||  Friday  ||  Hot ๐Ÿฅต       ||
$ table --table-format orgtbl
| Day    | Temperature |
|--------+-------------|
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
$ table --table-format outline
+--------+-------------+
| Day    | Temperature |
+========+=============+
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
+--------+-------------+
$ table --table-format pipe
| Day    | Temperature |
|:-------|:------------|
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
$ table --table-format plain
Day     Temperature
1       42.9
2
Friday  Hot ๐Ÿฅต
$ table --table-format presto
 Day    | Temperature
--------+-------------
 1      | 42.9
 2      |
 Friday | Hot ๐Ÿฅต
$ table --table-format pretty
+--------+-------------+
|  Day   | Temperature |
+--------+-------------+
|   1    |    42.9     |
|   2    |             |
| Friday |   Hot ๐Ÿฅต    |
+--------+-------------+
$ table --table-format psql
+--------+-------------+
| Day    | Temperature |
|--------+-------------|
| 1      | 42.9        |
| 2      |             |
| Friday | Hot ๐Ÿฅต      |
+--------+-------------+
$ table --table-format rounded-grid
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Day    โ”‚ Temperature โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2      โ”‚             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
$ table --table-format rounded-outline
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Day    โ”‚ Temperature โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”‚ 2      โ”‚             โ”‚
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
$ table --table-format rst
======  ===========
Day     Temperature
======  ===========
1       42.9
2
Friday  Hot ๐Ÿฅต
======  ===========
$ table --table-format simple
Day     Temperature
------  -----------
1       42.9
2
Friday  Hot ๐Ÿฅต
$ table --table-format simple-grid
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Day    โ”‚ Temperature โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2      โ”‚             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
$ table --table-format simple-outline
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Day    โ”‚ Temperature โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1      โ”‚ 42.9        โ”‚
โ”‚ 2      โ”‚             โ”‚
โ”‚ Friday โ”‚ Hot ๐Ÿฅต      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
$ table --table-format textile
|_.  Day    |_. Temperature |
|<. 1       |<. 42.9        |
|<. 2       |<.             |
|<. Friday  |<. Hot ๐Ÿฅต      |
$ table --table-format tsv
Day   	Temperature
1     	42.9
2
Friday	Hot ๐Ÿฅต
$ table --table-format unsafehtml
<table>
<thead>
<tr><th>Day   </th><th>Temperature</th></tr>
</thead>
<tbody>
<tr><td>1     </td><td>42.9       </td></tr>
<tr><td>2     </td><td>           </td></tr>
<tr><td>Friday</td><td>Hot ๐Ÿฅต     </td></tr>
</tbody>
</table>
$ table --table-format vertical
***************************[ 1. row ]***************************
Day         | 1
Temperature | 42.9
***************************[ 2. row ]***************************
Day         | 2
Temperature | 
***************************[ 3. row ]***************************
Day         | Friday
Temperature | Hot ๐Ÿฅต
$ table --table-format youtrack
||  Day     ||  Temperature  ||
|  1       |  42.9         |
|  2       |               |
|  Friday  |  Hot ๐Ÿฅต       |

Get table formatยถ

You can get the ID of the current table format from the context:

from click_extra import command, echo, pass_context, table_format_option

@command
@table_format_option
@pass_context
def vanilla_command(ctx):
    format_id = ctx.meta["click_extra.table_format"]
    echo(f"Table format: {format_id}")

    data = ((1, 87), (2, 80), (3, 79))
    headers = ("day", "temperature")
    ctx.print_table(data, headers)
$ vanilla --table-format fancy-outline
Table format: fancy-outline
โ•’โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚ day โ”‚ temperature โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1   โ”‚ 87          โ”‚
โ”‚ 2   โ”‚ 80          โ”‚
โ”‚ 3   โ”‚ 79          โ”‚
โ•˜โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•›

click_extra.table APIยถ

        classDiagram
  ExtraOption <|-- TableFormatOption
  StrEnum <|-- TableFormat
    

Collection of table rendering utilities.

class click_extra.table.TableFormat(*values)[source]ยถ

Bases: StrEnum

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. So maybe it has been silently deprecated? Hence my proposal to remove it in python-tabulate#375.

ASCIIDOC = 'asciidoc'ยถ
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'ยถ
HTML = 'html'ยถ
JIRA = 'jira'ยถ
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'ยถ
TSV = 'tsv'ยถ
UNSAFEHTML = 'unsafehtml'ยถ
VERTICAL = 'vertical'ยถ
YOUTRACK = 'youtrack'ยถ
click_extra.table.MARKUP_FORMATS = {TableFormat.ASCIIDOC, TableFormat.CSV, TableFormat.CSV_EXCEL, TableFormat.CSV_EXCEL_TAB, TableFormat.CSV_UNIX, TableFormat.GITHUB, TableFormat.HTML, TableFormat.JIRA, TableFormat.LATEX, TableFormat.LATEX_BOOKTABS, TableFormat.LATEX_LONGTABLE, TableFormat.LATEX_RAW, TableFormat.MEDIAWIKI, TableFormat.MOINMOIN, TableFormat.ORGTBL, TableFormat.PIPE, TableFormat.RST, TableFormat.TEXTILE, TableFormat.TSV, TableFormat.UNSAFEHTML, TableFormat.YOUTRACK}ยถ

Subset of table formats that are considered as markup rendering.

click_extra.table.DEFAULT_FORMAT = TableFormat.ROUNDED_OUTLINEยถ

Default table format, if none is specified.

click_extra.table.render_table(table_data, headers=None, table_format=None, **kwargs)[source]ยถ

Render a table and return it as a string.

Return type:

str

click_extra.table.print_table(table_data, headers=None, table_format=None, **kwargs)[source]ยถ

Render a table and print it to the console.

Return type:

None

class click_extra.table.TableFormatOption(param_decls=None, type=Choice(['asciidoc', 'csv', 'csv-excel', 'csv-excel-tab', 'csv-unix', 'double-grid', 'double-outline', 'fancy-grid', 'fancy-outline', 'github', 'grid', 'heavy-grid', 'heavy-outline', 'html', 'jira', 'latex', 'latex-booktabs', 'latex-longtable', 'latex-raw', 'mediawiki', 'mixed-grid', 'mixed-outline', 'moinmoin', 'orgtbl', 'outline', 'pipe', 'plain', 'presto', 'pretty', 'psql', 'rounded-grid', 'rounded-outline', 'rst', 'simple', 'simple-grid', 'simple-outline', 'textile', 'tsv', 'unsafehtml', 'vertical', 'youtrack']), default='rounded-outline', expose_value=False, is_eager=True, help='Rendering style of tables.', **kwargs)[source]ยถ

Bases: ExtraOption

A pre-configured option that is adding a --table-format flag to select the rendering style of a table.

The selected table format ID is made available in the context in ctx.meta["click_extra.table_format"].

init_formatter(ctx, param, value)[source]ยถ

Save table format in the context, and adds print_table() to it.

The print_table(table_data, headers, **kwargs) method added to the context is a ready-to-use helper that takes for parameters:

  • table_data: a 2-dimensional iterable of iterables for rows and cells values,

  • headers: a list of string to be used as column headers,

  • **kwargs: any extra keyword argument supported by the underlying table rendering function.

The rendering style of the table is normalized to one of the supported TableFormat enum.

Return type:

None