Path: blob/main/docs/source/src/python/user-guide/misc/styling.py
7890 views
# --8<-- [start:setup]1import warnings23# great-tables throws a Deprecation warning in `as_raw_html` under Python 3.134# https://github.com/posit-dev/great-tables/pull/5635warnings.filterwarnings(6"ignore", "'count' is passed as positional argument", category=DeprecationWarning7)8# --8<-- [end:setup]910# --8<-- [start:dataframe]11import polars as pl12import polars.selectors as cs1314path = "docs/assets/data/iris.csv"1516df = (17pl.scan_csv(path)18.group_by("species")19.agg(cs.starts_with("petal").mean().round(3))20.collect()21)22print(df)23# --8<-- [end:dataframe]2425# --8<-- [start:structure-header]26df.style.tab_header(title="Iris Data", subtitle="Mean measurement values per species")27# --8<-- [end:structure-header]2829# --8<-- [start:structure-header-out]30print(31df.style.tab_header(32title="Iris Data", subtitle="Mean measurement values per species"33).as_raw_html()34)35# --8<-- [end:structure-header-out]363738# --8<-- [start:structure-stub]39df.style.tab_stub(rowname_col="species")40# --8<-- [end:structure-stub]4142# --8<-- [start:structure-stub-out]43print(df.style.tab_stub(rowname_col="species").as_raw_html())44# --8<-- [end:structure-stub-out]4546# --8<-- [start:structure-spanner]47(48df.style.tab_spanner("Petal", cs.starts_with("petal")).cols_label(49petal_length="Length", petal_width="Width"50)51)52# --8<-- [end:structure-spanner]5354# --8<-- [start:structure-spanner-out]55print(56df.style.tab_spanner("Petal", cs.starts_with("petal"))57.cols_label(petal_length="Length", petal_width="Width")58.as_raw_html()59)60# --8<-- [end:structure-spanner-out]6162# --8<-- [start:format-number]63df.style.fmt_number("petal_width", decimals=1)64# --8<-- [end:format-number]656667# --8<-- [start:format-number-out]68print(df.style.fmt_number("petal_width", decimals=1).as_raw_html())69# --8<-- [end:format-number-out]707172# --8<-- [start:style-simple]73from great_tables import loc, style7475df.style.tab_style(76style.fill("yellow"),77loc.body(78rows=pl.col("petal_length") == pl.col("petal_length").max(),79),80)81# --8<-- [end:style-simple]8283# --8<-- [start:style-simple-out]84from great_tables import loc, style8586print(87df.style.tab_style(88style.fill("yellow"),89loc.body(90rows=pl.col("petal_length") == pl.col("petal_length").max(),91),92).as_raw_html()93)94# --8<-- [end:style-simple-out]959697# --8<-- [start:style-bold-column]98from great_tables import loc, style99100df.style.tab_style(101style.text(weight="bold"),102loc.body(columns="species"),103)104# --8<-- [end:style-bold-column]105106# --8<-- [start:style-bold-column-out]107from great_tables import loc, style108109print(110df.style.tab_style(111style.text(weight="bold"),112loc.body(columns="species"),113).as_raw_html()114)115# --8<-- [end:style-bold-column-out]116117# --8<-- [start:full-example]118from great_tables import loc, style119120(121df.style.tab_header(122title="Iris Data", subtitle="Mean measurement values per species"123)124.tab_stub(rowname_col="species")125.cols_label(petal_length="Length", petal_width="Width")126.tab_spanner("Petal", cs.starts_with("petal"))127.fmt_number("petal_width", decimals=2)128.tab_style(129style.fill("yellow"),130loc.body(131rows=pl.col("petal_length") == pl.col("petal_length").max(),132),133)134)135# --8<-- [end:full-example]136137# --8<-- [start:full-example-out]138from great_tables import loc, style139140print(141df.style.tab_header(142title="Iris Data", subtitle="Mean measurement values per species"143)144.tab_stub(rowname_col="species")145.cols_label(petal_length="Length", petal_width="Width")146.tab_spanner("Petal", cs.starts_with("petal"))147.fmt_number("petal_width", decimals=2)148.tab_style(149style.fill("yellow"),150loc.body(151rows=pl.col("petal_length") == pl.col("petal_length").max(),152),153)154.tab_style(155style.text(weight="bold"),156loc.body(columns="species"),157)158.as_raw_html()159)160# --8<-- [end:full-example-out]161162163