-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: Styler css Str optional input arguments as well as List[Tuple] #39564
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits Select commit Hold shift + click to select a range
b32dd6f ENH: better input arguments
attack68 faf587e ENH: style.ipynb updates
attack68 8718bcc also to tooltips
attack68 283a9d9 doc updates
attack68 a8aecb6 issue
attack68 2064398 whats new
attack68 351b615 typing
attack68 7ded508 typing
attack68 524f295 Merge remote-tracking branch 'upstream/master' into styler_css_strings
attack68 edaa1a0 typing
attack68 9b56e1d typing
attack68 39a09e5 whats new fix
attack68 d53b77b typing improvemnet
attack68 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -279,8 +279,8 @@ def set_tooltips_class( | |
| ---------- | ||
| name : str, default None | ||
| Name of the tooltip class used in CSS, should conform to HTML standards. | ||
| properties : list-like, default None | ||
| List of (attr, value) tuples; see example. | ||
| properties : list-like or str, default None | ||
| List of (attr, value) tuples or a valid CSS string; see example. | ||
| | ||
| Returns | ||
| ------- | ||
| | @@ -311,6 +311,8 @@ def set_tooltips_class( | |
| ... ('visibility', 'hidden'), | ||
| ... ('position', 'absolute'), | ||
| ... ('z-index', 1)]) | ||
| >>> df.style.set_tooltips_class(name='tt-add', | ||
| ... properties='visibility:hidden; position:absolute; z-index:1;') | ||
| """ | ||
| self._init_tooltips() | ||
| assert self.tooltips is not None # mypy requirement | ||
| | @@ -1172,13 +1174,20 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> Styler: | |
| ... 'props': [('background-color', 'yellow')]}] | ||
| ... ) | ||
| | ||
| Or with CSS strings | ||
| | ||
| >>> df.style.set_table_styles( | ||
| ... [{'selector': 'tr:hover', | ||
| ... 'props': 'background-color: yellow; font-size: 1em;']}] | ||
| ... ) | ||
| | ||
| Adding column styling by name | ||
| | ||
| >>> df.style.set_table_styles({ | ||
| ... 'A': [{'selector': '', | ||
| ... 'props': [('color', 'red')]}], | ||
| ... 'B': [{'selector': 'td', | ||
| ... 'props': [('color', 'blue')]}] | ||
| ... 'props': 'color: blue;']}] | ||
| ... }, overwrite=False) | ||
| | ||
| Adding row styling | ||
| | @@ -1203,6 +1212,14 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> Styler: | |
| for s in styles | ||
| ] | ||
| | ||
| table_styles = [ | ||
| { | ||
| "selector": s["selector"], | ||
| "props": _maybe_convert_css_to_tuples(s["props"]), | ||
| } | ||
| for s in table_styles | ||
| ] | ||
| | ||
| if not overwrite and self.table_styles is not None: | ||
| self.table_styles.extend(table_styles) | ||
| else: | ||
| | @@ -1843,7 +1860,12 @@ def _class_styles(self): | |
| ------- | ||
| styles : List | ||
| """ | ||
| return [{"selector": f".{self.class_name}", "props": self.class_properties}] | ||
| return [ | ||
| { | ||
| "selector": f".{self.class_name}", | ||
| "props": _maybe_convert_css_to_tuples(self.class_properties), | ||
| } | ||
| ] | ||
| | ||
| def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str): | ||
| """ | ||
| | @@ -2025,3 +2047,28 @@ def _maybe_wrap_formatter( | |
| else: | ||
| msg = f"Expected a string, got {na_rep} instead" | ||
| raise TypeError(msg) | ||
| | ||
| | ||
| def _maybe_convert_css_to_tuples( | ||
| style: Union[str, Sequence[Tuple[str, Union[str, int, float]]]] | ||
| ): | ||
| ||
| """ | ||
| Convert css-string to sequence of tuples format if needed. | ||
| 'color:red; border:1px solid black;' -> [('color', 'red'), | ||
| ('border','1px solid red')] | ||
| """ | ||
| if isinstance(style, str): | ||
| s = style.split(";") | ||
| try: | ||
| ret = [ | ||
| (x.split(":")[0].strip(), x.split(":")[1].strip()) | ||
| for x in s | ||
| if x.strip() != "" | ||
| ] | ||
| return ret | ||
| except IndexError: | ||
| raise ValueError( | ||
| "Styles supplied as string must follow CSS rule formats, " | ||
| f"for example 'attr: val;'. {style} was given." | ||
| ) | ||
| return style | ||
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
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.
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.
locate after the other note, L56 (which is missing an issue number; use the PR number for it)