Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust,python): reworked comfy-table layout constraints, improving table wrapping/repr #9744

Merged
merged 3 commits into from
Jul 7, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jul 6, 2023

Ref: #9656.

The 7.x comfy-table update has turned into a good opportunity to improve our table layout/constraints. Previously the only column width/wrapping constraint that we applied came from a lower bound based on the column name/header.

This PR significantly improves constraint application by considering not only the column name, but also the dtype string/sep (if not hidden) and all of the actual (displayed|/formatted) column data. It then uses this more detailed per-column info to apply an exact bound for especially narrow columns (eg: where all elements <= 3 chars), and a combined upper/lower bound for all other columns.

During width bucket allocation for each column (by comfy-table during table creation) the presence of exact/upper bounds prevents characters from being over-allocated to columns that don't require them, reducing instances of premature/unnecessary wrapping by better-allocating the character/width budget.

With this in place we can now upgrade to the latest comfy-table, which slightly tweaked lower-bound application...

Example

Simple example showing the impact of more accurate column width determination:

import polars as pl
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
with pl.Config( tbl_hide_column_data_types=True ):
    print( df )

#  before                 after    
# ┌─────┬─────┬─────┐    ┌─────┬────┬───┐
# │ aaa ┆ bb  ┆ c   │    │ aaa ┆ bb ┆ c │
# ╞═════╪═════╪═════╡    ╞═════╪════╪═══╡
# │ 1   ┆ 4   ┆ 7   │ >> │ 1   ┆ 4  ┆ 7 │
# │ 2   ┆ 5   ┆ 8   │    │ 2   ┆ 5  ┆ 8 │
# │ 3   ┆ 6   ┆ 9   │    │ 3   ┆ 6  ┆ 9 │
# └─────┴─────┴─────┘    └─────┴────┴───┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jul 6, 2023
@alexander-beedie alexander-beedie force-pushed the table-repr-constraints branch from 7da05c5 to c925d50 Compare July 6, 2023 12:52
@alexander-beedie alexander-beedie force-pushed the table-repr-constraints branch from c925d50 to 39c22c7 Compare July 6, 2023 13:22
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe good to actually bump the comfytable version in this PR, so that we can see that it works on the latest version.

Other than that, looks fine to me!

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jul 6, 2023

Maybe good to actually bump the comfytable version in this PR, so that we can see that it works on the latest version.

Not a problem; can update it later tonight (was running with 7.0.1 locally to validate :)

@alexander-beedie alexander-beedie force-pushed the table-repr-constraints branch from 39c22c7 to f7fbdbd Compare July 7, 2023 01:26
@alexander-beedie
Copy link
Collaborator Author

Done; updated to the latest (7.0.1)

@stinodego stinodego merged commit 4b0218c into pola-rs:main Jul 7, 2023
@alexander-beedie alexander-beedie deleted the table-repr-constraints branch July 7, 2023 17:01
c-peters pushed a commit to c-peters/polars that referenced this pull request Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants