beautifultable package¶
Module contents¶
- class beautifultable.BeautifulTable(maxwidth=80, default_alignment=Alignment.ALIGN_CENTER, default_padding=1, precision=3, serialno=False, serialno_header='SN', detect_numerics=True, sign=SignMode.SM_MINUS, **kwargs)[source]¶
Utility Class to print data in tabular format to terminal.
- Parameters
maxwidth (int, optional) – maximum width of the table in number of characters. this is ignored when manually setting the width of the columns. if this value is too low with respect to the number of columns and width of padding, the resulting table may override it(default 80).
default_alignment (int, optional) – Default alignment for new columns(default beautifultable.ALIGN_CENTER).
default_padding (int, optional) – Default width of the left and right padding for new columns(default 1).
precision (int, optional) – All float values will have maximum number of digits after the decimal, capped by this value(Default 3).
serialno (bool, optional) – If true, a column will be rendered with serial numbers(DEPRECATED).
serialno_header (str, optional) – The header of the serial number column if rendered(DEPRECATED).
detect_numerics (bool, optional) – Whether numeric strings should be automatically detected(Default True).
sign (SignMode, optional) – Parameter to control how signs in numeric data are displayed. (default beautifultable.SM_MINUS).
- precision¶
All float values will have maximum number of digits after the decimal, capped by this value(Default 3).
- Type
int
- detect_numerics¶
Whether numeric strings should be automatically detected(Default True).
- Type
bool
- property shape¶
Read only attribute which returns the shape of the table.
- property sign¶
Attribute to control how signs are displayed for numerical data.
It can be one of the following:
Option
Meaning
beautifultable.SM_PLUS
A sign should be used for both +ve and -ve numbers.
beautifultable.SM_MINUS
A sign should only be used for -ve numbers.
beautifultable.SM_SPACE
A leading space should be used for +ve numbers and a minus sign for -ve numbers.
- property border¶
Characters used to draw the border of the table.
You can set this directly to a character or use it’s several attribute to control how each section of the table is rendered. It is an instance of
BTBorder
- property junction¶
Character used to draw junctions in the row separator.
- property maxwidth¶
get/set the maximum width of the table.
The width of the table is guaranteed to not exceed this value. If it is not possible to print a given table with the width provided, this value will automatically adjust.
- set_style(style)[source]¶
Set the style of the table from a predefined set of styles.
- Parameters
style (Style) –
It can be one of the following:
beautifultable.STYLE_DEFAULT
beautifultable.STYLE_NONE
beautifultable.STYLE_DOTTED
beautifultable.STYLE_MYSQL
beautifultable.STYLE_SEPARATED
beautifultable.STYLE_COMPACT
beautifultable.STYLE_MARKDOWN
beautifultable.STYLE_RESTRUCTURED_TEXT
beautifultable.STYLE_BOX
beautifultable.STYLE_BOX_DOUBLED
beautifultable.STYLE_BOX_ROUNDED
beautifultable.STYLE_GRID
- clear(reset_columns=False, **kwargs)[source]¶
Clear the contents of the table.
Clear all rows of the table, and if specified clears all column specific data.
- Parameters
reset_columns (bool, optional) – If it is true(default False), all metadata of columns such as their alignment, padding, width, etc. are also cleared and number of columns is set to 0.
- stream(rows, append=False)[source]¶
Get a generator for the table.
This should be used in cases where data takes time to retrieve and it is required to be displayed as soon as possible. Any existing rows in the table shall also be returned. It is essential that atleast one of column header, width or existing rows set before calling this method.
- Parameters
rows (iterable) – A generator which yields one row at a time.
append (bool, optional) – If rows should also be appended to the table.(Default False)
- Returns
string representation of the table as a generators
- Return type
iterable
- to_csv(file_name, *args, **kwargs)[source]¶
Export table to CSV format.
- Parameters
file_name (str) – Path to CSV file.
- from_csv(file_name, header=True, **kwargs)[source]¶
Create table from CSV file.
- Parameters
file_name (str) – Path to CSV file.
header (bool, optional) – Whether First row in CSV file should be parsed as table header.
- Raises
ValueError – If file_name is not str type.
FileNotFoundError – If file_name is not valid path to file.
- class beautifultable.BTRowCollection(table)[source]¶
- property separator¶
Character used to draw the line seperating two rows.
- pop(index=- 1)[source]¶
Remove and return row at index (default last).
- Parameters
index (int, str) – index or heading of the row. Normal list rules apply.
- insert(index, row, header=None)[source]¶
Insert a row before index in the table.
- Parameters
index (int) – List index rules apply
row (iterable) – Any iterable of appropriate length.
header (str, optional) – Heading of the row
- Raises
TypeError: – If row is not an iterable.
ValueError: – If size of row is inconsistent with the current number of columns.
- append(row, header=None)[source]¶
Append a row to end of the table.
- Parameters
row (iterable) – Any iterable of appropriate length.
header (str, optional) – Heading of the row
- update(key, value)[source]¶
Update row(s) identified with key in the table.
key can be a index or a slice object.
- Parameters
key (int or slice) – index of the row, or a slice object.
value (iterable) – If an index is specified, value should be an iterable of appropriate length. Instead if a slice object is passed as key, value should be an iterable of rows.
- Raises
IndexError: – If index specified is out of range.
TypeError: – If value is of incorrect type.
ValueError: – If length of row does not matches number of columns.
- sort(key, reverse=False)[source]¶
Stable sort of the table IN-PLACE with respect to a column.
- Parameters
key (int, str) – index or header of the column. Normal list rules apply.
reverse (bool) – If True then table is sorted as if each comparison was reversed.
- class beautifultable.BTColumnCollection(table, default_alignment, default_padding)[source]¶
- property padding¶
Set width for left and rigth padding of the columns of the table.
- property header¶
get/set headings for the columns of the table.
It can be any iterable with all members an instance of str or None.
- property alignment¶
get/set alignment of the columns of the table.
It can be any iterable containing only the following:
beautifultable.ALIGN_LEFT
beautifultable.ALIGN_CENTER
beautifultable.ALIGN_RIGHT
- property width¶
get/set width for the columns of the table.
Width of the column specifies the max number of characters a column can contain. Larger characters are handled according to width_exceed_policy. This can be one of ‘auto’, a non-negative integer or an iterable of the same length as the number of columns. If set to anything other than ‘auto’, the user is responsible for updating it if new columns are added or existing ones are updated.
- property padding_left¶
get/set width for left padding of the columns of the table.
Left Width of the padding specifies the number of characters on the left of a column reserved for padding. By Default It is 1.
- property padding_right¶
get/set width for right padding of the columns of the table.
Right Width of the padding specifies the number of characters on the rigth of a column reserved for padding. By default It is 1.
- property width_exceed_policy¶
Attribute to control how exceeding column width should be handled.
It can be one of the following:
Option
Meaning
beautifulbable.WEP_WRAP
An item is wrapped so every line fits within it’s column width.
beautifultable.WEP_STRIP
An item is stripped to fit in it’s column.
beautifultable.WEP_ELLIPSIS
An item is stripped to fit in it’s column and appended with …(Ellipsis).
- property default_alignment¶
Attribute to control the alignment of newly created columns.
It can be one of the following:
Option
Meaning
beautifultable.ALIGN_LEFT
New columns are left aligned.
beautifultable.ALIGN_CENTER
New columns are center aligned.
beautifultable.ALIGN_RIGHT
New columns are right aligned.
- property default_padding¶
Initial value for Left and Right padding widths for new columns.
- property separator¶
Character used to draw the line seperating two columns.
- pop(index=- 1)[source]¶
Remove and return column at index (default last).
- Parameters
index (int, str) – index of the column, or the header of the column. If index is specified, then normal list rules apply.
- Raises
TypeError: – If index is not an instance of int, or str.
IndexError: – If Table is empty.
- update(key, value)[source]¶
Update a column named header in the table.
If length of column is smaller than number of rows, lets say k, only the first k values in the column is updated.
- Parameters
key (int, str) – If key is int, column at index key is updated. If key is str, the first column with heading key is updated.
value (iterable) – Any iterable of appropriate length.
- Raises
TypeError: – If length of column is shorter than number of rows.
ValueError: – If no column exists with heading header.
- insert(index, column, header=None, padding_left=None, padding_right=None, alignment=None)[source]¶
Insert a column before index in the table.
If length of column is bigger than number of rows, lets say k, only the first k values of column is considered. If column is shorter than ‘k’, ValueError is raised.
Note that Table remains in consistent state even if column is too short. Any changes made by this method is rolled back before raising the exception.
- Parameters
index (int) – List index rules apply.
column (iterable) – Any iterable of appropriate length.
header (str, optional) – Heading of the column.
padding_left (int, optional) – Left padding of the column.
padding_right (int, optional) – Right padding of the column.
alignment (Alignment, optional) – alignment of the column.
- Raises
TypeError: – If header is not of type str.
ValueError: – If length of column is shorter than number of rows.
- append(column, header=None, padding_left=None, padding_right=None, alignment=None)[source]¶
Append a column to end of the table.
- Parameters
column (iterable) – Any iterable of appropriate length.
header (str, optional) – Heading of the column
padding_left (int, optional) – Left padding of the column
padding_right (int, optional) – Right padding of the column
alignment (Alignment, optional) – alignment of the column
- class beautifultable.BTRowHeader(table, value)[source]¶
- index(item, *args)¶
Returns the index of item
- class beautifultable.BTColumnHeader(table, value)[source]¶
- property alignment¶
get/set alignment of the column header of the table.
It can be any iterable containing only the following:
beautifultable.ALIGN_LEFT
beautifultable.ALIGN_CENTER
beautifultable.ALIGN_RIGHT
- property separator¶
Character used to draw the line seperating header from the table.
- property junction¶
Character used to draw junctions in the header separator.
- index(item, *args)¶
Returns the index of item
- class beautifultable.BTBorder(top, left, bottom, right, top_left, bottom_left, bottom_right, top_right, header_left, header_right, top_junction, left_junction, bottom_junction, right_junction)[source]¶
Class to control how each section of the table’s border is rendered.
To disable a behaviour, just set its corresponding attribute to an empty string
- top¶
Character used to draw the top border.
- Type
str
- left¶
Character used to draw the left border.
- Type
str
- bottom¶
Character used to draw the bottom border.
- Type
str
- right¶
Character used to draw the right border.
- Type
str
- top_left¶
Left most character of the top border.
- Type
str
- bottom_left¶
Left most character of the bottom border.
- Type
str
- bottom_right¶
Right most character of the bottom border.
- Type
str
- top_right¶
Right most character of the top border.
- Type
str
- header_left¶
Left most character of the header separator.
- Type
str
- header_right¶
Right most character of the header separator.
- Type
str
- top_junction¶
Junction character for top border.
- Type
str
- left_junction¶
Junction character for left border.
- Type
str
- bottom_junction¶
Junction character for bottom border.
- Type
str
- right_junction¶
Junction character for right border.
- Type
str