Skip to content

Commit

Permalink
DOC: Add simple_table function
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Dec 14, 2023
1 parent 8ce36b6 commit 69b3937
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
31 changes: 31 additions & 0 deletions plotnine/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 14 additions & 10 deletions plotnine/doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion requirements/doc.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 69b3937

Please sign in to comment.