display#
This module contains functions to convert a DataFrame into a Rich Table.
The to_rich_table
function takes a DataFrame and converts it into a Rich Table.
It allows specifying styles, justification, and text wrapping for each column.
It also supports truncating the DataFrame with a maximum number of lines and
The print_rich_table
is this module’s main function.
It takes a DataFrame and prints it as a Rich Table in the console.
- wip.datatools.display.add_columns(table: Table, df: DataFrame, justify: dict, style: dict, no_wrap: bool = True, max_columns: int = 15)[source]#
Add columns from a DataFrame to a Rich Table with specified formatting.
This function takes a Rich Table and a DataFrame and adds the column names of the DataFrame to the table. Formatting options like justification, style, and wrapping can be specified for each column. Optionally, you can include the DataFrame index as a column.
- Parameters
table (
Table
) – The Rich Table to add the columns to.df (
DataFrame
) – Apandas.DataFrame
containing the data to add to the table.justify (
dict
) – A dictionary mapping column names to their justification (‘left’, ‘right’, etc.).style (
dict
) – A dictionary mapping column names to their style (color, font weight, etc.).no_wrap (
bool
, defaultTrue
) – Whether text wrapping should be disabled.max_columns (
int
, default15
) – Maximum number of columns to display.
- wip.datatools.display.create_row_data(df: DataFrame, max_lines: int, max_columns: int) list [source]#
Create row data for the table.
The row data is created by converting the
pandas.DataFrame
rows values into a string data type and then converting these values into a list of lists.If the number of rows in the DataFrame is greater than the
max_lines
parameter, then the rows are truncated and an ellipsis is added in the middle.
- wip.datatools.display.print_rich_table(df: DataFrame, index: bool = False, **kwargs)[source]#
Print a DataFrame as a formatted Rich Table in the console.
This function takes a DataFrame and prints it as a Rich Table in the console. It supports various formatting options including styles, justification, and text wrapping for columns, and allows truncation of the DataFrame with a maximum number of lines. The DataFrame index can also be included as a column in the table.
- Parameters
df (
DataFrame
) – Thepandas.DataFrame
to print.index (
bool
, defaultFalse
) – Whether to include the DataFrame index as a separate column.style (
str | dict | None
) – Styling information for the table’s columns. It can be a single style applied to all columns, a dictionary mapping column names to styles, or None for default style.no_wrap (
bool
, defaultTrue
) – Whether to disable text wrapping in the table.justify (
JustifyMethod | dict
, default'right'
) – Justification for the table’s columns. Can be a single justification for all columns or a dictionary mapping column names to justifications.max_lines (
int | None
, default10
) – The maximum number of lines to display from the DataFrame. If None, all lines are displayed.max_columns (
int | None
, default15
) – The maximum number of columns to display from the DataFrame. If None, up to 15 columns are displayed.
Examples
>>> df = pd.DataFrame( ... { ... 'ID': [1, 2, 3], ... 'Name': ['Julius', 'Albert', 'Newton'], ... 'Date of Birth': ['1904-04-22', '1879-03-14', '1643-01-04'] ... } ... ) >>> print_rich_table(df) ┏━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ ID ┃ Name ┃ Date of Birth ┃ ┡━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ 1 │ Julius │ 1904-04-22 │ │ 2 │ Albert │ 1879-03-14 │ │ 3 │ Newton │ 1643-01-04 │ └────┴────────┴───────────────┘
>>> print_rich_table(df, index=True) ┏━━━━━━━┳━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ index ┃ ID ┃ Name ┃ Date of Birth ┃ ┡━━━━━━━╇━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ 0 │ 1 │ Julius │ 1904-04-22 │ │ 1 │ 2 │ Albert │ 1879-03-14 │ │ 2 │ 3 │ Newton │ 1643-01-04 │ └───────┴────┴────────┴───────────────┘
>>> print_rich_table(df, index=True, max_lines=2) ┏━━━━━━━┳━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ index ┃ ID ┃ Name ┃ Date of Birth ┃ ┡━━━━━━━╇━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ 0 │ 1 │ Julius │ 1904-04-22 │ │ ... │ ... │ ... │ ... │ │ 2 │ 3 │ Newton │ 1643-01-04 │ └───────┴─────┴────────┴───────────────┘
>>> print_rich_table(df, max_columns=3) ┏━━━━┳━━━━━┳━━━━━━━━┓ ┃ ID ┃ ... ┃ Name9 ┃ ┡━━━━╇━━━━━╇━━━━━━━━┩ │ 1 │ ... │ Julius │ │ 2 │ ... │ Albert │ │ 3 │ ... │ Newton │ └────┴─────┴────────┘
See also
to_rich_table
Convert a DataFrame into a Rich Table with formatting options.
Notes
Setting
max_lines
argument to None might result in a huge table that will be challenging to render and display in the console. It is not recommended to set this argument to None for large DataFrames.
- wip.datatools.display.process_row(row: list, max_columns: int) list [source]#
Process a row of data to be added to the table.
This function takes a row of data from a DataFrame and converts it into a list of strings. If the number of columns in the DataFrame is greater than the
max_columns
parameter, then the columns are truncated and an ellipsis is added in the middle.
- wip.datatools.display.to_rich_table(df: DataFrame, index: bool = False, **kwargs: Any)[source]#
Convert a DataFrame into a Rich Table with formatting options.
This function takes a DataFrame and converts it into a Rich Table. It allows specifying styles, justification, and text wrapping for each column. It also supports truncating the DataFrame with a maximum number of lines and adding the DataFrame index as a separate column in the table.
- Parameters
df (
DataFrame
) – Thepandas.DataFrame
to convert to a rich table.index (
bool
, optional) – Whether to include the DataFrame index as a separate column. Defaults to False.style (
str | dict | None
) – Styling information for the table’s columns. It can be a single style applied to all columns, a dictionary mapping column names to styles, or None for default style.no_wrap (
bool
, defaultTrue
) – Whether to disable text wrapping in the table.justify (
JustifyMethod | dict
, default'right'
) – Justification for the table’s columns. Can be a single justification for all columns or a dictionary mapping column names to justifications.max_lines (
int | None
, default10
) – The maximum number of lines to display from the DataFrame. If None, all lines are displayed.max_columns (
int | None
, default15
) – The maximum number of columns to display from the DataFrame. If None, up to 15 columns are displayed.kwargs (Any) –
- Returns
The resulting Rich Table with the DataFrame data formatted as specified.
- Return type
Table
Notes
Setting
max_lines
argument to None might result in a huge table that will be challenging to render and display in the console. It is not recommended to set this argument to None for large DataFrames.
- wip.datatools.display.validate_and_set_default_justification(df: pd.DataFrame, justify: JustifyMethod | dict) dict [source]#
Validate and set default justification for the table columns.
- Parameters
df (
DataFrame
) – The DataFrame for which the justifications are being set.justify (
JustifyMethod | dict
) – The justification settings provided by the user.
- Returns
A dictionary with column justifications.
- Return type
- Raises
ValueError – If the
justify
parameter is not in the correct format.
- wip.datatools.display.validate_and_set_default_styles(df: pd.DataFrame, style: str | dict | None) dict [source]#
Validate and set default styles for the table columns.
- Parameters
df (
DataFrame
) – The DataFrame for which the styles are being set.style (
str | dict | None
) – The style settings provided by the user.
- Returns
A dictionary with column styles.
- Return type
- Raises
ValueError – If the
style
parameter is not in the correct format.