### Install Great Tables Python Package
Source: https://posit-dev.github.io/great-tables/articles/intro.html/blog/introduction_great_tables
Instructions on how to install the `great_tables` package using pip, the Python package installer.
```bash
pip install great_tables
```
--------------------------------
### Install Project Dependencies
Source: https://posit-dev.github.io/great-tables/articles/intro.html/get-started/contributing
Instructions for installing the 'great-tables' project and its development dependencies using pip. This includes editable mode installation, development-specific packages, and additional dependencies for image generation.
```Shell
pip install -e .
```
```Shell
pip install .[dev]
```
```Shell
pip install .[all]
```
--------------------------------
### Install Great Tables Python Package
Source: https://posit-dev.github.io/great-tables/articles/intro.html/get-started/index
This snippet demonstrates the straightforward installation process for the `great_tables` package using pip, the standard Python package installer. This command is the initial step required to begin utilizing the library's functionalities.
```Python
pip install great_tables
```
--------------------------------
### Installing Great Tables Python Package via pip
Source: https://posit-dev.github.io/great-tables/articles/intro.html/articles/intro
This command shows how to install the `great_tables` package from PyPI using the `pip` package installer. It's the standard method for adding the library to your Python environment, making it available for use in your projects.
```Bash
$ pip install great_tables
```
--------------------------------
### Initialize Great Tables with Pandas and Polars DataFrames
Source: https://posit-dev.github.io/great-tables/articles/intro.html/get-started/basic-styling
This snippet imports necessary libraries and prepares `GT` objects from the `airquality` dataset, demonstrating setup for both Pandas and Polars DataFrames for subsequent styling examples.
```Python
import polars as pl
from great_tables import GT, from_column, style, loc
from great_tables.data import airquality
air_head = airquality.head()
gt_air = GT(air_head)
gt_pl_air = GT(pl.from_pandas(air_head))
```
--------------------------------
### Install Great Tables with Extra Dependencies
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.save
This command installs the `great_tables` library along with its extra dependencies, including `selenium` and `PIL`, which are required for functionalities like saving tables as images or PDFs. Ensure you have pip installed before running this command.
```Bash
pip install great_tables[extra]
```
--------------------------------
### Initialize a Base Great Table
Source: https://posit-dev.github.io/great-tables/articles/intro.html/blog/introduction-0.12.0/index
This Python code sets up a foundational Great Table object from the `exibble` dataset. It demonstrates basic table creation, column hiding, and adding a title, subtitle, and source note, serving as the starting point for font styling examples.
```Python
from great_tables import GT, exibble, style, loc
gt_tbl = (
GT(exibble.head(), rowname_col="row", groupname_col="group")
.cols_hide(columns=["char", "fctr", "date", "time"])
.tab_header(
title="A small piece of the exibble dataset",
subtitle="Displaying the first five rows (of eight)",
)
.tab_source_note(
source_note="This dataset is included in Great Tables."
)
)
gt_tbl
```
--------------------------------
### Create a Basic Great Tables GT Object for Examples
Source: https://posit-dev.github.io/great-tables/articles/intro.html/get-started/table-theme-premade
This Python code snippet initializes a `GT` object using a subset of the `exibble` dataset. It demonstrates basic table construction, including setting a header, subtitle, stubhead, number and currency formatting, and adding a source note. This object serves as a foundation for subsequent examples of `opt_*` methods.
```python
from great_tables import GT, exibble, md
lil_exibble = exibble.head(5)[["num", "char", "row", "group"]]
gt_ex = (
GT(lil_exibble, rowname_col="row", groupname_col="group")
.tab_header(
title=md("Data listing from exibble"),
subtitle=md("This is a **Great Tables** dataset."),
)
.tab_stubhead(label="row")
.fmt_number(columns="num")
.fmt_currency(columns="currency")
.tab_source_note(source_note="This is only a portion of the dataset.")
.opt_vertical_padding(scale=0.5)
)
gt_ex
```
--------------------------------
### Move Multiple Columns to Start of Table with Great Tables
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.cols_move_to_start
This example illustrates how to move multiple columns, 'year' and 'population', to the beginning of a Great Tables table using the `cols_move_to_start()` method. It builds upon the previous example's `countrypops_mini` dataset and demonstrates passing a list of column names.
```Python
GT(countrypops_mini).cols_move_to_start(columns=["year", "population"])
```
--------------------------------
### Create a Basic Great Tables Object for Theming Examples
Source: https://posit-dev.github.io/great-tables/articles/intro.html/get-started/table-theme-options
This Python snippet initializes a `GT` object using the `exibble` dataset, configuring row and group names. It then adds a header, subtitle, stubhead, and source note to the table, creating a foundational object to be used in subsequent styling and theming examples.
```python
from great_tables import GT, exibble
gt_ex = (
GT(exibble.head(5), rowname_col="row", groupname_col="group")
.tab_header("THE HEADING", "(a subtitle)")
.tab_stubhead("THE STUBHEAD")
.tab_source_note("THE SOURCE NOTE")
)
gt_ex
```
--------------------------------
### Organize Table Columns with Spanners and Movement in Great Tables
Source: https://posit-dev.github.io/great-tables/articles/intro.html/blog/introduction_great_tables
This Python example illustrates how to enhance table readability using the `airquality` dataset and the Great Tables library. It demonstrates adding multiple column spanners with `tab_spanner` to logically group related columns and reordering columns to the start of the table using `cols_move_to_start` for improved presentation.
```Python
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year=1973)
(
GT(airquality_mini)
.tab_header(
title="New York Air Quality Measurements",
subtitle="Daily measurements in New York City (May 1-10, 1973)",
)
.cols_label(
Ozone=html("Ozone,
ppbV"),
Solar_R=html("Solar R.,
cal/m2"),
Wind=html("Wind,
mph"),
Temp=html("Temp,
°F"),
)
.tab_spanner(label="Date", columns=["Year", "Month", "Day"])
.tab_spanner(label="Measurement", columns=["Ozone", "Solar.R", "Wind", "Temp"])
.cols_move_to_start(columns=["Year", "Month", "Day"])
)
```
--------------------------------
### Format Air Quality Data Table with Spanners and HTML Labels
Source: https://posit-dev.github.io/great-tables/articles/intro.html/examples/index
This Python code showcases `great_tables` for presenting air quality measurements. It adds a header, subtitle, defines column spanners for 'Time' and 'Measurement' categories, moves time-related columns to the start, and applies custom HTML labels to measurement columns for better readability.
```python
from great_tables import GT, html
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year = 1973)
(
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"]
)
.tab_spanner(
label="Measurement",
columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
.cols_move_to_start(columns=["Year", "Month", "Day"])
.cols_label(
Ozone = html("Ozone,
ppbV"),
Solar_R = html("Solar R.,
cal/m2"),
Wind = html("Wind,
mph"),
Temp = html("Temp,
°F")
)
)
```
--------------------------------
### Output of fmt_number Python example
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_number
Shows the expected output when formatting numbers using `vals.fmt_number` with the given parameters in the example.
```text
['0.33', '777,000.00']
```
--------------------------------
### Format Ontario Municipalities Data with Great Tables
Source: https://posit-dev.github.io/great-tables/articles/intro.html/examples/index
This example shows how to load and process municipal data, create markdown-formatted URLs for websites and map locations, and then display the data as a `great_tables` table. It utilizes `fmt_markdown` for links, `fmt_number` for numerical columns, and `cols_label` for custom column headers with HTML.
```Python
from great_tables import GT, html
from great_tables.data import towny
towny_mini = (
towny[["name", "website", "density_2021", "land_area_km2", "latitude", "longitude"]]
.sort_values("density_2021", ascending=False)
.head(10)
)
towny_mini["url_name"] = [""] + towny_mini["name"] + ["]"] + ["("] + towny_mini["website"] + [")"]
towny_mini["location"] = (
["[map](http://maps.google.com/?ie=UTF8&hq=&ll="]
+ towny_mini["latitude"].astype(str)
+ [","]
+ towny_mini["longitude"].astype(str)
+ [")"]
)
(
GT(
towny_mini[["url_name", "location", "land_area_km2", "density_2021"]],
rowname_col="url_name",
)
.tab_header(
title="The Municipalities of Ontario",
subtitle="The top 10 highest population density in 2021",
)
.tab_stubhead(label="Municipality")
.fmt_markdown(columns=["url_name", "location"])
.fmt_number(columns=["land_area_km2", "density_2021"], decimals=1)
.cols_label(
land_area_km2=html("land area,
km2"),
density_2021=html("density,
people/km2"),
)
)
```
--------------------------------
### Python Example: Formatting Bytes with vals.fmt_bytes
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_bytes
This example demonstrates how to use `vals.fmt_bytes` from the `great_tables` library to convert a list of byte values into human-readable strings, specifically showing the output for decimal standard formatting.
```python
from great_tables import vals
vals.fmt_bytes([123.45, 3615844.256], standard="decimal")
# Expected output: ['123 B', '3.6 MB']
```
--------------------------------
### Python Example Output: Formatted Currency Values
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_currency
The expected output from the `great_tables.vals.fmt_currency` function when applied to the example input `[1.02, 3.46]` with `decimals=3`.
```text
['$1.020', '$3.460']
```
--------------------------------
### API Parameters: vals.fmt_time Function
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_time
Detailed descriptions of each parameter for the `vals.fmt_time` function, including type, purpose, and examples.
```APIDOC
x : X
A list of values to be formatted.
time_style : TimeStyle = 'iso'
The time style to use. By default this is the short name "iso" which corresponds to how times are formatted within ISO 8601 datetime values. There are 5 time styles in total and their short names can be viewed using `info_time_style()`.
pattern : str = '{x}'
A formatting pattern that allows for decoration of the formatted value. The formatted value is represented by the `{x}` (which can be used multiple times, if needed) and all other characters will be interpreted as string literals.
locale : str | None = None
An optional locale identifier that can be used for formatting values according the locale’s rules. Examples include "en" for English (United States) and "fr" for French (France).
```
--------------------------------
### API Time Styles: vals.fmt_time Argument
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_time
A table illustrating the available preset time styles for the `time_style` argument in `vals.fmt_time`, showing their short names, example outputs for `14:35:00`, and notes.
```APIDOC
| | Time Style | Output | Notes |
| --- | --- | --- | --- |
| 1 | "iso" | "14:35:00" | ISO 8601, 24h |
| 2 | "iso-short" | "14:35" | ISO 8601, 24h |
| 3 | "h_m_s_p" | "2:35:00 PM" | 12h |
| 4 | "h_m_p" | "2:35 PM" | 12h |
| 5 | "h_p" | "2 PM" | 12h |
```
--------------------------------
### Displaying Great Tables with GT.show
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.show
This Python example illustrates how to effectively use the `GT.show()` method to render `great_tables` objects. It demonstrates displaying both the beginning and end portions of the `exibble` dataset, showcasing the method's direct application.
```Python
from great_tables import GT, exibble
GT(exibble.head(2)).show()
GT(exibble.tail(2)).show()
```
--------------------------------
### Python Example: Formatting Time with fmt_time
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.fmt_time
Demonstrates how to use the `fmt_time()` method with the `exibble` dataset. This example filters the dataset to include only 'date' and 'time' columns, then formats the 'time' column using the 'h_m_s_p' time style.
```python
from great_tables import GT, exibble
exibble_mini = exibble[["date", "time"]]
(
GT(exibble_mini)
.fmt_time(columns="time", time_style="h_m_s_p")
)
```
--------------------------------
### Python Example: Applying Row Striping to a GT Table
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.opt_row_striping
This Python example demonstrates how to construct a `great_tables` GT object from the `exibble` dataset, apply various formatting options like header, number, and currency formatting, and then enable row striping using the `opt_row_striping()` method for improved readability.
```python
from great_tables import GT, exibble, md
(
GT(
exibble[["num", "char", "currency", "row", "group"]],
rowname_col="row",
groupname_col="group"
)
.tab_header(
title=md("Data listing from **exibble**"),
subtitle=md("`exibble` is a **Great Tables** dataset.")
)
.fmt_number(columns="num")
.fmt_currency(columns="currency")
.tab_source_note(source_note="This is only a subset of the dataset.")
.opt_row_striping()
)
```
--------------------------------
### Python Example: Convert Markdown to HTML with vals.fmt_markdown
Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_markdown
Illustrates how to use `vals.fmt_markdown` from `great_tables` to convert multiple Markdown strings into their corresponding HTML representations. The example defines two Markdown text blocks and shows the function call along with the expected HTML output.
```python
from great_tables import vals
text_1 = """
### This is Markdown.
Markdown’s syntax is comprised entirely of
punctuation characters, which punctuation
characters have been carefully chosen so as
to look like what they mean... assuming
you’ve ever used email.
"""
text_2 = """
Info on Markdown syntax can be found
[here](https://daringfireball.net/projects/markdown/).
"""
vals.fmt_markdown([text_1, text_2])
```
```text
['
Markdown’s syntax is comprised entirely of\npunctuation characters, which punctuation\ncharacters have been carefully chosen so as\nto look like what they mean... assuming\nyou’ve ever used email.',\n'Info on Markdown syntax can be found\nhere.'] ``` -------------------------------- ### Python Example: Basic Usage of vals.fmt_roman Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/vals.fmt_roman Demonstrates how to import and use the `vals.fmt_roman` function to convert a list of integers into Roman numerals. ```python from great_tables import vals vals.fmt_roman([3, 5]) ``` -------------------------------- ### Example HTML Output from great_tables Source: https://posit-dev.github.io/great-tables/articles/intro.html/reference/GT.as_raw_html This HTML snippet represents the raw HTML output generated by the `great_tables` library from the styled table object. It includes inline CSS for styling and the table structure, demonstrating the full rendered output. ```HTML