great-tables icon indicating copy to clipboard operation
great-tables copied to clipboard

Iterate over multiple columns for common formatting (GT_0.13.0, Polars_1.7.1)

Open starzar opened this issue 1 year ago • 2 comments

Prework

Question

  1. How to iterate over multiple columns for common formatting on cells eg. converting all float values to int's, showing positive/negative numbers with green/red colors (Polars)?

fmt example for iterating over multiple columns does not work in this case .

  1. Also GT.show() import raises an import error .How to resolve this?

ERROR:

  loc.body(lambda x: x > 0 , columns= df_columns)  # fmt example https://posit-dev.github.io/great-tables/reference/GT.fmt.html#examples
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: LocBody.__init__() got multiple values for argument 'columns'

CODE:


import polars as pl
from great_tables import GT,loc,style

df = pl.DataFrame({
    "A": [10, -5, 20, -15],
    "B": [30.32234, 25.234234, -10.234234, 15.23423],
})

df_columns = df.columns

# Not supported on polars df
df_gt = GT(df)


filtered_df_gt = (
    df_gt
    .tab_style(
        style.fill("green"),
        loc.body(lambda x: x > 0 , columns= df_columns)  # fmt example https://posit-dev.github.io/great-tables/reference/GT.fmt.html#examples
    )
)
# ImportError: cannot import name 'show' from 'great_tables'
# df.show()


html_output = filtered_df_gt.as_raw_html()

html_path = "secondTerminal.html"
with open(html_path, "w") as file:
    file.write(html_output)

starzar avatar Nov 02 '24 14:11 starzar