ansitable


Nameansitable JSON
Version 0.9.10 PyPI version JSON
download
home_page
SummaryQuick and easy display of tabular data and matrices with optional ANSI color and borders
upload_time2023-11-13 01:36:59
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords table table layout table format tabular tabular layout tabular format ansi ansi art ansi box characters color ansi color matrix format
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version fury.io](https://badge.fury.io/py/ansitable.svg)](https://pypi.python.org/pypi/ansitable/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/ansitable)](https://pypistats.org/packages/ansitable)
[![Anaconda version](https://anaconda.org/conda-forge/ansitable/badges/version.svg)](https://anaconda.org/conda-forge/ansitable)
[![pyversions](https://img.shields.io/pypi/pyversions/ansitable)](https://pypi.python.org/pypi/ansitable/)
[![PyPI status](https://img.shields.io/pypi/status/ansitable.svg)](https://pypi.python.org/pypi/ansitable/)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/petercorke/ansitable/graphs/commit-activity)
[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/petercorke/ansitable/blob/master/LICENSE)

- GitHub repository: [https://github.com/petercorke/ansitable](https://github.com/petercorke/ansitable)
- Dependencies: [`colored`](https://pypi.org/project/colored)

# Synopsis

Painless creation of nice-looking [tables of data](#tables) or [matrices](#matrices) in Python.

What's new:

0.9.5:

- methods to format table as MarkDown or LaTeX
- work with Python 3.4

0.9.3:

- create matrices as well as tables
- option to suppress color output

# Tables

Painless creation of nice-looking tables of data for Python.

![colored table](https://github.com/petercorke/ansitable/raw/master/figs/colortable.png) 

## Starting simple

```python
 1 | from ansitable import ANSITable, Column
 2 |
 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 an `ANSITable` object and the arguments are a sequence of 
column names followed by `ANSITable` keyword 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         9  
```

By default output is printed to the console (`stdout`) but we can also:

- provide a `file` option to `.print()` to allow writing to a specified output stream, the
default is `stdout`.
- obtain a multi-line string version of the entire table as `str(table)`.

The more general solution is to provide a sequence of `Column` objects which 
allows many column specific options to be given, as we shall see later. 
For now though, we could rewrite the example above as:

```python
table = ANSITable(
        Column("col1"),
        Column("column 2 has a big header"),
        Column("column 3")
    )
```

or as

```python
table = 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 for
`Column` and are given below.

***
We can specify a [Python `format()` style format string](https://docs.python.org/3/library/string.html#formatspec) for any column - by default it
is the general formatting option `"{}"`.
You may choose to left or right justify values via the format string, `ansitable` provides control over how those resulting strings are justified within the column.

```python
table = ANSITable(
        Column("col1"),
        Column("column 2 has a big header", "{:.3g}"),  # CHANGE
        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 yields

```
         col1  column 2 has a big header    column 3  
    aaaaaaaaa                        2.2      3.0000  
bbbbbbbbbbbbb                        5.5      6.0000  
      ccccccc                        8.8      9.0000  
      
```
Alternatively 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 the `width` argument

```python
table = ANSITable(
        Column("col1", width=10),                      # CHANGE
        Column("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 yields


```
      col1  column 2 has a big header    column 3  
 aaaaaaaaa                        2.2      3.0000  
bbbbbbbbb…                        5.5      6.0000  
   ccccccc                        8.8      9.0000  

```
where 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 the `ANSITable` option `ellipsis=False`.  The Unicode ellipsis
character u+2026 is used.

## Borders
We can add a table border made up of regular ASCII characters

```python
table = 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 the [ANSI box-drawing characters](https://en.wikipedia.org/wiki/Box-drawing_character) which are supported by most terminal
emulators

```python
table = 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 alignment
We can change the alignment of data and heading for any column with the alignment flags `"<"` (left), 
`">"` (right) and `"^"` (centered).

```python
table = ANSITable(
        Column("col1"),
        Column("column 2 has a big header", colalign="^"),  # CHANGE
        Column("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 independently

```python
table = ANSITable(
        Column("col1", headalign="<"),                      # CHANGE
        Column("column 2 has a big header", colalign="^"),
        Column("column 3", colalign="<"),                   # CHANGE
        border="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.

## Color
If you have the `colored` package 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.

```python
table = ANSITable(
    Column("col1", headalign="<", colcolor="red", headstyle="underlined"),  # CHANGE
    Column("column 2 has a big header", colalign="^", colstyle="reverse"),  # CHANGE
    Column("column 3", colalign="<", colbgcolor="green"),                   # CHANGE
    border="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 yields

![colored table](https://github.com/petercorke/ansitable/raw/master/figs/colortable.png) 

It 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>>`.

```python
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 options

### ANSITable
These keyword arguments control the styling of the entire table.

| Keyword  | Default | Purpose |
|----      |----     |----    |
colsep | 2 | Gap between columns (in spaces)
offset | 0 | Gap at start of each row, shifts the table to the left
border | no border  | Border style: 'ascii', 'thin', 'thick', 'double'
bordercolor | |Border color, see [possible values](https://pypi.org/project/colored)
ellipsis | True | Add an ellipsis if a wide column is truncated
header | True | Include the column header row
columns | | Specify the number of columns if `header=False` and no header name or `Column` arguments are given
color | True | Enable color 

- Color is only possible if the `colored` package is installed
- If `color` is False then no color escape sequences will be emitted, useful 
  override for tables included in Sphinx documentation.

### Column
These keyword arguments control the styling of a single column.

| Keyword  | Default | Purpose |
|----      |----     |----    |
fmt | `"{}"` | format string for the column value, or a callable that maps the column value to a string
width || maximum column width, excess will be truncated
colcolor || Text color, see [possible values](https://pypi.org/project/colored)
colbgcolor || Text background color, see [possible values](https://pypi.org/project/colored)
colstyle  || Text style: "bold", "underlined", "reverse", "dim", "blink"
colalign | `">"` | Text alignment: `">"` (left), `"<"` (right), `"^"` (centered)
headcolor || Heading text color, see [possible values](https://pypi.org/project/colored)
headbgcolor || Heading text background color, see [possible values](https://pypi.org/project/colored)
headstyle || Heading 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 formats

The 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.

```python
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 Markdown

```
table.markdown()

|          col1 | column 2 has a big header | column 3 |
| ------------: | ------------------------: | -------: |
|     aaaaaaaaa |                       2.2 |        3 |
| bbbbbbbbbbbbb |                      -5.5 |        6 |
|       ccccccc |                       8.8 |       -9 |
```
or LaTex

```
table.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.


# Matrices

Painless creation of nice-looking matrices for Python.


We can create a formatter for NumPy arrays (1D or 2D)

```python
from ansitable import ANSIMatrix
formatter = ANSIMatrix(style='thick')
```

and then use it to format a NumPy array

```python
m = np.random.rand(4,4) - 0.5
m[0,0] = 1.23456e-14
formatter.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 suffixes


```python
formatter.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   ┃
┗                                           ┛3
```

By default output is printed to the console (stdout) but we can also:

* provide a `file` option to `.print()` to allow writing to a specified output stream, the default is `stdout`.
* 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.

### ANSIMatrix
These keyword arguments control the overall styling and operation of the formatter.

| Keyword  | Default | Purpose |
|----      |----     |----    |
style | `"thin"` | `"thin"`, `"round"`, `"thick"`, `"double"`
fmt | `"{:< 10.3g}"` | format for each element
squish | True | set small elements to zero
squishtol | 100 | elements less than `squishtol * eps` are set to zero

### Formatter
A formatter takes additional arguments to the styling for a particular call.

| Keyword  | Default | Purpose |
|----      |----     |----    |
suffix_super | `""` | superscript suffix text
suffix_sub | `""` | subscript suffix text

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "ansitable",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "table,table layout,table format,tabular,tabular layout,tabular format,ANSI,ANSI art,ANSI box characters,color,ANSI color,matrix format",
    "author": "",
    "author_email": "Peter Corke <rvc@petercorke.com>",
    "download_url": "https://files.pythonhosted.org/packages/5b/73/fbb3e0903641c9122cca2a0069a58fbecd0be11f1041fd3f03c1196ddd9e/ansitable-0.9.10.tar.gz",
    "platform": null,
    "description": "[![PyPI version fury.io](https://badge.fury.io/py/ansitable.svg)](https://pypi.python.org/pypi/ansitable/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/ansitable)](https://pypistats.org/packages/ansitable)\n[![Anaconda version](https://anaconda.org/conda-forge/ansitable/badges/version.svg)](https://anaconda.org/conda-forge/ansitable)\n[![pyversions](https://img.shields.io/pypi/pyversions/ansitable)](https://pypi.python.org/pypi/ansitable/)\n[![PyPI status](https://img.shields.io/pypi/status/ansitable.svg)](https://pypi.python.org/pypi/ansitable/)\n[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/petercorke/ansitable/graphs/commit-activity)\n[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/petercorke/ansitable/blob/master/LICENSE)\n\n- GitHub repository: [https://github.com/petercorke/ansitable](https://github.com/petercorke/ansitable)\n- Dependencies: [`colored`](https://pypi.org/project/colored)\n\n# Synopsis\n\nPainless creation of nice-looking [tables of data](#tables) or [matrices](#matrices) in Python.\n\nWhat's new:\n\n0.9.5:\n\n- methods to format table as MarkDown or LaTeX\n- work with Python 3.4\n\n0.9.3:\n\n- create matrices as well as tables\n- option to suppress color output\n\n# Tables\n\nPainless creation of nice-looking tables of data for Python.\n\n![colored table](https://github.com/petercorke/ansitable/raw/master/figs/colortable.png) \n\n## Starting simple\n\n```python\n 1 | from ansitable import ANSITable, Column\n 2 |\n 3 | table = ANSITable(\"col1\", \"column 2 has a big header\", \"column 3\")\n 4 | table.row(\"aaaaaaaaa\", 2.2, 3)\n 5 | table.row(\"bbbbbbbbbbbbb\", 5.5, 6)\n 6 | table.row(\"ccccccc\", 8.8, 9)\n 7 | table.print()\n\n```\nLine 3 constructs an `ANSITable` object and the arguments are a sequence of \ncolumn names followed by `ANSITable` keyword arguments - there are none in this first example.  Since there are three column names this this will be \na 3-column table.\nLines 4-6 add rows, 3 data values for each row.\n\nLine 7 prints the table and yields a tabular display\nwith column widths automatically chosen, and headings and column \ndata all right-justified (default)\n\n```\n         col1  column 2 has a big header  column 3  \n    aaaaaaaaa                        2.2         3  \nbbbbbbbbbbbbb                        5.5         6  \n      ccccccc                        8.8         9  \n```\n\nBy default output is printed to the console (`stdout`) but we can also:\n\n- provide a `file` option to `.print()` to allow writing to a specified output stream, the\ndefault is `stdout`.\n- obtain a multi-line string version of the entire table as `str(table)`.\n\nThe more general solution is to provide a sequence of `Column` objects which \nallows many column specific options to be given, as we shall see later. \nFor now though, we could rewrite the example above as:\n\n```python\ntable = ANSITable(\n        Column(\"col1\"),\n        Column(\"column 2 has a big header\"),\n        Column(\"column 3\")\n    )\n```\n\nor as\n\n```python\ntable = ANSITable()\ntable.addcolumn(\"col1\")\ntable.addcolumn(\"column 2 has a big header\")\ntable.addcolumn(\"column 3\")\n```\nwhere the keyword arguments to `.addcolumn()` are the same as those for\n`Column` and are given below.\n\n***\nWe can specify a [Python `format()` style format string](https://docs.python.org/3/library/string.html#formatspec) for any column - by default it\nis the general formatting option `\"{}\"`.\nYou may choose to left or right justify values via the format string, `ansitable` provides control over how those resulting strings are justified within the column.\n\n```python\ntable = ANSITable(\n        Column(\"col1\"),\n        Column(\"column 2 has a big header\", \"{:.3g}\"),  # CHANGE\n        Column(\"column 3\", \"{:-10.4f}\")\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nwhich yields\n\n```\n         col1  column 2 has a big header    column 3  \n    aaaaaaaaa                        2.2      3.0000  \nbbbbbbbbbbbbb                        5.5      6.0000  \n      ccccccc                        8.8      9.0000  \n      \n```\nAlternatively we can specify the format argument as a function that converts\nthe value to a string.\n\n\n***\nThe data in column 1 is quite long, we might wish to set a maximum column width which\nwe can do using the `width` argument\n\n```python\ntable = ANSITable(\n        Column(\"col1\", width=10),                      # CHANGE\n        Column(\"column 2 has a big header\", \"{:.3g}\"),\n        Column(\"column 3\", \"{:-10.4f}\")\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nwhich yields\n\n\n```\n      col1  column 2 has a big header    column 3  \n aaaaaaaaa                        2.2      3.0000  \nbbbbbbbbb\u2026                        5.5      6.0000  \n   ccccccc                        8.8      9.0000  \n\n```\nwhere we see that the data in column 1 has been truncated.\n\nIf you don't like the ellipsis you can turn it off, and get to see one more\ncharacter, with the `ANSITable` option `ellipsis=False`.  The Unicode ellipsis\ncharacter u+2026 is used.\n\n## Borders\nWe can add a table border made up of regular ASCII characters\n\n```python\ntable = ANSITable(\n        Column(\"col1\"),\n        Column(\"column 2 has a big header\"),\n        Column(\"column 3\"),\n        border=\"ascii\"                          # CHANGE\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nwhich yields\n\n```\n+--------------+---------------------------+----------+\n|         col1 | column 2 has a big header | column 3 |\n+--------------+---------------------------+----------+\n|    aaaaaaaaa |                       2.2 |        3 |\n|bbbbbbbbbbbbb |                       5.5 |        6 |\n|      ccccccc |                       8.8 |        9 |\n+--------------+---------------------------+----------+\n```\n***\nOr we can construct a border using the [ANSI box-drawing characters](https://en.wikipedia.org/wiki/Box-drawing_character) which are supported by most terminal\nemulators\n\n```python\ntable = ANSITable(\n        Column(\"col1\"),\n        Column(\"column 2 has a big header\"),\n        Column(\"column 3\"),\n        border=\"thick\"                           # CHANGE\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nwhich yields\n\n```\n\u250f\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n\u2503         col1 \u2503 column 2 has a big header \u2503 column 3 \u2503\n\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252b\n\u2503    aaaaaaaaa \u2503                       2.2 \u2503        3 \u2503\n\u2503bbbbbbbbbbbbb \u2503                       5.5 \u2503        6 \u2503\n\u2503      ccccccc \u2503                       8.8 \u2503        9 \u2503\n\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251b\n```\n_Note: this actually looks better on the console than it does in GitHub markdown._\n\nOther border options include \"thin\", \"rounded\" (thin with round corners) and \"double\".\n\n## Header and column alignment\nWe can change the alignment of data and heading for any column with the alignment flags `\"<\"` (left), \n`\">\"` (right) and `\"^\"` (centered).\n\n```python\ntable = ANSITable(\n        Column(\"col1\"),\n        Column(\"column 2 has a big header\", colalign=\"^\"),  # CHANGE\n        Column(\"column 3\"),\n        border=\"thick\"\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nwhich yields\n\n\n```\n\u250f\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n\u2503         col1 \u2503 column 2 has a big header \u2503 column 3 \u2503\n\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252b\n\u2503    aaaaaaaaa \u2503            2.2            \u2503        3 \u2503\n\u2503bbbbbbbbbbbbb \u2503            5.5            \u2503        6 \u2503\n\u2503      ccccccc \u2503            8.8            \u2503        9 \u2503\n\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251b\n```\nwhere the data for column 2 has been centered.\n***\nHeading and data alignment for any column can be set independently\n\n```python\ntable = ANSITable(\n        Column(\"col1\", headalign=\"<\"),                      # CHANGE\n        Column(\"column 2 has a big header\", colalign=\"^\"),\n        Column(\"column 3\", colalign=\"<\"),                   # CHANGE\n        border=\"thick\"\n    )\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\nyields\n\n```\n\u250f\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n\u2503col1          \u2503 column 2 has a big header \u2503 column 3 \u2503\n\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u254b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252b\n\u2503    aaaaaaaaa \u2503            2.2            \u2503 3        \u2503\n\u2503bbbbbbbbbbbbb \u2503            5.5            \u2503 6        \u2503\n\u2503      ccccccc \u2503            8.8            \u2503 9        \u2503\n\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253b\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251b\n```\nwhere we have left-justified the heading for column 1 and the data for column 3.\n\n## Color\nIf you have the `colored` package installed then you can set the foreground and\nbackground color and style (bold, reverse, underlined, dim) of the header and column data, as well as the border color.\n\n```python\ntable = ANSITable(\n    Column(\"col1\", headalign=\"<\", colcolor=\"red\", headstyle=\"underlined\"),  # CHANGE\n    Column(\"column 2 has a big header\", colalign=\"^\", colstyle=\"reverse\"),  # CHANGE\n    Column(\"column 3\", colalign=\"<\", colbgcolor=\"green\"),                   # CHANGE\n    border=\"thick\", bordercolor=\"blue\"                                      # CHANGE\n)\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", 5.5, 6)\ntable.row(\"ccccccc\", 8.8, 9)\ntable.print()\n```\n\nwhich yields\n\n![colored table](https://github.com/petercorke/ansitable/raw/master/figs/colortable.png) \n\nIt is also possible to change the color of individual cells in the table\nby prefixing the value with a color enclosed in double angle brackets, for example `<<red>>`.\n\n```python\ntable = ANSITable(\"col1\", \"column 2 has a big header\", \"column 3\")\n    table.row(\"aaaaaaaaa\", 2.2, 3)\n    table.row(\"<<red>>bbbbbbbbbbbbb\", 5.5, 6)\n    table.row(\"<<blue>>ccccccc\", 8.8, 9)\n    table.print()\n```\n\n## All options\n\n### ANSITable\nThese keyword arguments control the styling of the entire table.\n\n| Keyword  | Default | Purpose |\n|----      |----     |----    |\ncolsep | 2 | Gap between columns (in spaces)\noffset | 0 | Gap at start of each row, shifts the table to the left\nborder | no border  | Border style: 'ascii', 'thin', 'thick', 'double'\nbordercolor | |Border color, see [possible values](https://pypi.org/project/colored)\nellipsis | True | Add an ellipsis if a wide column is truncated\nheader | True | Include the column header row\ncolumns | | Specify the number of columns if `header=False` and no header name or `Column` arguments are given\ncolor | True | Enable color \n\n- Color is only possible if the `colored` package is installed\n- If `color` is False then no color escape sequences will be emitted, useful \n  override for tables included in Sphinx documentation.\n\n### Column\nThese keyword arguments control the styling of a single column.\n\n| Keyword  | Default | Purpose |\n|----      |----     |----    |\nfmt | `\"{}\"` | format string for the column value, or a callable that maps the column value to a string\nwidth || maximum column width, excess will be truncated\ncolcolor || Text color, see [possible values](https://pypi.org/project/colored)\ncolbgcolor || Text background color, see [possible values](https://pypi.org/project/colored)\ncolstyle  || Text style: \"bold\", \"underlined\", \"reverse\", \"dim\", \"blink\"\ncolalign | `\">\"` | Text alignment: `\">\"` (left), `\"<\"` (right), `\"^\"` (centered)\nheadcolor || Heading text color, see [possible values](https://pypi.org/project/colored)\nheadbgcolor || Heading text background color, see [possible values](https://pypi.org/project/colored)\nheadstyle || Heading text style: \"bold\", \"underlined\", \"reverse\", \"dim\", \"blink\"\nheadalign | `\">\"` | Heading text alignment: `\">\"` (left), `\"<\"` (right), `\"^\"` (centered)\n\n \nNote that many terminal emulators do not support the \"blink\" style.\n\n# Output in other tabular formats\n\nThe main use for this package is to generate tables on the console that are easy to read, but\nsometimes you might want the table in a different format to include in\ndocumentation.\n\n```python\ntable = ANSITable(\"col1\", \"column 2 has a big header\", \"column 3\")\ntable.row(\"aaaaaaaaa\", 2.2, 3)\ntable.row(\"bbbbbbbbbbbbb\", -5.5, 6)\ntable.row(\"ccccccc\", 8.8, -9)\ntable.print()\n```\n\ncan be rendered into Markdown\n\n```\ntable.markdown()\n\n|          col1 | column 2 has a big header | column 3 |\n| ------------: | ------------------------: | -------: |\n|     aaaaaaaaa |                       2.2 |        3 |\n| bbbbbbbbbbbbb |                      -5.5 |        6 |\n|       ccccccc |                       8.8 |       -9 |\n```\nor LaTex\n\n```\ntable.latex()\n\n\\begin{tabular}{ |r|r|r| }\\hline\n\\multicolumn{1}{|r|}{col1} & \\multicolumn{1}{|r|}{column 2 has a big header} & \\multicolumn{1}{|r|}{column 3}\\\\\\hline\\hline\naaaaaaaaa & 2.2 & 3 \\\\\nbbbbbbbbbbbbb & -5.5 & 6 \\\\\nccccccc & 8.8 & -9 \\\\\n\\hline\n\\end{tabular}\n```\n\nIn both cases the method returns a string and column alignment is supported.\nMarkDown doesn't allow the header to have different alignment to the data.\n\n\n# Matrices\n\nPainless creation of nice-looking matrices for Python.\n\n\nWe can create a formatter for NumPy arrays (1D or 2D)\n\n```python\nfrom ansitable import ANSIMatrix\nformatter = ANSIMatrix(style='thick')\n```\n\nand then use it to format a NumPy array\n\n```python\nm = np.random.rand(4,4) - 0.5\nm[0,0] = 1.23456e-14\nformatter.print(m)\n```\n\nyields\n\n```\n\u250f                                           \u2513\n\u2503 0         -0.385     -0.106      0.296    \u2503\n\u2503 0.0432     0.339      0.119     -0.468    \u2503\n\u2503 0.405     -0.306      0.0165    -0.439    \u2503\n\u2503 0.203      0.4       -0.499     -0.487    \u2503\n\u2517                                           \u251b\n```\n\nwe can also add suffixes\n\n\n```python\nformatter.print(m, suffix_super='T', suffix_sub='3')\n```\n\nyields\n\n```\n\u250f                                           \u2513T\n\u2503 0         -0.239      0.186     -0.414    \u2503\n\u2503 0.49       0.215     -0.0148     0.0529   \u2503\n\u2503 0.0473     0.0311     0.45       0.394    \u2503\n\u2503-0.192      0.193     -0.455      0.0302   \u2503\n\u2517                                           \u251b3\n```\n\nBy default output is printed to the console (stdout) but we can also:\n\n* provide a `file` option to `.print()` to allow writing to a specified output stream, the default is `stdout`.\n* obtain a multi-line string version of the entire table using the `.str()` method\ninstead of `.print()`.\n\nThe formatter takes additional arguments to control the numeric format and to \ncontrol the suppression of very small values.\n\n### ANSIMatrix\nThese keyword arguments control the overall styling and operation of the formatter.\n\n| Keyword  | Default | Purpose |\n|----      |----     |----    |\nstyle | `\"thin\"` | `\"thin\"`, `\"round\"`, `\"thick\"`, `\"double\"`\nfmt | `\"{:< 10.3g}\"` | format for each element\nsquish | True | set small elements to zero\nsquishtol | 100 | elements less than `squishtol * eps` are set to zero\n\n### Formatter\nA formatter takes additional arguments to the styling for a particular call.\n\n| Keyword  | Default | Purpose |\n|----      |----     |----    |\nsuffix_super | `\"\"` | superscript suffix text\nsuffix_sub | `\"\"` | subscript suffix text\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Quick and easy display of tabular data and matrices with optional ANSI color and borders",
    "version": "0.9.10",
    "project_urls": {
        "Bug Tracker": "https://github.com/petercorke/ansitable/issues",
        "Documentation": "https://github.com/petercorke/ansitable",
        "Homepage": "https://github.com/petercorke/ansitable",
        "Source": "https://github.com/petercorke/ansitable"
    },
    "split_keywords": [
        "table",
        "table layout",
        "table format",
        "tabular",
        "tabular layout",
        "tabular format",
        "ansi",
        "ansi art",
        "ansi box characters",
        "color",
        "ansi color",
        "matrix format"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b8df72d9d030350aef8b0a6a06422b8e97139e1d5707b3feb55823914190b43d",
                "md5": "7fce48b955fe71fc1f63b4304101cf47",
                "sha256": "db3aba6b5767a967e64324bd82641d1ce836465ed71a77d7b46610dc8c897b19"
            },
            "downloads": -1,
            "filename": "ansitable-0.9.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7fce48b955fe71fc1f63b4304101cf47",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 12988,
            "upload_time": "2023-11-13T01:36:57",
            "upload_time_iso_8601": "2023-11-13T01:36:57.410420Z",
            "url": "https://files.pythonhosted.org/packages/b8/df/72d9d030350aef8b0a6a06422b8e97139e1d5707b3feb55823914190b43d/ansitable-0.9.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b73fbb3e0903641c9122cca2a0069a58fbecd0be11f1041fd3f03c1196ddd9e",
                "md5": "19606a0766cf74e5b551182fe3e2b346",
                "sha256": "7cb55000c0bbb5a9157b5d28482f265f3047f9fce8788447277eab2ce97e8b46"
            },
            "downloads": -1,
            "filename": "ansitable-0.9.10.tar.gz",
            "has_sig": false,
            "md5_digest": "19606a0766cf74e5b551182fe3e2b346",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 17466,
            "upload_time": "2023-11-13T01:36:59",
            "upload_time_iso_8601": "2023-11-13T01:36:59.391670Z",
            "url": "https://files.pythonhosted.org/packages/5b/73/fbb3e0903641c9122cca2a0069a58fbecd0be11f1041fd3f03c1196ddd9e/ansitable-0.9.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-13 01:36:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "petercorke",
    "github_project": "ansitable",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ansitable"
}
        
Elapsed time: 0.14262s