|
12 | 12 | Sequence, |
13 | 13 | Tuple, |
14 | 14 | Union, |
| 15 | + cast, |
15 | 16 | ) |
16 | 17 | from uuid import uuid4 |
17 | 18 |
|
|
20 | 21 | from pandas._config import get_option |
21 | 22 |
|
22 | 23 | from pandas._libs import lib |
23 | | -from pandas._typing import ( |
24 | | - FrameOrSeriesUnion, |
25 | | - TypedDict, |
26 | | -) |
| 24 | +from pandas._typing import FrameOrSeriesUnion |
27 | 25 | from pandas.compat._optional import import_optional_dependency |
28 | 26 |
|
29 | 27 | from pandas.core.dtypes.generic import ABCSeries |
|
47 | 45 | CSSPair = Tuple[str, Union[str, int, float]] |
48 | 46 | CSSList = List[CSSPair] |
49 | 47 | CSSProperties = Union[str, CSSList] |
50 | | - |
51 | | - |
52 | | -class CSSDict(TypedDict): |
53 | | - selector: str |
54 | | - props: CSSProperties |
55 | | - |
56 | | - |
57 | | -CSSStyles = List[CSSDict] |
| 48 | +CSSStyles = List[Dict[str, CSSProperties]] # = List[CSSDict] |
| 49 | +# class CSSDict(TypedDict): # available when TypedDict is valid in pandas |
| 50 | +# selector: str |
| 51 | +# props: CSSProperties |
58 | 52 |
|
59 | 53 |
|
60 | 54 | class StylerRenderer: |
@@ -108,10 +102,42 @@ def __init__( |
108 | 102 | tuple[int, int], Callable[[Any], str] |
109 | 103 | ] = defaultdict(lambda: partial(_default_formatter, precision=def_precision)) |
110 | 104 |
|
111 | | - def _render_html(self, **kwargs) -> str: |
| 105 | + def render(self, **kwargs) -> str: |
112 | 106 | """ |
113 | | - Renders the ``Styler`` including all applied styles to HTML. |
114 | | - Generates a dict with necessary kwargs passed to jinja2 template. |
| 107 | + Render the ``Styler`` including all applied styles to HTML. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + **kwargs |
| 112 | + Any additional keyword arguments are passed |
| 113 | + through to ``self.template.render``. |
| 114 | + This is useful when you need to provide |
| 115 | + additional variables for a custom template. |
| 116 | +
|
| 117 | + Returns |
| 118 | + ------- |
| 119 | + rendered : str |
| 120 | + The rendered HTML. |
| 121 | +
|
| 122 | + Notes |
| 123 | + ----- |
| 124 | + Styler objects have defined the ``_repr_html_`` method |
| 125 | + which automatically calls ``self.render()`` when it's the |
| 126 | + last item in a Notebook cell. When calling ``Styler.render()`` |
| 127 | + directly, wrap the result in ``IPython.display.HTML`` to view |
| 128 | + the rendered HTML in the notebook. |
| 129 | +
|
| 130 | + Pandas uses the following keys in render. Arguments passed |
| 131 | + in ``**kwargs`` take precedence, so think carefully if you want |
| 132 | + to override them: |
| 133 | +
|
| 134 | + * head |
| 135 | + * cellstyle |
| 136 | + * body |
| 137 | + * uuid |
| 138 | + * table_styles |
| 139 | + * caption |
| 140 | + * table_attributes |
115 | 141 | """ |
116 | 142 | self._compute() |
117 | 143 | # TODO: namespace all the pandas keys |
@@ -486,8 +512,13 @@ def format( |
486 | 512 | formatter = {col: formatter for col in columns} |
487 | 513 |
|
488 | 514 | for col in columns: |
| 515 | + try: |
| 516 | + format_func = formatter[col] |
| 517 | + except KeyError: |
| 518 | + format_func = None |
| 519 | + |
489 | 520 | format_func = _maybe_wrap_formatter( |
490 | | - formatter.get(col), |
| 521 | + format_func, |
491 | 522 | na_rep=na_rep, |
492 | 523 | precision=precision, |
493 | 524 | decimal=decimal, |
@@ -584,9 +615,15 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles: |
584 | 615 | {'selector': 'th', 'props': 'a:v;'}] |
585 | 616 | """ |
586 | 617 | return [ |
587 | | - {"selector": selector, "props": css_dict["props"]} |
588 | | - for css_dict in styles |
589 | | - for selector in css_dict["selector"].split(",") |
| 618 | + item |
| 619 | + for sublist in [ |
| 620 | + [ # this is a CSSDict when TypedDict is available to avoid cast. |
| 621 | + {"selector": x, "props": style["props"]} |
| 622 | + for x in cast(str, style["selector"]).split(",") |
| 623 | + ] |
| 624 | + for style in styles |
| 625 | + ] |
| 626 | + for item in sublist |
590 | 627 | ] |
591 | 628 |
|
592 | 629 |
|
|
0 commit comments