-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: Add ability to style axis names with Styler #48972
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
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits Select commit Hold shift + click to select a range
c85e074 Added initial tests
tehunter 6221501 Fix argument name
tehunter fa019f6 Add names to apply_index API
tehunter 7484e07 Update test_style.py
tehunter f2e091b Add ctx_*_names to deep copy list
tehunter c36bf24 Add ctx_*_names rendering
tehunter 9196591 Fix tests
tehunter f8619a4 Change order of header styles
tehunter ae28dbc Implement axis name styles
tehunter ad0d26c Apply pre-commit
tehunter f1556ee Clean code styling
tehunter 4589d1b Add type annotation to fix mypy error
tehunter 5d84097 Fix type annotation
tehunter 0dbf0b7 Add PEP484 type comment
tehunter 38e5665 Linting
tehunter e10a5db Merge remote-tracking branch 'upstream/main' into styler-axis-names
tehunter ee5ecb1 Rename to `col_name`
tehunter 5c5bc1b Update documentation CSS class list
tehunter 3d98d33 Add whatsnew and versionadded
tehunter 03bcd37 Add tests for basic Index
tehunter 2d36941 Add names example
tehunter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -63,6 +63,10 @@ class CSSDict(TypedDict): | |
| | ||
| CSSStyles = List[CSSDict] | ||
| Subset = Union[slice, Sequence, Index] | ||
| # Maps styles to list of cell element id's that have that style | ||
| CellMapStyles = DefaultDict[Tuple[CSSPair, ...], List[str]] | ||
| # Maps cell positions to CSS styles | ||
| CSSCellMap = DefaultDict[Tuple[int, int], CSSList] | ||
| | ||
| | ||
| class StylerRenderer: | ||
| | @@ -110,6 +114,7 @@ def __init__( | |
| "row_heading": "row_heading", | ||
| "col_heading": "col_heading", | ||
| "index_name": "index_name", | ||
| "col_name": "col_name", | ||
| "col": "col", | ||
| "row": "row", | ||
| "col_trim": "col_trim", | ||
| | @@ -127,9 +132,11 @@ def __init__( | |
| self.hide_columns_: list = [False] * self.columns.nlevels | ||
| self.hidden_rows: Sequence[int] = [] # sequence for specific hidden rows/cols | ||
| self.hidden_columns: Sequence[int] = [] | ||
| self.ctx: DefaultDict[tuple[int, int], CSSList] = defaultdict(list) | ||
| self.ctx_index: DefaultDict[tuple[int, int], CSSList] = defaultdict(list) | ||
| self.ctx_columns: DefaultDict[tuple[int, int], CSSList] = defaultdict(list) | ||
| self.ctx: CSSCellMap = defaultdict(list) | ||
| self.ctx_index: CSSCellMap = defaultdict(list) | ||
| self.ctx_columns: CSSCellMap = defaultdict(list) | ||
| self.ctx_index_names: CSSCellMap = defaultdict(list) | ||
| self.ctx_col_names: CSSCellMap = defaultdict(list) | ||
| self.cell_context: DefaultDict[tuple[int, int], str] = defaultdict(str) | ||
| self._todo: list[tuple[Callable, tuple, dict]] = [] | ||
| self.tooltips: Tooltips | None = None | ||
| | @@ -307,9 +314,9 @@ def _translate( | |
| max_cols, | ||
| ) | ||
| | ||
| self.cellstyle_map_columns: DefaultDict[ | ||
| tuple[CSSPair, ...], list[str] | ||
| ] = defaultdict(list) | ||
| self.cellstyle_map_columns: CellMapStyles = defaultdict(list) | ||
| self.cellstyle_map_col_names: CellMapStyles = defaultdict(list) | ||
| self.cellstyle_map_index_names: CellMapStyles = defaultdict(list) | ||
| head = self._translate_header(sparse_cols, max_cols) | ||
| d.update({"head": head}) | ||
| | ||
| | @@ -319,19 +326,17 @@ def _translate( | |
| ) | ||
| d.update({"index_lengths": idx_lengths}) | ||
| | ||
| self.cellstyle_map: DefaultDict[tuple[CSSPair, ...], list[str]] = defaultdict( | ||
| list | ||
| ) | ||
| self.cellstyle_map_index: DefaultDict[ | ||
| tuple[CSSPair, ...], list[str] | ||
| ] = defaultdict(list) | ||
| self.cellstyle_map: CellMapStyles = defaultdict(list) | ||
| self.cellstyle_map_index: CellMapStyles = defaultdict(list) | ||
| body: list = self._translate_body(idx_lengths, max_rows, max_cols) | ||
| d.update({"body": body}) | ||
| | ||
| ctx_maps = { | ||
| "cellstyle": "cellstyle_map", | ||
| "cellstyle_index": "cellstyle_map_index", | ||
| "cellstyle_columns": "cellstyle_map_columns", | ||
| "cellstyle_index_names": "cellstyle_map_index_names", | ||
| "cellstyle_col_names": "cellstyle_map_col_names", | ||
| } # add the cell_ids styles map to the render dictionary in right format | ||
| for k, attr in ctx_maps.items(): | ||
| map = [ | ||
| | @@ -455,7 +460,7 @@ def _generate_col_header_row(self, iter: tuple, max_cols: int, col_lengths: dict | |
| ( | ||
| f"{self.css['blank']} {self.css['level']}{r}" | ||
| if name is None | ||
| else f"{self.css['index_name']} {self.css['level']}{r}" | ||
| else f"{self.css['col_name']} {self.css['level']}{r}" | ||
| ), | ||
| name | ||
| if (name is not None and not self.hide_column_names) | ||
| | @@ -464,6 +469,16 @@ def _generate_col_header_row(self, iter: tuple, max_cols: int, col_lengths: dict | |
| ) | ||
| ] | ||
| | ||
| if ( | ||
| not all(self.hide_index_) | ||
| and (r, 0) in self.ctx_col_names | ||
| and self.ctx_col_names[r, 0] | ||
| ): | ||
| Contributor 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. is this following the same pattern as above? | ||
| column_name[0]["id"] = f"{self.css['col_name']}_{self.css['level']}{r}" | ||
| self.cellstyle_map_col_names[ | ||
| tuple(self.ctx_col_names[r, 0]) | ||
| ].append(f"{self.css['col_name']}_{self.css['level']}{r}") | ||
| | ||
| column_headers: list = [] | ||
| visible_col_count: int = 0 | ||
| for c, value in enumerate(clabels[r]): | ||
| | @@ -534,15 +549,26 @@ def _generate_index_names_row(self, iter: tuple, max_cols: int, col_lengths: dic | |
| | ||
| clabels = iter | ||
| | ||
| index_names = [ | ||
| _element( | ||
| index_names = [] | ||
| for c, name in enumerate(self.data.index.names): | ||
| index_name_element = _element( | ||
| "th", | ||
| f"{self.css['index_name']} {self.css['level']}{c}", | ||
| self.css["blank_value"] if name is None else name, | ||
| not self.hide_index_[c], | ||
| ) | ||
| for c, name in enumerate(self.data.index.names) | ||
| ] | ||
| | ||
| if ( | ||
| not self.hide_index_[c] | ||
| and (0, c) in self.ctx_index_names | ||
| and self.ctx_index_names[0, c] | ||
| ): | ||
| element_id = f"{self.css['index_name']}_{self.css['level']}{c}" | ||
| index_name_element["id"] = element_id | ||
| self.cellstyle_map_index_names[ | ||
| tuple(self.ctx_index_names[0, c]) | ||
| ].append(element_id) | ||
| index_names.append(index_name_element) | ||
| | ||
| column_blanks: list = [] | ||
| visible_col_count: int = 0 | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this a breaking change? For column level names, it removes the
index_nameclass and adds in thecolumns_namesclass in it's place. If users were purposefully doing styles with.index_nameselectors with the intention of applying those to column level names, it will break that behavior.Uh oh!
There was an error while loading. Please reload this page.
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.
Well it will break the example in the user guide because that styles class
index_name.However, this should be an easy fix:
As long as this break is documented I think including the additional functionality is worth it in long run.