Tableprint¶
About¶
Tableprint is a library for printing out numerical data in Ascii formatted tables. Check it out on github. You can use it to print single rows of data at a time (useful for printing ongoing computation results).
Quickstart¶
Tableprint offers two functions that print a table directly,
tableprint.table
and tableprint.dataframe
. The first takes a numpy array and a list of
headers, whereas the second takes a pandas DataFrame as input. For example, you can do the following:
>>> tableprint.table(np.random.randn(10, 3), ['A', 'B', 'C'])
If you want to append to a table on the fly, you can use the functions tableprint.header
,
tableprint.row
, and finally tableprint.bottom
. These functions return a formatted string
given a list of headers, an array of data, and a number of columns, respectively. For example
>>> print(tableprint.header(['A', 'B', 'C']))
>>> for ix in range(10):
# insert time-intensive data collection here
data = np.random.randn(3)
# print data to stdout
print(tableprint.row(data), flush=True)
>>> print(tableprint.bottom(3))
Sometimes you just want to print a fancy string but without any numbers. In that case, you can use the tableprint.banner
function:
>> tableprint.banner("Hello, World!")
All of these functions take two optional keyword arguments, a width
that defines the width of each column and a style
that specifies
what unicode or ascii characters to use to build the table. The available styles are: round
(default), fancy_grid
, grid
, clean
, and block
.
API¶
-
tableprint.
table
(data, headers=None, format_spec='5g', width=None, align='right', style='round', out=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]¶ Print a table with the given data
- data: array_like
- An (m x n) array containing the data to print (m rows of n columns)
- headers: list, optional
- A list of n strings consisting of the header of each of the n columns (Default: None)
- format_spec: string, optional
- Format specification for formatting numbers (Default: ‘5g’)
- width: int or None or array_like, optional
- The width of each column in the table. If None, tries to estimate an appropriate width based on the length of the data in the table. (Default: None)
- align: string, optional
- The alignment to use (‘left’, ‘center’, or ‘right’). (Default: ‘right’)
- style: string or tuple, optional
- A formatting style. (Default: ‘fancy_grid’)
- out: IO writer, optional
- File handle or object used to manage IO (displaying the table). Must have a write() method that takes a string argument, and a flush() method. See sys.stdout for an example. (Default: ‘sys.stdout’)
-
tableprint.
TableContext
(headers, width=11, align='right', style='round', add_hr=True, out=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]¶
-
tableprint.
dataframe
(df, **kwargs)[source]¶ Print table with data from the given pandas DataFrame
- df: DataFrame
- A pandas DataFrame with the table to print
Prints a banner message
- message: string
- The message to print in the banner
- width: int
- The minimum width of the banner (Default: 30)
- style: string
- A line formatting style (Default: ‘banner’)
- out: writer
- An object that has write() and flush() methods (Default: sys.stdout)
-
tableprint.
header
(headers, width=None, align='right', style='round', add_hr=True)[source]¶ Returns a formatted row of column header strings
- headers: list of strings
- A list of n strings, the column headers
- width: int, optional
- The width of each column. If None, automatically determines the width. (Default: None)
- style: string or tuple, optional
- A formatting style (see STYLES)
- headerstr : string
- A string consisting of the full header row to print
-
tableprint.
row
(values, width=None, format_spec='5g', align='right', style='round')[source]¶ Returns a formatted row of data
- values: array_like
- An iterable array of data (numbers or strings), each value is printed in a separate column
- width: int, optional
- The width of each column. If None, automatically determines the width. (Default: None)
- format_spec: string, optional
- The precision format string used to format numbers in the values array (Default: ‘5g’)
- align: string, optional
- The alignment to use (‘left’, ‘center’, or ‘right’). (Default: ‘right’)
- style: namedtuple, optional
- A line formatting style
- rowstr: string
- A string consisting of the full row of data to print