-
Notifications
You must be signed in to change notification settings - Fork 77
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
Allow for granular section restyling via convenience api #341
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
200fd72
WIP allow for granular section restyling via convenience api
timkpaine f80e1cd
fix lint
timkpaine 5c18797
working on tests
timkpaine 07680cd
Merge branch 'main' into tkp/sectionstyle
machow 10d6cd9
refactor: prepare to use loc classes directly, not strings
machow 39902a0
refactor: clean up last use of .locname
machow 6627b6a
fix: support spanner label targeting
machow 85f0967
feat: add style.css for raw css
machow eced055
tests: add kitchen sink location snapshot test
machow 98db730
tests: update snapshots
machow 17b990a
fix: title style tag needs space before
machow e4b6b6c
fix: resolve_rows_i can always handle None expr
machow 978bce4
feat: implement LocRowGroupLabel styles
machow 2fab150
feat: support LocRowLabel styles
machow 7cccdaf
feat: support LocSourceNotes styles
machow 8f5dada
tests: add source note to styles test, update snapshot
machow b549c7e
tests: correctly target row of data in snapshot
machow 8b4b811
refactor: remove LocStubheadLabel
machow 033ef5b
docs: add targeted styles page to get-started
machow a3bf5df
chore: remove print statement
machow 3ba7818
feat: implement loc.headers
machow e8070ab
Merge branch 'main' into tkp/sectionstyle
machow 90095a4
feat: implement LocFooter styles
machow 9b746e9
tests: update snapshots
machow 805c155
fix: extra space in body html tag
machow 9e623e0
refactor: remove groups attr from Loc classes
machow 1a2e9d4
refactor: remove StyleInfo.locnum attr, .loc always a Loc class
machow e2bea6a
refactor: remove redundant or unimplemented set_style concretes
machow cbc39bd
refactor!: rename or remove locations based on Rich feedback
machow e87ab10
tests: update snapshots
machow bdbe223
refactor!: rename loc.row_group_labels to row_groups
machow 367194d
docs: get targeted styles working again
machow febbd70
docs: flesh out targeted styles a bit
machow 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 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 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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
--- | ||
title: Targeted styles | ||
jupyter: python3 | ||
--- | ||
|
||
In [Styling the Table Body](./basic-styling), we discussed styling table data with `.tab_style()`. | ||
In this article we'll cover how the same method can be used to style many other parts of the table, like the header, specific spanner labels, the footer, and more. | ||
|
||
:::{.callout-warning} | ||
This feature is currently a work in progress, and not yet released. Great Tables must be installed from github in order to try it. | ||
::: | ||
|
||
|
||
## Kitchen sink | ||
|
||
Below is a big example that shows all possible `loc` specifiers being used. | ||
|
||
```{python} | ||
from great_tables import GT, exibble, loc, style | ||
|
||
# https://colorbrewer2.org/#type=qualitative&scheme=Paired&n=12 | ||
brewer_colors = [ | ||
"#a6cee3", | ||
"#1f78b4", | ||
"#b2df8a", | ||
"#33a02c", | ||
"#fb9a99", | ||
"#e31a1c", | ||
"#fdbf6f", | ||
"#ff7f00", | ||
"#cab2d6", | ||
"#6a3d9a", | ||
"#ffff99", | ||
"#b15928", | ||
] | ||
|
||
c = iter(brewer_colors) | ||
|
||
gt = ( | ||
GT(exibble.loc[[0, 1, 4], ["num", "char", "fctr", "row", "group"]]) | ||
.tab_header("title", "subtitle") | ||
.tab_stub(rowname_col="row", groupname_col="group") | ||
.tab_source_note("yo") | ||
.tab_spanner("spanner", ["char", "fctr"]) | ||
.tab_stubhead("stubhead") | ||
) | ||
|
||
( | ||
gt.tab_style(style.fill(next(c)), loc.body()) | ||
# Columns ----------- | ||
# TODO: appears in browser, but not vs code | ||
.tab_style(style.fill(next(c)), loc.column_labels(columns="num")) | ||
.tab_style(style.fill(next(c)), loc.column_header()) | ||
.tab_style(style.fill(next(c)), loc.spanner_labels(ids=["spanner"])) | ||
# Header ----------- | ||
.tab_style(style.fill(next(c)), loc.header()) | ||
.tab_style(style.fill(next(c)), loc.subtitle()) | ||
.tab_style(style.fill(next(c)), loc.title()) | ||
# Footer ----------- | ||
.tab_style(style.borders(weight="3px"), loc.source_notes()) | ||
.tab_style(style.fill(next(c)), loc.footer()) | ||
# Stub -------------- | ||
.tab_style(style.fill(next(c)), loc.row_groups()) | ||
.tab_style(style.borders(weight="3px"), loc.stub(rows=1)) | ||
.tab_style(style.fill(next(c)), loc.stub()) | ||
.tab_style(style.fill(next(c)), loc.stubhead()) | ||
) | ||
``` | ||
|
||
## Body | ||
|
||
```{python} | ||
gt.tab_style(style.fill("yellow"), loc.body()) | ||
``` | ||
|
||
## Column labels | ||
|
||
```{python} | ||
( | ||
gt | ||
.tab_style(style.fill("yellow"), loc.column_header()) | ||
.tab_style(style.fill("blue"), loc.column_labels(columns="num")) | ||
.tab_style(style.fill("red"), loc.spanner_labels(ids=["spanner"])) | ||
) | ||
|
||
``` | ||
|
||
|
||
|
||
## Header | ||
|
||
```{python} | ||
( | ||
gt.tab_style(style.fill("yellow"), loc.header()) | ||
.tab_style(style.fill("blue"), loc.title()) | ||
.tab_style(style.fill("red"), loc.subtitle()) | ||
) | ||
``` | ||
|
||
## Footer | ||
|
||
```{python} | ||
( | ||
gt.tab_style( | ||
style.fill("yellow"), | ||
loc.source_notes(), | ||
).tab_style( | ||
style.borders(weight="3px"), | ||
loc.footer(), | ||
) | ||
) | ||
``` | ||
|
||
## Stub | ||
|
||
```{python} | ||
( | ||
gt.tab_style(style.fill("yellow"), loc.stub()) | ||
.tab_style(style.fill("blue"), loc.row_groups()) | ||
.tab_style( | ||
style.borders(style="dashed", weight="3px", color="red"), | ||
loc.stub(rows=[1]), | ||
) | ||
) | ||
``` | ||
|
||
## Stubhead | ||
|
||
```{python} | ||
gt.tab_style(style.fill("yellow"), loc.stubhead()) | ||
``` |
This file contains 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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from collections.abc import Sequence | ||
from dataclasses import dataclass, field, replace | ||
from enum import Enum, auto | ||
from typing import Any, Callable, Tuple, TypeVar, overload, TYPE_CHECKING | ||
from typing import Any, Callable, Literal, Tuple, TypeVar, Union, overload, TYPE_CHECKING | ||
|
||
from typing_extensions import Self, TypeAlias | ||
|
||
|
@@ -28,6 +28,7 @@ | |
|
||
if TYPE_CHECKING: | ||
from ._helpers import Md, Html, UnitStr, Text | ||
from ._locations import Loc | ||
|
||
T = TypeVar("T") | ||
|
||
|
@@ -610,7 +611,7 @@ | |
# TODO: validate | ||
return self.__class__(self.rows, self.group_rows.reorder(group_order)) | ||
|
||
def group_indices_map(self) -> list[tuple[int, str | None]]: | ||
def group_indices_map(self) -> list[tuple[int, GroupRowInfo | None]]: | ||
return self.group_rows.indices_map(len(self.rows)) | ||
|
||
def __iter__(self): | ||
|
@@ -740,7 +741,7 @@ | |
|
||
return self.__class__(reordered) | ||
|
||
def indices_map(self, n: int) -> list[tuple[int, str | None]]: | ||
def indices_map(self, n: int) -> list[tuple[int, GroupRowInfo]]: | ||
"""Return pairs of row index, group label for all rows in data. | ||
|
||
Note that when no groupings exist, n is used to return from range(n). | ||
|
@@ -751,7 +752,7 @@ | |
|
||
if not len(self._d): | ||
return [(ii, None) for ii in range(n)] | ||
return [(ind, info.defaulted_label()) for info in self for ind in info.indices] | ||
return [(ind, info) for info in self for ind in info.indices] | ||
|
||
|
||
# Spanners ---- | ||
|
@@ -852,7 +853,7 @@ | |
|
||
@dataclass(frozen=True) | ||
class FootnoteInfo: | ||
locname: str | None = None | ||
locname: Loc | None = None | ||
grpname: str | None = None | ||
colname: str | None = None | ||
locnum: int | None = None | ||
|
@@ -869,8 +870,7 @@ | |
|
||
@dataclass(frozen=True) | ||
class StyleInfo: | ||
locname: str | ||
locnum: int | ||
locname: Loc | ||
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. locname with a Loc class now fully identifies a style info, so no need for locnum 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. Great! |
||
grpname: str | None = None | ||
colname: str | None = None | ||
rownum: int | None = None | ||
|
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.
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.
now returning GroupRowInfo directly, since we need the group ids, not just their defaulted labels to match group name locs