Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion great_tables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Main gt imports ----

from .gt import GT
from . import vals, loc, style
from . import loc, style, types, vals
from ._styles import FromColumn as from_column
from ._helpers import (
letters,
Expand Down Expand Up @@ -41,6 +41,7 @@
"random_id",
"from_column",
"vals",
"types",
"loc",
"style",
)
Expand Down
22 changes: 5 additions & 17 deletions great_tables/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
from typing import TYPE_CHECKING, Literal

from typing_extensions import TypeAlias
from typing import TYPE_CHECKING

from .types import DebugDumpOptions, RenderTargets, WebDrivers
from ._helpers import random_id
from ._scss import compile_scss
from ._utils import _try_import
Expand Down Expand Up @@ -44,11 +43,11 @@ def _create_temp_file_server(fname: Path) -> HTTPServer:

def _infer_render_target(
ipy: InteractiveShell | None | type = MISSING,
) -> Literal["auto", "notebook", "browser"]:
) -> RenderTargets:
# adapted from py-htmltools
# Note that `ipy` arguments are possible return values of IPython.get_ipython()
# They are manually passed in from unit tests to validate this function.
target: Literal["auto", "notebook", "browser"]
target: RenderTargets
try:
import IPython # pyright: ignore[reportUnknownVariableType]
from IPython.terminal.interactiveshell import TerminalInteractiveShell
Expand All @@ -73,7 +72,7 @@ def _infer_render_target(

def show(
self: GTSelf,
target: Literal["auto", "notebook", "browser"] = "auto",
target: RenderTargets = "auto",
):
"""Display the table in a notebook or a web browser.

Expand Down Expand Up @@ -340,17 +339,6 @@ def as_latex(self: GT, use_longtable: bool = False, tbl_pos: str | None = None)
return latex_table


# Create a list of all selenium webdrivers
WebDrivers: TypeAlias = Literal[
"chrome",
"firefox",
"safari",
"edge",
]

DebugDumpOptions: TypeAlias = Literal["zoom", "width_resize", "final_resize"]


def save(
self: GT,
file: Path | str,
Expand Down
41 changes: 3 additions & 38 deletions great_tables/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from decimal import Decimal
from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Literal, TypedDict, TypeVar, cast
from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypedDict, TypeVar, cast

import babel
import faicons
from babel.dates import format_date, format_datetime, format_time
from typing_extensions import TypeAlias


from .types import DateStyle, MissingVals, PlotType, TimeStyle
from ._gt_data import FormatFn, FormatFns, FormatInfo, GTData
from ._helpers import px
from ._locale import (
Expand Down Expand Up @@ -41,42 +42,6 @@
from ._types import GTSelf

T = TypeVar("T")
DateStyle: TypeAlias = Literal[
"iso",
"wday_month_day_year",
"wd_m_day_year",
"wday_day_month_year",
"month_day_year",
"m_day_year",
"day_m_year",
"day_month_year",
"day_month",
"day_m",
"year",
"month",
"day",
"year.mn.day",
"y.mn.day",
"year_week",
"year_quarter",
]
TimeStyle: TypeAlias = Literal[
"iso",
"iso-short",
"h_m_s_p",
"h_m_p",
"h_p",
]
PlotType: TypeAlias = Literal[
"line",
"bar",
]
MissingVals: TypeAlias = Literal[
"marker",
"gap",
"zero",
"remove",
]


def fmt(
Expand Down
7 changes: 3 additions & 4 deletions great_tables/_gt_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from collections.abc import Sequence
from dataclasses import dataclass, field, replace
from enum import Enum, auto
from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, overload
from typing import TYPE_CHECKING, Any, Callable, TypeVar, overload

from typing_extensions import Self, TypeAlias, Union

from .types import ColumnAlignment

# TODO: move this class somewhere else (even gt_data could work)
from ._styles import CellStyle
from ._tbl_data import (
Expand Down Expand Up @@ -195,9 +197,6 @@ def from_empty(cls, body: DataFrameLike):


# Boxhead ----
ColumnAlignment: TypeAlias = Literal["left", "center", "right", "justify"]


class ColInfoTypeEnum(Enum):
default = auto()
stub = auto()
Expand Down
23 changes: 3 additions & 20 deletions great_tables/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,13 @@
import re
import string
from dataclasses import dataclass
from typing import Any, Callable, Literal
from typing import Any, Callable

from typing_extensions import Self, TypeAlias
from typing_extensions import Self

from .types import FontStackName
from ._text import BaseText, Html, Md, _md_html

FontStackName: TypeAlias = Literal[
"system-ui",
"transitional",
"old-style",
"humanist",
"geometric-humanist",
"classical-humanist",
"neo-grotesque",
"monospace-slab-serif",
"monospace-code",
"industrial",
"rounded-sans",
"slab-serif",
"antique",
"didone",
"handwritten",
]


FONT_STACKS = {
"system-ui": [
Expand Down
13 changes: 7 additions & 6 deletions great_tables/_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import itertools
from dataclasses import dataclass
from functools import singledispatch
from typing import TYPE_CHECKING, Any, Callable, Literal
from typing import TYPE_CHECKING, Any, Callable

from typing_extensions import TypeAlias

from .types import NullMeans, PlacementOptions, RowNameAttrs

# note that types like Spanners are only used in annotations for concretes of the
# resolve generic, but we need to import at runtime, due to singledispatch looking
# up annotations
Expand All @@ -27,7 +29,6 @@

# Misc Types ===========================================================================

PlacementOptions: TypeAlias = Literal["auto", "left", "right"]
RowSelectExpr: TypeAlias = 'list[int] | PlExpr | Callable[["TblData"], bool] | None'

# Locations ============================================================================
Expand Down Expand Up @@ -672,7 +673,7 @@ def resolve_cols_c(
strict: bool = True,
excl_stub: bool = True,
excl_group: bool = True,
null_means: Literal["everything", "nothing"] = "everything",
null_means: NullMeans = "everything",
) -> list[str]:
"""Return a list of column names, selected by expr."""
selected = resolve_cols_i(
Expand All @@ -692,7 +693,7 @@ def resolve_cols_i(
strict: bool = True,
excl_stub: bool = True,
excl_group: bool = True,
null_means: Literal["everything", "nothing"] = "everything",
null_means: NullMeans = "everything",
) -> list[tuple[str, int]]:
"""Return a tuple of (column name, position) pairs, selected by expr."""

Expand Down Expand Up @@ -759,8 +760,8 @@ def resolve_cols_i(
def resolve_rows_i(
data: GTData | list[str],
expr: RowSelectExpr = None,
null_means: Literal["everything", "nothing"] = "everything",
row_name_attr: Literal["rowname", "group_id"] = "rowname",
null_means: NullMeans = "everything",
row_name_attr: RowNameAttrs = "rowname",
) -> list[tuple[str, int]]:
"""Return matching row numbers, based on expr

Expand Down
7 changes: 3 additions & 4 deletions great_tables/_render.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import os
from typing import Literal

from .types import RenderEnvs

IDE_ENV_FLAGS = {
"default": {"make_page": False, "all_important": False},
Expand All @@ -13,9 +14,7 @@
}


def infer_render_env() -> (
Literal["quarto", "databricks", "ipython_terminal", "vscode", "positron", "default"]
):
def infer_render_env() -> RenderEnvs:
# Check if we are rendering in the Quarto environment
if "QUARTO_BIN_PATH" in os.environ:
return "quarto"
Expand Down
5 changes: 3 additions & 2 deletions great_tables/_substitution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING, Any

from .types import HTMLContext
from ._formats import fmt
from ._gt_data import FormatterSkipElement
from ._helpers import html
Expand All @@ -13,7 +14,7 @@
from ._types import GTSelf


def _convert_missing(context: Literal["html"], el: str):
def _convert_missing(context: HTMLContext, el: str):
"""Convert el to a context specific representation."""

# TODO: how is context passed? Could use a literal string (e.g. "html") for now?
Expand Down
11 changes: 2 additions & 9 deletions great_tables/_utils_selenium.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from __future__ import annotations

from types import TracebackType
from typing import Literal
from typing_extensions import TypeAlias

from selenium import webdriver

# Create a list of all selenium webdrivers
WebDrivers: TypeAlias = Literal[
"chrome",
"firefox",
"safari",
"edge",
]
from .types import WebDrivers


class _BaseWebDriver:
Expand Down
82 changes: 82 additions & 0 deletions great_tables/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from typing import Literal

from typing_extensions import TypeAlias


ColumnAlignment: TypeAlias = Literal["left", "center", "right", "justify"]

RenderTargets: TypeAlias = Literal["auto", "notebook", "browser"]

RenderEnvs: TypeAlias = Literal[
"quarto", "databricks", "ipython_terminal", "vscode", "positron", "default"
]

WebDrivers: TypeAlias = Literal["chrome", "firefox", "safari", "edge"]

HTMLContext: TypeAlias = Literal["html"]

DebugDumpOptions: TypeAlias = Literal["zoom", "width_resize", "final_resize"]

PlacementOptions: TypeAlias = Literal["auto", "left", "right"]

NullMeans = Literal["everything", "nothing"]

RowNameAttrs = Literal["rowname", "group_id"]

FontStackName: TypeAlias = Literal[
"system-ui",
"transitional",
"old-style",
"humanist",
"geometric-humanist",
"classical-humanist",
"neo-grotesque",
"monospace-slab-serif",
"monospace-code",
"industrial",
"rounded-sans",
"slab-serif",
"antique",
"didone",
"handwritten",
]

DateStyle: TypeAlias = Literal[
"iso",
"wday_month_day_year",
"wd_m_day_year",
"wday_day_month_year",
"month_day_year",
"m_day_year",
"day_m_year",
"day_month_year",
"day_month",
"day_m",
"year",
"month",
"day",
"year.mn.day",
"y.mn.day",
"year_week",
"year_quarter",
]

TimeStyle: TypeAlias = Literal[
"iso",
"iso-short",
"h_m_s_p",
"h_m_p",
"h_p",
]

PlotType: TypeAlias = Literal[
"line",
"bar",
]

MissingVals: TypeAlias = Literal[
"marker",
"gap",
"zero",
"remove",
]