---
title: GT
output: html
jupyter: python3
---

```{python}
import great_tables as gt
from great_tables import exibble

```

Let's use the exibble dataset for the next few examples, we'll learn how to make simple gt tables with the `GT()` class. The most basic thing to do is to just use `GT()` with the dataset as the input.

```{python}
gt.GT(exibble)
```

This dataset has the row and group columns. The former contains unique values that are ideal for labeling rows, and this often happens in what is called the 'stub' (a reserved area that serves to label rows). With the `GT()` class, we can immediately place the contents of the row column into the stub column. To do this, we use the `rowname_col` argument with the name of the column to use.

```{python}
# | eval: false
gt.GT(exibble, rowname_col="row")
```

This sets up a table with a stub, the row labels are placed within the stub column, and a vertical dividing line has been placed on the right-hand side.

The group column can be used to divide the rows into discrete groups. Within that column, we see repetitions of the values `'grp_a'` and `'grp_b'`. These serve both as ID values and the initial label for the groups. With the `groupname_col` argument in `GT()`, we can set up the row groups immediately upon creation of the table.

```{python}
# | eval: false
gt.GT(exibble, rowname_col="row", groupname_col="group")
```

If you'd rather perform the set up of row groups later (i.e., not in the `GT()` call), this is possible with use of the `tab_row_group()` method (and `row_group_order()` can help with the arrangement of row groups).

One more thing to consider with row groups is their layout. By default, row group labels reside in separate rows the appear above the group. However, we can use the `row_group_as_column=True` option to put the row group labels within a secondary column within the table stub.

```{python}
# | eval: false
gt.GT(exibble, rowname_col="row", groupname_col="group", row_group_as_column=True)
```

This could be done later if need be, and using `tab_options(row_group.as_column=True)` would be the way to do it outside of the `GT()` call.

Some datasets have rownames built in; `mtcars` has the car model names as the rownames. To use those rownames as row labels in the stub, the `rownames_to_stub=TRUE` option will prove to be useful.

```{python}
# | eval: false
gt.GT(head(mtcars, 10), rownames_to_stub=True)
```

By default, values in the body of a gt table (and their column labels) are automatically aligned. The alignment is governed by the types of values in a column. If you'd like to disable this form of auto-alignment, the `auto_align=False` option can be taken.

```{python}
# | eval: false
gt.GT(exibble, rowname_col="row", auto_align=False)
```

What you'll get from that is center-alignment of all table body values and all column labels. Note that row labels in the the stub are still left-aligned; and `auto_align` has no effect on alignment within the table stub.

However which way you generate the initial gt table object, you can use it with a huge variety of methods in the package to further customize the presentation. Formatting body cells is commonly done with the family of formatting methods (e.g., `fmt_number()`, `fmt_date()`, etc.). The package supports formatting with internationalization ('i18n' features) and so locale-aware methods come with a locale argument. To avoid having to use that argument repeatedly, the `GT()` class has its own `locale` argument. Setting a locale in that will make it available globally. Here's an example of how that works in practice when setting `locale='fr'` in `GT()` and using formatting methods:

```{python}
# | eval: false
gt.GT(
    exibble, rowname_col="row", groupname_col="group", locale="fr"
).fmt_number().fmt_date(columns="date", date_style="yMEd").fmt_datetime(
    columns="datetime", format="EEEE, MMMM d, y", locale="en"
)
```
