tablate.Tablate#

class tablate.Tablate(container_border=None, container_padding=None, container_width=None, html_default_colors=None, frame_divider=None, background=None, background_padding=None, html_px_multiplier=None, html_container_styles=None, column_styles=None, text_styles=None, html_frame_styles=None, html_column_styles=None, html_text_styles=None)[source]#

Container for text, grid and table frames. Allows cumulative addition of frames to an instance.

Note:

When using the ‘Tablate’ class in an IPython environment, it is important to name each frame (or else rerunning cells will create duplicate entries).

Parameters:
  • container_border (Optional[Literal['blank', 'thin', 'thick', 'double']]) – outer border style for the Tablate container instance. [default: ‘thick’]

  • container_padding (Optional[int]) – outer padding (in Unicode character width) for the Tablate container instance. [default: 1]

  • container_width (Optional[int]) – outer width (in Unicode character width) for the Tablate container instance. [default: 120 or terminal width]

  • html_default_colors (Optional[bool]) – apply the default HTML color scheme to the Tablate instance. [default: True]

  • frame_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – default dividing line between frames within the Tablate container instance. [default: ‘thick’]

  • background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – default background for frames withing the Tablate container instance. [default: None]

  • background_padding (Optional[int]) – additional padding applied to cells with a background color set (ASCII only). [default: 1]

  • html_px_multiplier (Optional[int]) – Multiplier applied to HTML px properties. [default: 1]

  • html_container_styles (Optional[HtmlContainerStylesInput]) – style dictionary for HTML specific styles for Tablate container instance. [default: None]

  • column_styles (Optional[ColumnStylesInput]) – style dictionary for default base column styles. [default: {“column_divider”: ‘thin’, “padding”: 1, “background_padding”: 1}]

  • text_styles (Optional[TextStylesInput]) – style dictionary for default base text styles. [default: { “text_style”: ‘normal’, “text_align”: ‘left’, “text_color”: None }]

  • html_frame_styles (Optional[HtmlFrameStylesInput]) –

    style dictionary for default HTML specific frame styles. [default: {

    “html_frame_divider_style”: “thick”, “html_frame_divider_weight: 1, “html_frame_divider_color”: ‘black’, “html_multiline”: True (False for Table frames), “html_max_lines”: None, “html_background”: None }]

  • html_column_styles (Optional[HtmlColumnStylesInput]) – style dictionary for default HTML specific column styles. [default: { “html_column_divider_style”: ‘thin’, “html_column_divider_weight”: 1, “html_column_divider_color”: ‘black’, “html_padding”: 6px }]

  • html_text_styles (Optional[HtmlTextStylesInput]) – style dictionary for default HTML specific text styles. [default: { “html_text_style”: ‘normal’, “html_text_align”: ‘left’, “html_text_color”: None, “html_text_size”: 16 }]

add_text_frame(text, name=None, text_style=None, text_align=None, text_color=None, frame_divider=None, frame_padding=None, background=None, background_padding=None, multiline=None, max_lines=None, html_px_multiplier=None, html_styles=None)[source]#

Add a new text frame (single row, single column) to Tablate container instance.

Parameters:
  • text (Union[str, int, float]) – Text frame content. [required]

  • name (Optional[str]) – Frame name (to prevent duplicate entries). [default: None]

  • text_style (Optional[Literal['normal', 'bold', 'underlined', 'bold_underlined']]) – Text style for text frame. [default: ‘normal’]

  • text_align (Optional[Literal['left', 'center', 'right']]) – Text alignment for text frame. [default: ‘left’]

  • text_color (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Text color for text frame. [default: ‘None’]

  • frame_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the frame. [default: ‘thick’]

  • frame_padding (Optional[int]) – Text padding within text frame. [default: 1]

  • background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for the text frame. [default: None]

  • background_padding (Optional[int]) – Additional padding applied to text if background color set (ASCII only). [default: 1]

  • multiline (Optional[bool]) – Whether the text may wrap to multiple lines. [default: True]

  • max_lines (Optional[int]) – Maximum number of lines to which the text may wrap. [default: None]

  • html_px_multiplier (Optional[int]) – Multiplier applied to HTML px properties. [default: 1]

  • html_styles (Optional[HtmlTextFrameStylesInput]) – HTML specific styles for text frame. [default: { “html_frame_styles”: {“html_frame_divider_style”: “thick”, “html_frame_divider_weight: 1, “html_frame_divider_color”: ‘black’, “html_multiline”: True (False for Table frames), “html_max_lines”: None, “html_background”: None }, “html_text_styles”: {“html_text_style”: ‘normal’, “html_text_align”: ‘left’, “html_text_color”: None, “html_text_size”: 16 } }]

Return type:

None

add_grid_frame(columns, name=None, frame_divider=None, background=None, background_padding=None, multiline=None, max_lines=None, column_divider=None, column_padding=None, text_style=None, text_align=None, text_color=None, html_px_multiplier=None, html_styles=None)[source]#

Add a new grid frame (single row of multiple columns) to Tablate instance.

Parameters:
  • columns (List[Union[str, GridColumnInput]]) – Either a list of contents (str, int, float) for columns or a list of column dicts containing contents and styles for columns. [required]

  • name (Optional[str]) – Frame name (to prevent duplicate entries). [default: None]

  • frame_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the frame. [default: ‘thick’]

  • background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for the grig frame. [default: None]

  • background_padding (Optional[int]) – Additional padding applied to text if background color set (ASCII only). [default: 1]

  • multiline (Optional[bool]) – Whether the text may wrap to multiple lines. [default: True]

  • max_lines (Optional[int]) – Maximum number of lines to which the text may wrap. [default: None]

  • column_divider (Optional[Literal['blank', 'thin', 'thick', 'double']]) – Dividing line between columns. [default: ‘thin’]

  • column_padding (Optional[int]) – Text padding within each column. [default: 1]

  • text_style (Optional[Literal['normal', 'bold', 'underlined', 'bold_underlined']]) – Text style for grid frame. [default: ‘normal’]

  • text_align (Optional[Literal['left', 'center', 'right']]) – Text alignment for grid frame. [default: ‘left’]

  • text_color (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Text color for grid frame. [default: ‘None’]

  • html_px_multiplier (Optional[int]) – Multiplier applied to HTML px properties. [default: 1]

  • html_styles (Optional[HtmlStylesInput]) – HTML specific styles for grid frame. [default: { “html_frame_styles”: {“html_frame_divider_style”: “thick”, “html_frame_divider_weight: 1, “html_frame_divider_color”: ‘black’, “html_multiline”: True (False for Table frames), “html_max_lines”: None, “html_background”: None }, “html_column_styles”: {“html_column_divider_style”: “thin”, “html_column_divider_weight”: 1, “html_column_divider_color”: “black”, “html_padding”: “6px”} “html_text_styles”: {“html_text_style”: ‘normal’, “html_text_align”: ‘left’, “html_text_color”: None, “html_text_size”: 16 } }]

Return type:

None

add_table_frame(columns, rows, name=None, frame_divider=None, background=None, background_padding=None, multiline=None, max_lines=None, multiline_header=None, max_lines_header=None, hide_header=None, column_divider=None, column_padding=None, header_base_divider=None, row_line_divider=None, odd_row_background=None, even_row_background=None, text_style=None, text_align=None, text_color=None, header_styles=None, html_px_multiplier=None, html_styles=None, html_header_styles=None)[source]#

Add a new table frame (multiple rows of multiple columns) to Tablate instance.

Parameters:
  • columns (List[TableColumnInput]) – List of column dicts containing a required column key and optional (str, int, float) to display, as well as column specific styling options. [required]

  • rows (List[Dict[str, Union[str, int, float]]]) – List of row dicts containing the (str, int, float) values for each column key. [required]

  • name (Optional[str]) – Frame name (to prevent duplicate entries). [default: None]

  • frame_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the frame. [default: ‘thick’]

  • background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for the table frame. [default: None]

  • background_padding (Optional[int]) – Additional padding applied to text if background color set (ASCII only). [default: 1]

  • multiline (Optional[bool]) – Whether the text may wrap to multiple lines. [default: False]

  • max_lines (Optional[int]) – Maximum number of lines to which the text may wrap. [default: None]

  • multiline_header (Optional[bool]) – Whether the header text may wrap to multiple lines. [default: False]

  • max_lines_header (Optional[int]) – Maximum number of lines to which the header text may wrap. [default: None]

  • hide_header (Optional[bool]) – Whether to not render the header. [default: False]

  • column_divider (Optional[Literal['blank', 'thin', 'thick', 'double']]) – Dividing line between columns. [default: ‘thin’]

  • column_padding (Optional[int]) – Text padding within each column. [default: 1]

  • header_base_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the table header frame. [default: ‘thick’]

  • row_line_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line between each row of the table frame. [default: ‘thin’]

  • odd_row_background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for odd rows of table frame. [default: None]

  • even_row_background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for even rows of table frame. [default: None]

  • text_style (Optional[Literal['normal', 'bold', 'underlined', 'bold_underlined']]) – Text style for table frame. [default: ‘normal’]

  • text_align (Optional[Literal['left', 'center', 'right']]) – Text alignment for table frame. [default: ‘left’]

  • text_color (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Text color for grid frame. [default: None]

  • header_styles (Optional[BaseStylesInput]) – Specific styles to apply to the table header frame. [default: None (inherits table styles)]

  • html_px_multiplier (Optional[int]) – Multiplier applied to HTML px properties. [default: 1]

  • html_styles (Optional[HtmlTableFrameStylesInput]) – HTML specific styles for table frame. [default: { “html_frame_styles”: {“html_frame_divider_style”: “thick”, “html_frame_divider_weight: 1, “html_frame_divider_color”: ‘black’, “html_multiline”: True (False for Table frames), “html_max_lines”: None, “html_background”: None }, “html_column_styles”: {“html_column_divider_style”: “thin”, “html_column_divider_weight”: 1, “html_column_divider_color”: “black”, “html_padding”: “6px”}, “html_text_styles”: {“html_text_style”: ‘normal’, “html_text_align”: ‘left’, “html_text_color”: None, “html_text_size”: 16 }, “html_row_styles”: {“html_row_line_divider_weight”: 1, “html_row_line_divider_style”: “thin”, “html_row_line_divider_color”: None, “html_odds_background”: None, “html_evens_background”: None} }] (Tablate constructor option html_default_colors applies styles to table headers and even rows, separately from, but overwritten by, this html_styles dict)

  • html_header_styles (Optional[HtmlStylesInput]) – HTML specific styles for table header frame. [default: None (inherits table styles)]

Return type:

None

from_dict(dict_object, name=None, capitalize_keys=True, frame_divider=None, multiline=None, max_lines=None, background=None, background_padding=None, multiline_header=None, max_lines_header=None, hide_header=None, column_divider=None, column_padding=None, header_base_divider=None, row_line_divider=None, odd_row_background=None, even_row_background=None, text_style=None, text_align=None, text_color=None, header_styles=None, html_px_multiplier=None, html_styles=None, html_header_styles=None)[source]#
Creates a table frame from a dict in three possible formats:
  • {“one”: [1, 2, 3], “two”: [4, 5, 6]}

  • {‘one’: {0: 1, 1: 2, 2: 3}, ‘two’: {0: 1, 1: 2, 2: 3}}

  • {‘one’: {(1, ‘red’): 1, (1, ‘blue’): 2, (2, ‘green’): 3}, ‘two’: {(1, ‘red’): 1, (1, ‘blue’): 2, (2, ‘green’): 3}

Pandas returns the latter two dict types from its .to_dict() method.

Parameters:
  • dict_object (dict) – Input dict. [required]

  • name (Optional[str]) – Frame name (to prevent duplicate entries). [default: None]

  • capitalize_keys (bool) – Whether to capitalize Pandas column keys. [default: True]

  • frame_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the frame. [default: ‘thick’]

  • background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for the table frame. [default: None]

  • background_padding (Optional[int]) – Additional padding applied to text if background color set (ASCII only). [default: 1]

  • multiline (Optional[bool]) – Whether the text may wrap to multiple lines. [default: False]

  • max_lines (Optional[int]) – Maximum number of lines to which the text may wrap. [default: None]

  • multiline_header (Optional[bool]) – Whether the header text may wrap to multiple lines. [default: False]

  • max_lines_header (Optional[int]) – Maximum number of lines to which the header text may wrap. [default: None]

  • hide_header (Optional[bool]) – Whether to not render the header. [default: False]

  • column_divider (Optional[Literal['blank', 'thin', 'thick', 'double']]) – Dividing line between columns. [default: ‘thin’]

  • column_padding (Optional[int]) – Text padding within each column. [default: 1]

  • header_base_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line at the bottom of the table header frame. [default: ‘thick’]

  • row_line_divider (Optional[Literal['none', 'blank', 'thin', 'thick', 'double']]) – Dividing line between each row of the table frame. [default: ‘thin’]

  • odd_row_background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for odd rows of table frame. [default: None]

  • even_row_background (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Background color for even rows of table frame. [default: None]

  • text_style (Optional[Literal['normal', 'bold', 'underlined', 'bold_underlined']]) – Text style for table frame. [default: ‘normal’]

  • text_align (Optional[Literal['left', 'center', 'right']]) – Text alignment for table frame. [default: ‘left’]

  • text_color (Optional[Literal['white', 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'dark_red', 'dark_green', 'dark_yellow', 'dark_blue', 'dark_magenta', 'dark_cyan', 'dark_grey', 'black']]) – Text color for grid frame. [default: None]

  • header_styles (Optional[BaseStylesInput]) – Specific styles to apply to the table header frame. [default: None (inherits table styles)]

  • html_px_multiplier (Optional[int]) – Multiplier applied to HTML px properties. [default: 1]

  • html_styles (Optional[HtmlTableFrameStylesInput]) – HTML specific styles for table frame. [default: { “html_frame_styles”: {“html_frame_divider_style”: “thick”, “html_frame_divider_weight: 1, “html_frame_divider_color”: ‘black’, “html_multiline”: True (False for Table frames), “html_max_lines”: None, “html_background”: None }, “html_column_styles”: {“html_column_divider_style”: “thin”, “html_column_divider_weight”: 1, “html_column_divider_color”: “black”, “html_padding”: “6px”}, “html_text_styles”: {“html_text_style”: ‘normal’, “html_text_align”: ‘left’, “html_text_color”: None, “html_text_size”: 16 }, “html_row_styles”: {“html_row_line_divider_weight”: 1, “html_row_line_divider_style”: “thin”, “html_row_line_divider_color”: None, “html_odds_background”: None, “html_evens_background”: None} }] (Tablate constructor option html_default_colors applies styles to table headers and even rows, separately from, but overwritten by, this html_styles dict)

  • html_header_styles (Optional[HtmlStylesInput]) – HTML specific styles for table header frame. [default: None (inherits table styles)]

apply(function, raise_merge_error=True)#

Iterates over frame items and calls the passed function for on each iteration. Optionally, if the functions returns a valid frame options dict or global options dicts, these will be set to the internal store. If there is an error, trying to set the return objects, the .apply() method will break and reset the internal stores.

Note

As a rule of thumb, the return objects should have the same form as the third (frame options) and forth (global options) arguments passed to the .apply() method.

Parameters:
  • function (Callable[[str, Literal['text', 'grid', 'table'], dict, dict], Tuple[dict, dict]]) – A function called for each frame element. The function accepts four arguments: the frame type, the frame name, the frame options and the global options. The function may return a tuple containing the frame options and the global options. Tablate will attempt to apply the return values, if set, to its internal frame store and the global options store.

  • raise_merge_error (bool) – If set to True, the apply function will raise an

apply_style(selector, css, sub_selector=None)#

Applies custom CSS styling to the Tablate container instance. CSS stylings are scoped to the specific Tablate container instance.

Parameters:
  • selector (str) – The HTML classnames within the Tablate container instance.

  • css (str) – The CSS style to be set (for instance: background-color: #FFFFFF).

  • sub_selector (Optional[str]) – An additional selector appended immediately after the main selector block.

get_frame(selector, apply_globals=False)#

Gets a frame from the Tablate container instance and returns it as a TablateItem. :type selector: Union[int, str] :param selector: The index or name of the frame to select. :type apply_globals: bool :param apply_globals: Whether to apply the global defaults and container styles to the returned TablateItem.

Returns:

insert_frame(insert_index, new_frame, new_name=None)#

Insert a frame into a Tablate container instance at a specified index. :type insert_index: int :param insert_index: The index at which the new frame should be inserted. :type new_frame: TablateBase :param new_frame: The Tablate container instance to insert. :type new_name: Optional[str] :param new_name: (Optional) The name to apply to the inserted frame.

list_frames()#

List the frames in the Tablate container instance with some details about each frame.

move_frame(from_selector, to_index)#

Move a frame within a Tablate container instance to a new index. :type from_selector: Union[int, str] :param from_selector: The index or name of the frame to move. :type to_index: int :param to_index: The index to which the selected frame should be moved.

print()#

Prints an ASCII rendering of the Tablate container instance.

Return type:

None

remove_frame(selector)#

Deletes a frame. :type selector: Union[int, str] :param selector: The index or name of the frame to delete.

rename_frame(selector, new_name)#

Renames a frame. :type selector: Union[int, str] :param selector: The index or name of the frame to rename. :type new_name: str :param new_name: The new name to apply to the frame.

replace_frame(selector, new_frame, new_name=None)#

Replaces a frame with another frame. :type selector: Union[int, str] :param selector: The index or name of the frame to replace. :type new_frame: TablateBase :param new_frame: A Tablate Container instance to replace the frame with (if more than one frame in the Tablate container instance, only the first frame will be used). :type new_name: Optional[str] :param new_name: (Optional) The new name of the frame.

to_html()#

Generates an HTML rendering of the Tablate container instance.

Returns: An HTML rendering of the Tablate container instance.

Return type:

str

to_string()#

Generates an ASCII rendering of the Tablate container instance.

Returns: An ASCII rendering of the Tablate container instance.

Return type:

str