-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: simplify Styler._translate
#43686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 30 commits
7903723
6a2793c
d227914
7ca5002
f27f7ed
a1000a7
021bc26
4ba3dff
7fee05d
baa3233
f4ad390
22b03e3
db214d8
24952ae
1d47d0f
566738d
230138a
b9ba9ea
ea2bba1
ca70491
7a1994e
91320f8
92c1941
aad0e16
61b24ed
d4c5715
aa0172f
84a814f
f06b727
131070f
a048122
4931f32
e9716f2
d983464
16946b0
6e71e8c
1aa7c55
d567d55
9d3c177
f0b3a20
f945ac8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,12 +141,19 @@ class Styler(StylerRenderer): | |
Object to define how values are displayed. See ``Styler.format``. If not given | ||
uses ``pandas.options.styler.format.formatter``. | ||
|
||
.. versionadded:: 1.4.0 | ||
css_class_names : dict, optional | ||
A dict of strings used to replace the default CSS class names described below. | ||
|
||
.. versionadded:: 1.4.0 | ||
|
||
Attributes | ||
---------- | ||
env : Jinja2 jinja2.Environment | ||
template : Jinja2 Template | ||
template_html : Jinja2 Template | ||
template_html_table : Jinja2 Template | ||
template_html_style : Jinja2 Template | ||
template_latex : Jinja2 Template | ||
loader : Jinja2 Loader | ||
|
||
See Also | ||
|
@@ -182,6 +189,10 @@ class Styler(StylerRenderer): | |
|
||
* Blank cells include ``blank`` | ||
* Data cells include ``data`` | ||
* Trimmed cells include ``col_trim`` or ``row_trim``. | ||
|
||
Any, or all, or these classes can be renamed by using the ``css`` argument, giving | ||
attack68 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
a value such as `{"row": "MY_ROW_CLASS", "col_trim": "", "row_trim": ""}`. | ||
""" | ||
|
||
def __init__( | ||
|
@@ -199,6 +210,7 @@ def __init__( | |
thousands: str | None = None, | ||
escape: str | None = None, | ||
formatter: ExtFormatter | None = None, | ||
css_class_names: dict[str, str] | None = None, | ||
): | ||
super().__init__( | ||
data=data, | ||
|
@@ -209,6 +221,7 @@ def __init__( | |
caption=caption, | ||
cell_ids=cell_ids, | ||
precision=precision, | ||
css=css_class_names, | ||
) | ||
|
||
# validate ordered args | ||
|
@@ -1159,6 +1172,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |
- caption | ||
|
||
Non-data dependent attributes [copied and exported]: | ||
- css | ||
- hidden index state and hidden columns state (.hide_index_, .hide_columns_) | ||
- table_attributes | ||
- table_styles | ||
|
@@ -1185,6 +1199,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |
"template_html", | ||
] | ||
deep = [ # nested lists or dicts | ||
"css", | ||
"_display_funcs", | ||
"_display_funcs_index", | ||
"_display_funcs_columns", | ||
|
@@ -1948,9 +1963,11 @@ def set_sticky( | |
|
||
def set_table_styles( | ||
self, | ||
table_styles: dict[Any, CSSStyles] | CSSStyles, | ||
table_styles: dict[Any, CSSStyles] | CSSStyles | None = None, | ||
axis: int = 0, | ||
overwrite: bool = True, | ||
css_class_names: dict[str, str] | None = None, | ||
cell_ids: bool | None = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need cell_ids here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed/ |
||
) -> Styler: | ||
""" | ||
Set the table styles included within the ``<style>`` HTML element. | ||
|
@@ -1990,6 +2007,16 @@ def set_table_styles( | |
|
||
.. versionadded:: 1.2.0 | ||
|
||
css_class_names : dict, optional | ||
A dict of strings used to replace the default CSS classes described below. | ||
|
||
.. versionadded:: 1.4.0 | ||
|
||
cell_ids : bool, optional | ||
Whether to include ids on every element of the format *T_{uuid}_rowM_colN*. | ||
|
||
.. versionadded:: 1.4.0 | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
@@ -2001,6 +2028,22 @@ def set_table_styles( | |
Styler.set_table_attributes: Set the table attributes added to the ``<table>`` | ||
HTML element. | ||
|
||
Notes | ||
----- | ||
The default CSS classes dict, whose values can be replaced is as follows: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good. in a followup can you add how this interacts with table_styles, e.g. when to use each one (via docs / examples). |
||
|
||
.. code-block:: python | ||
|
||
css_class_names = {"row_heading": "row_heading", | ||
"col_heading": "col_heading", | ||
"index_name": "index_name", | ||
"col": "col", | ||
"col_trim": "col_trim", | ||
"row_trim": "row_trim", | ||
"level": "level", | ||
"data": "data", | ||
"blank": "blank} | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame(np.random.randn(10, 4), | ||
|
@@ -2036,10 +2079,17 @@ def set_table_styles( | |
See `Table Visualization <../../user_guide/style.ipynb>`_ user guide for | ||
more details. | ||
""" | ||
if isinstance(table_styles, dict): | ||
if css_class_names is not None: | ||
self.css = {**self.css, **css_class_names} | ||
if cell_ids is not None: | ||
self.cell_ids = cell_ids | ||
|
||
if table_styles is None: | ||
return self | ||
elif isinstance(table_styles, dict): | ||
axis = self.data._get_axis_number(axis) | ||
obj = self.data.index if axis == 1 else self.data.columns | ||
idf = ".row" if axis == 1 else ".col" | ||
idf = f".{self.css['row']}" if axis == 1 else f".{self.css['col']}" | ||
|
||
table_styles = [ | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since you are adding this to set_table_styles, do we need to add here (not a big deal but we already have a lot of options)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for preference I prefer to. most dont import
Styler
class but those who do might use it, and there are unlikely to be many more options added here in futures so this may be the last...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am confused over how this interacts with table_styles. can you either remove this enitrely or clarify.