website/examples/gallery/table-4.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 [("Oak", 45, 40, 28), ("Lake", 31, 32, 24), ("Elm", 27, 26, 22)]


def make_before(data):
    rows = [(clinic, base, extra - base) for clinic, _, base, extra in data]
    frame = pd.DataFrame(rows, columns=["Clinic", "Model intercept", "Model coefficient"])
    return (
        warm_editorial_table(GT(frame))
        .tab_header(title="Triage staffing and wait time")
        .tab_source_note(
            "One nurse is coded 0 and two nurses 1. The intercept is 40 at Oak, 32 at Lake and 26 at Elm."
        )
    )


def make_after(data):
    rows = [
        (clinic, observed, base, extra, "▰" * ((base - extra) // 2))
        for clinic, observed, base, extra in data
    ]
    frame = pd.DataFrame(
        rows, columns=["Clinic", "Observed", "One nurse", "Two nurses", "Reduction"]
    )
    return (
        warm_editorial_table(GT(frame))
        .tab_header(
            title="A second nurse saves the most time at Oak",
            subtitle="Mean triage wait in minutes",
        )
        .tab_spanner(label="Predicted wait", columns=["One nurse", "Two nurses"])
        .cols_label(Observed="Observed¹", Reduction="Minutes saved")
        .tab_style(style=style.text(color="#0F766E"), locations=loc.body(columns="Reduction"))
        .tab_source_note("¹ Observed with one nurse. Each block represents two minutes saved.")
    )


EXAMPLE = {
    "kind": "table",
    "source": "Illustrative clinic scheduling model comparing two staffing plans.",
    "before": {"summary": "Model coefficients describe changes from one to two triage nurses."},
    "after": {"summary": "A second triage nurse reduces predicted waits most at Oak."},
    "principles": {
        "contextualize-model-estimates": {
            "question": "How close is the prediction to the observed wait?",
            "book": {
                "quote": "figure that is as close as possible to showing the raw data",
                "page": 348,
            },
            "before": {"caption": "Only model terms appear beside each clinic."},
            "after": {
                "caption": "Observed waits sit beside the one-nurse predictions in the same units."
            },
        },
        "show-interpretable-model-effects": {
            "question": "What wait can patients expect under each plan?",
            "book": {
                "quote": "coefficients on the main effect and interaction terms,",
                "page": 464,
            },
            "before": {
                "caption": "Interpreting the coefficient requires adding it to the intercept."
            },
            "after": {"caption": "One-nurse and two-nurse predictions give the waits directly."},
        },
        "table-heading-hierarchy": {
            "question": "Which columns belong to the same model?",
            "book": {"quote": "umn spanners to group the columns", "page": 86},
            "before": {"caption": "Model repeats in both headings on a single level."},
            "after": {"caption": "Predicted wait spans the two staffing plans."},
        },
        "table-semi-graphic": {
            "question": "Where does the extra nurse save the most time?",
            "book": {"quote": "For sets of highly labeled numbers", "page": 180, "line": 7755},
            "before": {
                "caption": "Readers must compare signed coefficients to spot the largest reduction."
            },
            "after": {"caption": "A row of blocks shows minutes saved on a common scale."},
        },
        "text-table-for-prose": {
            "question": "Where can readers look up the wait with one nurse?",
            "book": {
                "quote": "The conventional sentence is a poor way to show more than two numbers",
                "page": 178,
                "line": 7126,
            },
            "before": {
                "caption": "A sentence strings the three "
                "baseline estimates together below "
                "the table."
            },
            "after": {"caption": "The One nurse column puts each baseline beside its clinic."},
        },
    },
}