beautifultable package¶
Module contents¶
-
class
beautifultable.
BeautifulTable
(max_width=80, default_alignment=ALIGN_CENTER, default_padding=1)[source]¶ Bases:
object
Utility Class to print data in tabular format to terminal.
The instance attributes can be used to customize the look of the table. To disable a behaviour, just set its corresponding attribute to an empty string. For example, if Top border should not be drawn, set top_border_char to ‘’.
- Parameters
max_width (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).
-
left_border_char
¶ Character used to draw the left border.
- Type
str
-
right_border_char
¶ Character used to draw the right border.
- Type
str
-
top_border_char
¶ Character used to draw the top border.
- Type
str
-
bottom_border_char
¶ Character used to draw the bottom border.
- Type
str
-
header_separator_char
¶ Character used to draw the line seperating Header from data.
- Type
str
-
row_separator_char
¶ Character used to draw the line seperating two rows.
- Type
str
-
column_separator_char
¶ Character used to draw the line seperating two columns.
- Type
str
-
intersection_char
¶ Character used to draw intersection of a vertical and horizontal line. Disabling it just draws the horizontal line char in it’s place. (DEPRECATED).
- Type
str
-
intersect_top_left
¶ Left most character of the top border.
- Type
str
-
intersect_top_mid
¶ Intersection character for top border.
- Type
str
-
intersect_top_right
¶ Right most character of the top border.
- Type
str
-
intersect_header_left
¶ Left most character of the header separator.
- Type
str
-
intersect_header_mid
¶ Intersection character for header separator.
- Type
str
-
intersect_header_right
¶ Right most character of the header separator.
- Type
str
-
intersect_row_left
¶ Left most character of the row separator.
- Type
str
-
intersect_row_mid
¶ Intersection character for row separator.
- Type
str
-
intersect_row_right
¶ Right most character of the row separator.
- Type
str
-
intersect_bottom_left
¶ Left most character of the bottom border.
- Type
str
-
intersect_bottom_mid
¶ Intersection character for bottom border.
- Type
str
-
intersect_bottom_right
¶ Right most character of the bottom border.
- Type
str
-
numeric_precision
¶ All float values will have maximum number of digits after the decimal, capped by this value(Default 3).
- Type
int
-
serialno
¶ Whether automatically generated serial number should be printed for each row(Default False).
- Type
bool
-
serialno_header
¶ The header of the autogenerated serial number column. This value is only used if serialno is True(Default SN).
- Type
str
-
detect_numerics
¶ Whether numeric strings should be automatically detected(Default True).
- Type
bool
-
property
column_count
¶ Get the number of columns in the table(read only)
-
property
intersection_char
Character used to draw intersection of perpendicular lines.
Disabling it just draws the horizontal line char in it’s place. This attribute is deprecated. Use specific intersect_*_* attribute.
-
property
sign_mode
¶ 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
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
column_widths
¶ 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 the value of width_exceed_policy.
-
property
column_headers
¶ get/set titles for the columns of the table.
It can be any iterable having all memebers an instance of str.
-
property
column_alignments
¶ 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
left_padding_widths
¶ 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
right_padding_widths
¶ 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
max_table_width
¶ 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
-
set_padding_widths
(pad_width)[source]¶ Set width for left and rigth padding of the columns of the table.
- Parameters
pad_width (array_like) – pad widths for the 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.
-
copy
()[source]¶ Return a shallow copy of the table.
- Returns
shallow copy of the BeautifulTable instance.
- Return type
-
get_column_header
(index)[source]¶ Get header of a column from it’s index.
- Parameters
index (int) – Normal list rules apply.
-
get_column_index
(header)[source]¶ Get index of a column from it’s header.
- Parameters
header (str) – header of the column.
- Raises
ValueError: – If no column could be found corresponding to header.
-
get_column
(key)[source]¶ Return an iterator to a column.
- Parameters
key (int, str) – index of the column, or the header of the column. If index is specified, then normal list rules apply.
- Raises
TypeError: – If key is not of type int, or str.
- Returns
Iterator to the specified column.
- Return type
iter
-
pop_row
(index=-1)[source]¶ Remove and return row at index (default last).
- Parameters
index (int) – index of the row. Normal list rules apply.
-
pop_column
(index=-1)[source]¶ Remove and return row 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.
-
insert_row
(index, row)[source]¶ Insert a row before index in the table.
- Parameters
index (int) – List index rules apply
row (iterable) – Any iterable of appropriate length.
- Raises
TypeError: – If row is not an iterable.
ValueError: – If size of row is inconsistent with the current number of columns.
-
append_row
(row)[source]¶ Append a row to end of the table.
- Parameters
row (iterable) – Any iterable of appropriate length.
-
update_row
(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 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.
-
update_column
(header, column)[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
header (str) – Header of the column
column (iterable) – Any iterable of appropriate length.
- Raises
TypeError: – If length of column is shorter than number of rows.
ValueError: – If no column exists with title header.
-
insert_column
(index, header, column)[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.
header (str) – Title of the column.
column (iterable) – Any iterable of appropriate length.
- Raises
TypeError: – If header is not of type str.
ValueError: – If length of column is shorter than number of rows.
-
append_column
(header, column)[source]¶ Append a column to end of the table.
- Parameters
header (str) – Title of the column
column (iterable) – Any iterable of appropriate length.
-
clear
(clear_metadata=False)[source]¶ Clear the contents of the table.
Clear all rows of the table, and if specified clears all column specific data.
- Parameters
clear_metadata (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.
-
get_top_border
()[source]¶ Get the Top border of table.
Column width should be set prior to calling this method.
- Returns
String which will be printed as the Top border of the table.
- Return type
str
-
get_header_separator
()[source]¶ Get the Header separator of table.
Column width should be set prior to calling this method.
- Returns
String which will be printed as Header separator of the table.
- Return type
str
-
get_row_separator
()[source]¶ Get the Row separator of table.
Column width should be set prior to calling this method.
- Returns
String which will be printed as Row separator of the table.
- Return type
str
-
get_bottom_border
()[source]¶ Get the Bottom border of table.
Column width should be set prior to calling this method.
- Returns
String which will be printed as Bottom border of the table.
- Return type
str
-
get_table_width
()[source]¶ Get the width of the table as number of characters.
Column width should be set prior to calling this method.
- Returns
Width of the table as number of characters.
- Return type
int
-
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 be displayed as soon as possible. Any existing rows in the table shall also be returned. It is required that atleast one of title, width or existing rows set prior to 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
-
get_string
(recalculate_width=True)[source]¶ Get the table as a String.
- Parameters
recalculate_width (bool, optional) – If width for each column should be recalculated(default True). Note that width is always calculated if it wasn’t set explicitly when this method is called for the first time , regardless of the value of recalculate_width.
- Returns
Table as a string.
- Return type
str