Skip to content

Commit 6ade772

Browse files
attack68yeshsurya
authored andcommitted
ENH: add decimal and thousands args to Styler.format() (pandas-dev#40596)
1 parent b309683 commit 6ade772

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

pandas/io/formats/style_render.py

+56-19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Sequence,
1313
Tuple,
1414
Union,
15+
cast,
1516
)
1617
from uuid import uuid4
1718

@@ -20,10 +21,7 @@
2021
from pandas._config import get_option
2122

2223
from pandas._libs import lib
23-
from pandas._typing import (
24-
FrameOrSeriesUnion,
25-
TypedDict,
26-
)
24+
from pandas._typing import FrameOrSeriesUnion
2725
from pandas.compat._optional import import_optional_dependency
2826

2927
from pandas.core.dtypes.generic import ABCSeries
@@ -47,14 +45,10 @@
4745
CSSPair = Tuple[str, Union[str, int, float]]
4846
CSSList = List[CSSPair]
4947
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
5852

5953

6054
class StylerRenderer:
@@ -108,10 +102,42 @@ def __init__(
108102
tuple[int, int], Callable[[Any], str]
109103
] = defaultdict(lambda: partial(_default_formatter, precision=def_precision))
110104

111-
def _render_html(self, **kwargs) -> str:
105+
def render(self, **kwargs) -> str:
112106
"""
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
115141
"""
116142
self._compute()
117143
# TODO: namespace all the pandas keys
@@ -486,8 +512,13 @@ def format(
486512
formatter = {col: formatter for col in columns}
487513

488514
for col in columns:
515+
try:
516+
format_func = formatter[col]
517+
except KeyError:
518+
format_func = None
519+
489520
format_func = _maybe_wrap_formatter(
490-
formatter.get(col),
521+
format_func,
491522
na_rep=na_rep,
492523
precision=precision,
493524
decimal=decimal,
@@ -584,9 +615,15 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles:
584615
{'selector': 'th', 'props': 'a:v;'}]
585616
"""
586617
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
590627
]
591628

592629

0 commit comments

Comments
 (0)