py_research.tables module#

Utilities for creating pretty result tables.

class TableStyle(dfs=None, cols=None, rows=None, name=None, format=None, css=None, header_css=None, hide=None, include=None, require=None)[source]#

Bases: object

Define a pretty table column format.

Parameters:
dfs: str | list[str] | None = None#

Dataframe names to apply this style to. If None, apply to all dataframes.

cols: str | list[str] | None = None#

Column name(s) to apply this style to. If None, apply to all columns. Index levels can be styled via their names as if they were columns.

rows: Series | None = None#

Rows to apply this style to. If None, apply to all rows.

name: str | None = None#

Name of this style

format: str | Callable[[Any], str] | None = None#

Format string or callback function to use for formatting data values

css: dict[str, str] | Callable[[Any], str] | None = None#

Custom CSS styles to apply to matched cells. Supplied as a mapping from CSS property to value.

header_css: dict[str, str] | None = None#

Custom CSS styles applied to the headers of matched cells. Supplied as a mapping from CSS property to value.

hide: Literal['headers', 'rows', 'cols', 'cells'] | None = None#

Whether to hide headers, rows, columns or cells matched by this style

include: bool | None = None#

Whether to show rows if matched by this selection.

require: bool | None = None#

Whether to only show rows if matched by this selection.

class TableColors(row_even='#f2f2f2', row_odd='#fff', header_even='#677a96', header_odd='#677a96')[source]#

Bases: object

Define colors for a pretty table.

Parameters:
  • row_even (str) –

  • row_odd (str) –

  • header_even (str) –

  • header_odd (str) –

row_even: str = '#f2f2f2'#

Background color for even rows.

row_odd: str = '#fff'#

Background color for odd rows.

header_even: str = '#677a96'#

Background color for even headers.

header_odd: str = '#677a96'#

Background color for odd headers.

class ResultTable(df, styles=<factory>, labels=<factory>, widths=<factory>, title=None, max_row_cutoff=100, font_size=1.0, default_style=<factory>, column_flatten_format=None, show_index=False, table_colors=<factory>, table_css=<factory>, index_label=None)[source]#

Bases: object

Define and render a pretty result table with custom formatting and highlights.

Parameters:
df: dataclasses.InitVar[DataFrame]#

Dataframe to render as pretty table.

styles: list[TableStyle]#

Styles to apply to the table.

labels: dict[str, str] | Callable[[str], str]#

Labels to use for the table headers.

If dict, the keys are column names and the values are the labels. If callable, the function is called with the column name and should return the label

widths: dict[str, float | str]#

Widths to use for the table columns.

If a float, the width is relative to the sum of all widths. If a string, the width is set to the CSS string value.

title: str | None = None#

Title of the table.

max_row_cutoff: int = 100#

Maximum number of rows to render.

font_size: float = 1.0#

Default font size to use for the table.

default_style: TableStyle#

Default style to apply to the table.

column_flatten_format: str | None = None#

Format string to use for flatteting multi-index column labels. Leave as None to keep a multi-level column header. Format string must take two positional arguments, e.g. “{0}_{1}”.

show_index: bool = False#

Whether to show the index as the first columns of the styled table.

table_colors: TableColors#

Colors to use for this table.

table_css: dict[str, dict[str, str]]#

Additional styles to apply to the table. Dictionary keys must be CSS selectors and values must be dictionaries of CSS properties.

index_label: str | None = None#

Label to use for multi-index.

html_description(full_doc=True)[source]#

Return HTML description for table filters and highlights.

Parameters:

full_doc (bool) – Whether to wrap the description in a full HTML document.

Returns:

HTML code for the description.

Return type:

str

to_styled_df()[source]#

Render table to styled pandas dataframe.

Return type:

Styler

to_html(write_to=None, full_html=True)[source]#

Return HTML representation and optionally write it to a file.

Parameters:
  • styled – Styled dataframe to render.

  • write_to (Path | str | TextIO | None) – File to write the HTML code to.

  • full_html (bool) – Whether to wrap the table in a full HTML document.

Returns:

HTML code for the table.

Return type:

str

to_html(styled, full_doc=True)[source]#

Return HTML representation of a pretty table.

Parameters:
  • styled (Styler) – Styled dataframe to render.

  • full_doc (bool) – Whether to wrap the table in a full HTML document.

Returns:

HTML code for the table.

Return type:

str

html_to_pdf(doc, file)[source]#

Render and save HTML doc as PDF document.

Parameters:
  • doc (str) – HTML document to render.

  • file (Path) – File to save the rendered PDF document to.

html_to_image(doc, file)[source]#

Render and save HTML doc as PNG image.

Parameters:
  • doc (str) – HTML document to render.

  • file (Path) – File to save the rendered image to.