website/examples/gallery/table-2.py

import pandas as pd
from great_tables import GT, loc, style


def warm_editorial_table(table: GT) -> GT:
    return (
        table.opt_table_font(font=["Inter", "system-ui", "sans-serif"])
        .opt_align_table_header(align="left")
        .tab_options(
            table_background_color="#FFF7ED",
            table_font_color="#1C1917",
            table_font_size="14px",
            table_border_top_style="none",
            table_border_bottom_color="#E7DDD2",
            heading_title_font_size="18px",
            heading_subtitle_font_size="13px",
            heading_border_bottom_style="none",
            column_labels_font_weight="600",
            column_labels_border_top_style="none",
            column_labels_border_bottom_color="#1C1917",
            column_labels_border_bottom_width="1px",
            column_labels_vlines_style="none",
            table_body_hlines_color="#E7DDD2",
            table_body_vlines_style="none",
            table_body_border_bottom_color="#E7DDD2",
            row_group_font_weight="600",
            row_group_border_top_color="#E7DDD2",
            row_group_border_bottom_color="#E7DDD2",
            stub_border_style="none",
            summary_row_border_color="#1C1917",
            grand_summary_row_border_color="#1C1917",
            source_notes_font_size="12px",
        )
        .tab_style(
            style=style.text(font="Georgia, 'Times New Roman', serif", weight="600"),
            locations=loc.title(),
        )
        .tab_style(style=style.text(color="#57534E"), locations=loc.subtitle())
        .tab_style(style=style.text(color="#57534E"), locations=loc.source_notes())
    )


def load_data():
    return [
        ("North", "Elm", 23, 21),
        ("South", "Lake", 18, 19),
        ("North", "Oak", 19, 20),
        ("South", "Birch", 22, 21),
        ("North", "Park", 25, 23),
        ("South", "Willow", 17, 18),
        ("North", "Cedar", 21, 22),
        ("South", "Harbor", 26, 24),
        ("North", "Union", 20, 19),
        ("South", "Market", 19, 18),
        ("North", "Pine", 24, 22),
        ("South", "Hill", 21, 20),
        ("North", "Canal", 18, 20),
        ("South", "River", 23, 22),
    ]


def make_before(data):
    frame = pd.DataFrame(data, columns=["Region", "Clinic", "A", "B"])
    return warm_editorial_table(GT(frame[["B", "A", "Region", "Clinic"]])).tab_source_note(
        "See the model table for the adjusted rate."
    )


def make_after(data):
    frame = pd.DataFrame(sorted(data), columns=["Region", "Clinic", "A", "B"])
    return (
        warm_editorial_table(GT(frame, groupname_col="Region", rowname_col="Clinic"))
        .tab_header(
            title="Table 2. Return visits within 30 days",
            subtitle="Estimated probability of a return visit by outpatient clinic",
        )
        .tab_stubhead(label="Clinic")
        .cols_label(A="(1) Unadjusted (%)", B="(2) Adjusted (%)")
        .tab_source_note("Column (2) adjusts for age and prior visits using logistic regression.")
        .tab_source_note("Source: illustrative outpatient follow-up records.")
    )


EXAMPLE = {
    "kind": "table",
    "source": "Illustrative clinic follow-up model for fourteen outpatient sites.",
    "before": {"summary": "Fourteen clinics report the estimated chance of a return visit."},
    "after": {"summary": "Clinic estimates can be compared within each service region."},
    "principles": {
        "arrange-table-for-lookup": {
            "question": "Where can a reader find a named clinic?",
            "book": {
                "quote": "store the key attribute in the first column, for understandability",
                "page": 35,
            },
            "before": {"caption": "The clinic name sits at the far right of each row."},
            "after": {
                "caption": "Clinic names form the first "
                "column and run alphabetically "
                "within each region."
            },
        },
        "document-model-specification": {
            "question": "Which factors does the second model adjust for?",
            "book": {"quote": "explain it in a footnote to the table", "page": 83},
            "before": {"caption": "A and B conceal the difference between model specifications."},
            "after": {
                "caption": "The headings distinguish "
                "the models and the note "
                "names the adjustment "
                "variables and method."
            },
        },
        "table-caption-and-source": {
            "question": "What outcome and population do the figures describe?",
            "book": {"quote": "Based on 12,782 interviews with voters", "page": 179, "line": 7182},
            "before": {"caption": "The untitled table gives bare numbers with no units or source."},
            "after": {
                "caption": "The title names the 30-day "
                "outcome and the columns and "
                "source note identify "
                "percentages and outpatient "
                "records."
            },
        },
        "table-number-and-cross-reference": {
            "question": "How should the adjusted rate be cited?",
            "book": {
                "quote": "indicate relationship between and within tables and series",
                "page": 58,
            },
            "before": {"caption": "The note points vaguely to the model table."},
            "after": {"caption": "Table 2 and column (2) identify the adjusted estimate."},
        },
        "table-topical-groups": {
            "question": "Which clinics share a region?",
            "book": {
                "quote": "the rows are ordered so as to tell an ordered story",
                "page": 179,
                "line": 7178,
            },
            "before": {
                "caption": "North and South alternate and their labels repeat down the table."
            },
            "after": {"caption": "Regional headings divide the clinics into two blocks."},
        },
    },
}