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=11, style='round', out=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)¶ 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 array_like, optional
- The width of each column in the table (Default: 11)
- style : string or tuple, optional
- A formatting style. (Default: ‘fancy_grid’)
- out : writer, optional
- A file handle or object that has write() and flush() methods (Default: sys.stdout)
-
tableprint.
TableContext
(headers, width=11, style='round', add_hr=True, out=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)¶
-
tableprint.
dataframe
(df, **kwargs)¶ 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=11, style='round', add_hr=True)¶ Returns a formatted row of column header strings
- headers : list of strings
- A list of n strings, the column headers
- width : int
- The width of each column (Default: 11)
- 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=11, format_spec='5g', style='round')¶ 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
- The width of each column (Default: 11)
- format_spec : string
- The precision format string used to format numbers in the values array (Default: ‘5g’)
- style : namedtuple, optional
- A line formatting style
- rowstr : string
- A string consisting of the full row of data to print
-
tableprint.
top
(n, width=11, style='round')¶ Prints the top row of a table
-
tableprint.
bottom
(n, width=11, style='round')¶ Prints the top row of a table
-
tableprint.
humantime
(time)¶ Converts a time in seconds to a reasonable human readable time
- t : float
- The number of seconds
- time : string
- The human readable formatted value of the given time