|
@@ -5,7 +5,6 @@ from pathlib import Path
|
|
|
from typing import TYPE_CHECKING, Dict, Union, ByteString
|
|
from typing import TYPE_CHECKING, Dict, Union, ByteString
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
-
|
|
|
|
|
from redmail.utils import is_bytes
|
|
from redmail.utils import is_bytes
|
|
|
from redmail.utils import import_from_string
|
|
from redmail.utils import import_from_string
|
|
|
|
|
|
|
@@ -16,7 +15,7 @@ from jinja2.environment import Template, Environment
|
|
|
from markupsafe import Markup
|
|
from markupsafe import Markup
|
|
|
|
|
|
|
|
# We try to import matplotlib and PIL but if fails, they will be None
|
|
# We try to import matplotlib and PIL but if fails, they will be None
|
|
|
-from .utils import PIL, plt, pd
|
|
|
|
|
|
|
+from .utils import PIL, plt, pd, css_inline
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
if TYPE_CHECKING:
|
|
|
# For type hinting
|
|
# For type hinting
|
|
@@ -62,7 +61,18 @@ class Body:
|
|
|
if pd is None:
|
|
if pd is None:
|
|
|
raise ImportError("Missing package 'pandas'. Prettifying tables requires Pandas.")
|
|
raise ImportError("Missing package 'pandas'. Prettifying tables requires Pandas.")
|
|
|
|
|
|
|
|
|
|
+ from pandas.io.formats.style import Styler
|
|
|
|
|
+
|
|
|
extra = {} if extra is None else extra
|
|
extra = {} if extra is None else extra
|
|
|
|
|
+
|
|
|
|
|
+ # Allow for pandas styler object, convert to inline CSS for email client rendering
|
|
|
|
|
+ # https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.html
|
|
|
|
|
+ if isinstance(tbl, Styler):
|
|
|
|
|
+ if css_inline is None:
|
|
|
|
|
+ raise ImportError("Missing package 'css_inline'. Prettifying tables with Pandas styler requires css_inline.")
|
|
|
|
|
+ inliner = css_inline.CSSInliner()
|
|
|
|
|
+ return inliner.inline(tbl.to_html())
|
|
|
|
|
+
|
|
|
df = pd.DataFrame(tbl)
|
|
df = pd.DataFrame(tbl)
|
|
|
|
|
|
|
|
tbl_html = self.table_template.render({"df": df, **extra})
|
|
tbl_html = self.table_template.render({"df": df, **extra})
|