---
title: Cookbook
jupyter: python3
echo: false
---


```{python}
# | label: setup
import polars as pl
from great_tables import GT, html
import gt_extras as gte
```

#### Large Landmasses

```{python}
# | label: large-landmasses
from great_tables.data import islands

def make_simple_table(data):
    gt = (
        GT(data)
        .tab_stub(rowname_col="name")
        .tab_stubhead(label="landmass")
        .tab_header(title="Large Landmasses of the World")
    )

    return gt

islands_first_20 = (
    pl.from_pandas(islands)
    .sort("size", descending=True)
    .head(20)
)

make_simple_table(islands_first_20)
```

{{< include ./_show-last.qmd >}}



#### gt_two_column_layout

```{python}
# | label: two-column-layout
def make_color_box_table(data):
    gt = make_simple_table(data)

    gt = gt.pipe(
        gte.gt_color_box,
        columns="size",
        domain=[0, 17000],
        palette=["lightblue", "darkblue"]
    )

    return gt

islands_first_10 = (
    pl.from_pandas(islands)
    .sort("size", descending=True)
    .slice(0, 10)
)

islands_second_10 = (
    pl.from_pandas(islands)
    .sort("size", descending=True)
    .slice(10, 10)
)

gt1 = make_color_box_table(islands_first_10)
gt2 = make_color_box_table(islands_second_10)

gte.gt_two_column_layout(gt1, gt2, table_header_from=1)
```

{{< include ./_show-last.qmd >}}

```{python}
from great_tables.data import airquality

airquality_mini = airquality.head(10).assign(Year = 1973)

airquality_gt = (
    GT(airquality_mini)
    .tab_header(
        title="New York Air Quality Measurements",
        subtitle="Daily measurements in New York City (May 1-10, 1973)"
    )
    .tab_spanner(
        label="Time",
        columns=["Year", "Month", "Day"]
    )
    .cols_move_to_start(columns=["Year", "Month", "Day"])
    .cols_label(
        Ozone = html("Ozone,<br>ppbV"),
        Solar_R = html("Solar R.,<br>cal/m<sup>2</sup>"),
        Wind = html("Wind,<br>mph"),
        Temp = html("Temp,<br>&deg;F")
    )
)

airquality_gt.tab_spanner(
    label="Measurement",
    columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
```

```{python}
import numpy as np

# Calculate differences for each measurement column
airquality_with_changes = airquality_mini.copy()
airquality_with_changes["Ozone Change"] = airquality_mini["Ozone"].diff()
airquality_with_changes["Solar_R Change"] = airquality_mini["Solar_R"].diff()
airquality_with_changes["Wind Change"] = airquality_mini["Wind"].diff()
airquality_with_changes["Temp Change"] = airquality_mini["Temp"].diff()

# Create the GT table with the difference data
airquality_gt_with_changes = (
    GT(airquality_with_changes)
    .tab_header(
        title="New York Air Quality Measurements",
        subtitle="Daily measurements in New York City (May 1-10, 1973)"
    )
    .tab_spanner(
        label="Time",
        columns=["Year", "Month", "Day"]
    )
    .cols_move_to_start(columns=["Year", "Month", "Day"])
    .cols_label(
        Ozone = html("Ozone,<br>ppbV"),
        Solar_R = html("Solar R.,<br>cal/m<sup>2</sup>"),
        Wind = html("Wind,<br>mph"),
        Temp = html("Temp,<br>&deg;F"),
    )
    .tab_spanner(
        label="Measurement",
        columns=["Ozone", "Solar_R", "Wind", "Temp"]
    )
    .tab_spanner(
        label="Change from Previous",
        columns=["Ozone Change", "Solar_R Change", "Wind Change", "Temp Change"]
    )
    # Apply rank change styling to the difference columns
    .pipe(gte.gt_fa_rank_change, column=["Ozone Change"], show_text=False)
    .pipe(gte.gt_fa_rank_change, column=["Solar_R Change"], show_text=False)
    .pipe(gte.gt_fa_rank_change, column=["Wind Change"], show_text=False)
    .pipe(gte.gt_fa_rank_change, column=["Temp Change"], show_text=False)
)

airquality_gt_with_changes
```
