From 69b3937ea249277bad4330063c6c367a00d09aec Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Thu, 14 Dec 2023 22:53:23 +0300 Subject: [PATCH] DOC: Add simple_table function --- plotnine/_utils/__init__.py | 31 +++++++++++++++++++++++++++++++ plotnine/doctools.py | 24 ++++++++++++++---------- requirements/doc.txt | 2 +- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/plotnine/_utils/__init__.py b/plotnine/_utils/__init__.py index 83d79c2ee..0652535e3 100644 --- a/plotnine/_utils/__init__.py +++ b/plotnine/_utils/__init__.py @@ -1175,3 +1175,34 @@ def get_ipython() -> "InteractiveShell | None": except ImportError: return None return get_ipython() + + +def simple_table( + rows: list[tuple[str, str]], headers: tuple[str, str], **kwargs +): + """ + Generate a simple markdown table + + The header is center aligned + The cells is left aligned + """ + # +2 reserves some margins for aligning + column_width = [len(s) + 2 for s in headers] + for row in rows: + for i, cell in enumerate(row): + column_width[i] = max(column_width[i], len(cell)) + + sep = " " + underline = sep.join("-" * w for w in column_width) + formatting_spec = sep.join( + f"{{{i}: <{w}}}" for i, w in enumerate(column_width) + ) + format_row = formatting_spec.format + format_header = formatting_spec.replace("<", "^").format + + _rows = [ + format_header(*headers), # C1 C2 C3 + underline, # --- --- --- + *[format_row(*row) for row in rows], # Ri1 Ri2 Ri3 + ] + return "\n".join(_rows) diff --git a/plotnine/doctools.py b/plotnine/doctools.py index f38290114..6cfa7ed77 100644 --- a/plotnine/doctools.py +++ b/plotnine/doctools.py @@ -14,6 +14,14 @@ T = TypeVar("T") +# Markup that is robust for documentation needs grid tables +# using tabulate and it is only required when +# building documentation. +try: + from tabulate import tabulate as table_function +except ImportError: + from ._utils import simple_table as table_function + # Parameter arguments that are listed first in the geom and # stat class signatures @@ -172,21 +180,17 @@ def dict_to_table(header: tuple[str, str], contents: dict[str, str]) -> str: -------- >>> d = {"alpha": 1, "color": "blue", "fill": None} >>> print(dict_to_table(("Aesthetic", "Default Value"), d)) - ========= ========= - Aesthetic Default Value - ========= ========= - alpha `1` - color `'blue'` - fill `None` - ========= ========= + Aesthetic Default Value + --------- ------------- + alpha `1` + color `'blue'` + fill `None` """ - from tabulate import tabulate - rows = [ (name, value if value == "" else f"`{value!r}`" "{.py}") for name, value in contents.items() ] - return tabulate(rows, headers=header, tablefmt="grid") + return table_function(rows, headers=header, tablefmt="grid") def make_signature( diff --git a/requirements/doc.txt b/requirements/doc.txt index 5f5b18398..5a9dd074c 100644 --- a/requirements/doc.txt +++ b/requirements/doc.txt @@ -1,3 +1,3 @@ # This cannot be in pyproject.toml, pypi does not install git packages plotnine-examples @ git+https://github.com/has2k1/plotnine-examples#egg=plotnine_examples -quartodoc @ git+https://github.com/has2k1/quartodoc@more-extendable-quartodoc#egg=quartodoc +quartodoc