Package: ansitable
GitHub repository:https://github.com/petercorke/ansitableDependencies:coloredSynopsisPainless creation of nice-lookingtables of dataormatricesin Python.What's new:0.9.5:methods to format table as MarkDown or LaTeXwork with Python 3.40.9.3:create matrices as well as tablesoption to suppress color outputTablesPainless creation of nice-looking tables of data for Python.Starting simple1|fromansitableimportANSITable,Column2|3|table=ANSITable("col1","column 2 has a big header","column 3")4|table.row("aaaaaaaaa",2.2,3)5|table.row("bbbbbbbbbbbbb",5.5,6)6|table.row("ccccccc",8.8,9)7|table.print()Line 3 constructs anANSITableobject and the arguments are a sequence of
column names followed byANSITablekeyword arguments - there are none in this first example.  Since there are three column names this this will be
a 3-column table.
Lines 4-6 add rows, 3 data values for each row.Line 7 prints the table and yields a tabular display
with column widths automatically chosen, and headings and column
data all right-justified (default)col1  column 2 has a big header  column 3  
    aaaaaaaaa                        2.2         3  
bbbbbbbbbbbbb                        5.5         6  
      ccccccc                        8.8         9By default output is printed to the console (stdout) but we can also:provide afileoption to.print()to allow writing to a specified output stream, the
default isstdout.obtain a multi-line string version of the entire table asstr(table).The more general solution is to provide a sequence ofColumnobjects which
allows many column specific options to be given, as we shall see later.
For now though, we could rewrite the example above as:table=ANSITable(Column("col1"),Column("column 2 has a big header"),Column("column 3"))or astable=ANSITable()table.addcolumn("col1")table.addcolumn("column 2 has a big header")table.addcolumn("column 3")where the keyword arguments to.addcolumn()are the same as those forColumnand are given below.We can specify aPythonformat()style format stringfor any column - by default it
is the general formatting option"{}".
You may choose to left or right justify values via the format string,ansitableprovides control over how those resulting strings are justified within the column.table=ANSITable(Column("col1"),Column("column 2 has a big header","{:.3g}"),# CHANGEColumn("column 3","{:-10.4f}"))table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yieldscol1  column 2 has a big header    column 3  
    aaaaaaaaa                        2.2      3.0000  
bbbbbbbbbbbbb                        5.5      6.0000  
      ccccccc                        8.8      9.0000Alternatively we can specify the format argument as a function that converts
the value to a string.The data in column 1 is quite long, we might wish to set a maximum column width which
we can do using thewidthargumenttable=ANSITable(Column("col1",width=10),# CHANGEColumn("column 2 has a big header","{:.3g}"),Column("column 3","{:-10.4f}"))table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yieldscol1  column 2 has a big header    column 3  
 aaaaaaaaa                        2.2      3.0000  
bbbbbbbbb…                        5.5      6.0000  
   ccccccc                        8.8      9.0000where we see that the data in column 1 has been truncated.If you don't like the ellipsis you can turn it off, and get to see one more
character, with theANSITableoptionellipsis=False.  The Unicode ellipsis
character u+2026 is used.BordersWe can add a table border made up of regular ASCII characterstable=ANSITable(Column("col1"),Column("column 2 has a big header"),Column("column 3"),border="ascii"# CHANGE)table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yields+--------------+---------------------------+----------+
|         col1 | column 2 has a big header | column 3 |
+--------------+---------------------------+----------+
|    aaaaaaaaa |                       2.2 |        3 |
|bbbbbbbbbbbbb |                       5.5 |        6 |
|      ccccccc |                       8.8 |        9 |
+--------------+---------------------------+----------+Or we can construct a border using theANSI box-drawing characterswhich are supported by most terminal
emulatorstable=ANSITable(Column("col1"),Column("column 2 has a big header"),Column("column 3"),border="thick"# CHANGE)table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yields┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃         col1 ┃ column 2 has a big header ┃ column 3 ┃
┣━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━┫
┃    aaaaaaaaa ┃                       2.2 ┃        3 ┃
┃bbbbbbbbbbbbb ┃                       5.5 ┃        6 ┃
┃      ccccccc ┃                       8.8 ┃        9 ┃
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┛Note: this actually looks better on the console than it does in GitHub markdown.Other border options include "thin", "rounded" (thin with round corners) and "double".Header and column alignmentWe can change the alignment of data and heading for any column with the alignment flags"<"(left),">"(right) and"^"(centered).table=ANSITable(Column("col1"),Column("column 2 has a big header",colalign="^"),# CHANGEColumn("column 3"),border="thick")table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yields┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃         col1 ┃ column 2 has a big header ┃ column 3 ┃
┣━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━┫
┃    aaaaaaaaa ┃            2.2            ┃        3 ┃
┃bbbbbbbbbbbbb ┃            5.5            ┃        6 ┃
┃      ccccccc ┃            8.8            ┃        9 ┃
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┛where the data for column 2 has been centered.Heading and data alignment for any column can be set independentlytable=ANSITable(Column("col1",headalign="<"),# CHANGEColumn("column 2 has a big header",colalign="^"),Column("column 3",colalign="<"),# CHANGEborder="thick")table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()yields┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃col1          ┃ column 2 has a big header ┃ column 3 ┃
┣━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━┫
┃    aaaaaaaaa ┃            2.2            ┃ 3        ┃
┃bbbbbbbbbbbbb ┃            5.5            ┃ 6        ┃
┃      ccccccc ┃            8.8            ┃ 9        ┃
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┛where we have left-justified the heading for column 1 and the data for column 3.ColorIf you have thecoloredpackage installed then you can set the foreground and
background color and style (bold, reverse, underlined, dim) of the header and column data, as well as the border color.table=ANSITable(Column("col1",headalign="<",colcolor="red",headstyle="underlined"),# CHANGEColumn("column 2 has a big header",colalign="^",colstyle="reverse"),# CHANGEColumn("column 3",colalign="<",colbgcolor="green"),# CHANGEborder="thick",bordercolor="blue"# CHANGE)table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",5.5,6)table.row("ccccccc",8.8,9)table.print()which yieldsIt is also possible to change the color of individual cells in the table
by prefixing the value with a color enclosed in double angle brackets, for example<<red>>.table=ANSITable("col1","column 2 has a big header","column 3")table.row("aaaaaaaaa",2.2,3)table.row("<<red>>bbbbbbbbbbbbb",5.5,6)table.row("<<blue>>ccccccc",8.8,9)table.print()All optionsANSITableThese keyword arguments control the styling of the entire table.KeywordDefaultPurposecolsep2Gap between columns (in spaces)offset0Gap at start of each row, shifts the table to the leftborderno borderBorder style: 'ascii', 'thin', 'thick', 'double'bordercolorBorder color, seepossible valuesellipsisTrueAdd an ellipsis if a wide column is truncatedheaderTrueInclude the column header rowcolumnsSpecify the number of columns ifheader=Falseand no header name orColumnarguments are givencolorTrueEnable colorColor is only possible if thecoloredpackage is installedIfcoloris False then no color escape sequences will be emitted, useful
override for tables included in Sphinx documentation.ColumnThese keyword arguments control the styling of a single column.KeywordDefaultPurposefmt"{}"format string for the column value, or a callable that maps the column value to a stringwidthmaximum column width, excess will be truncatedcolcolorText color, seepossible valuescolbgcolorText background color, seepossible valuescolstyleText style: "bold", "underlined", "reverse", "dim", "blink"colalign">"Text alignment:">"(left),"<"(right),"^"(centered)headcolorHeading text color, seepossible valuesheadbgcolorHeading text background color, seepossible valuesheadstyleHeading text style: "bold", "underlined", "reverse", "dim", "blink"headalign">"Heading text alignment:">"(left),"<"(right),"^"(centered)Note that many terminal emulators do not support the "blink" style.Output in other tabular formatsThe main use for this package is to generate tables on the console that are easy to read, but
sometimes you might want the table in a different format to include in
documentation.table=ANSITable("col1","column 2 has a big header","column 3")table.row("aaaaaaaaa",2.2,3)table.row("bbbbbbbbbbbbb",-5.5,6)table.row("ccccccc",8.8,-9)table.print()can be rendered into Markdowntable.markdown()

|          col1 | column 2 has a big header | column 3 |
| ------------: | ------------------------: | -------: |
|     aaaaaaaaa |                       2.2 |        3 |
| bbbbbbbbbbbbb |                      -5.5 |        6 |
|       ccccccc |                       8.8 |       -9 |or LaTextable.latex()

\begin{tabular}{ |r|r|r| }\hline
\multicolumn{1}{|r|}{col1} & \multicolumn{1}{|r|}{column 2 has a big header} & \multicolumn{1}{|r|}{column 3}\\\hline\hline
aaaaaaaaa & 2.2 & 3 \\
bbbbbbbbbbbbb & -5.5 & 6 \\
ccccccc & 8.8 & -9 \\
\hline
\end{tabular}In both cases the method returns a string and column alignment is supported.
MarkDown doesn't allow the header to have different alignment to the data.MatricesPainless creation of nice-looking matrices for Python.We can create a formatter for NumPy arrays (1D or 2D)fromansitableimportANSIMatrixformatter=ANSIMatrix(style='thick')and then use it to format a NumPy arraym=np.random.rand(4,4)-0.5m[0,0]=1.23456e-14formatter.print(m)yields┏                                           ┓
┃ 0         -0.385     -0.106      0.296    ┃
┃ 0.0432     0.339      0.119     -0.468    ┃
┃ 0.405     -0.306      0.0165    -0.439    ┃
┃ 0.203      0.4       -0.499     -0.487    ┃
┗                                           ┛we can also add suffixesformatter.print(m,suffix_super='T',suffix_sub='3')yields┏                                           ┓T
┃ 0         -0.239      0.186     -0.414    ┃
┃ 0.49       0.215     -0.0148     0.0529   ┃
┃ 0.0473     0.0311     0.45       0.394    ┃
┃-0.192      0.193     -0.455      0.0302   ┃
┗                                           ┛3By default output is printed to the console (stdout) but we can also:provide afileoption to.print()to allow writing to a specified output stream, the default isstdout.obtain a multi-line string version of the entire table using the.str()method
instead of.print().The formatter takes additional arguments to control the numeric format and to
control the suppression of very small values.ANSIMatrixThese keyword arguments control the overall styling and operation of the formatter.KeywordDefaultPurposestyle"thin""thin","round","thick","double"fmt"{:< 10.3g}"format for each elementsquishTrueset small elements to zerosquishtol100elements less thansquishtol * epsare set to zeroFormatterA formatter takes additional arguments to the styling for a particular call.KeywordDefaultPurposesuffix_super""superscript suffix textsuffix_sub""subscript suffix text