diff --git a/src/pyladoc/__init__.py b/src/pyladoc/__init__.py index 228ba04..e594daa 100644 --- a/src/pyladoc/__init__.py +++ b/src/pyladoc/__init__.py @@ -33,6 +33,7 @@ else: Table = DataFrame | Styler except ImportError: Table = DataFrame + Styler = None try: import matplotlib.pyplot as plt @@ -475,8 +476,8 @@ class DocumentWriter(): centered: Whether to center the table in LaTeX output """ assert Table and isinstance(table, Table), 'Table has to be a pandas DataFrame oder DataFrame Styler' - styler = table if isinstance(table, Styler) else getattr(table, 'style', None) - assert isinstance(styler, Styler), 'Jinja2 package is required for rendering tables' + styler = table if Styler and isinstance(table, Styler) else getattr(table, 'style', None) # type: ignore[truthy-function] + assert Styler and isinstance(styler, Styler), 'Jinja2 package is required for rendering pandas tables' # type: ignore[truthy-function] def render_to_html() -> str: caption_prefix, reference = self._add_item(ref_id, ref_type, prefix_pattern) diff --git a/src/pyladoc/latex.py b/src/pyladoc/latex.py index 80c57df..d1d2f59 100644 --- a/src/pyladoc/latex.py +++ b/src/pyladoc/latex.py @@ -1,6 +1,5 @@ from html.parser import HTMLParser -from typing import Generator, Any, Literal, get_args -from pandas.io.formats.style import Styler +from typing import Generator, Any, Literal, get_args, TYPE_CHECKING import re import os import shutil @@ -8,6 +7,13 @@ import subprocess import tempfile from .latex_escaping import unicode_to_latex_dict, latex_escape_dict +if TYPE_CHECKING: + from pandas.io.formats.style import Styler +else: + try: + from pandas.io.formats.style import Styler + except ImportError: + Styler = None LatexEngine = Literal['pdflatex', 'lualatex', 'xelatex', 'tectonic'] @@ -116,6 +122,9 @@ def render_pandas_styler_table(df_style: Styler, caption: str = '', label: str = Returns: The LaTeX code. """ + assert Styler, 'Jinja2 package is required for rendering pandas tables' + assert isinstance(df_style, Styler), 'df_style has to be of type Styler' + def iter_table(table: dict[str, Any]) -> Generator[str, None, None]: yield '\\begin{table}\n' if centering: