-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ENH: Add `to_typst` method to `Styler` * TST: Add `Styler.to_typst()` test cases * STY: Apply Ruff suggestions * DOC: Update What's new * DOC: Update reference * CI: Add `Styler.template_typst` to validation ignore list * DOC: Update docstring format for `Styler.to_typst()` example * DOC: Update versionadded for `Styler.to_typst()` to 3.0.0 in documentation --------- Co-authored-by: Matthew Roeschke <[email protected]>
- Loading branch information
Showing
7 changed files
with
233 additions
and
0 deletions.
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
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
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,12 @@ | ||
#table( | ||
columns: {{ head[0] | length }}, | ||
{% for r in head %} | ||
{% for c in r %}[{% if c["is_visible"] %}{{ c["display_value"] }}{% endif %}],{% if not loop.last %} {% endif%}{% endfor %} | ||
|
||
{% endfor %} | ||
|
||
{% for r in body %} | ||
{% for c in r %}[{% if c["is_visible"] %}{{ c["display_value"] }}{% endif %}],{% if not loop.last %} {% endif%}{% endfor %} | ||
|
||
{% endfor %} | ||
) |
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,96 @@ | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
) | ||
|
||
pytest.importorskip("jinja2") | ||
from pandas.io.formats.style import Styler | ||
|
||
|
||
@pytest.fixture | ||
def df(): | ||
return DataFrame( | ||
{"A": [0, 1], "B": [-0.61, -1.22], "C": Series(["ab", "cd"], dtype=object)} | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def styler(df): | ||
return Styler(df, uuid_len=0, precision=2) | ||
|
||
|
||
def test_basic_table(styler): | ||
result = styler.to_typst() | ||
expected = dedent( | ||
"""\ | ||
#table( | ||
columns: 4, | ||
[], [A], [B], [C], | ||
[0], [0], [-0.61], [ab], | ||
[1], [1], [-1.22], [cd], | ||
)""" | ||
) | ||
assert result == expected | ||
|
||
|
||
def test_concat(styler): | ||
result = styler.concat(styler.data.agg(["sum"]).style).to_typst() | ||
expected = dedent( | ||
"""\ | ||
#table( | ||
columns: 4, | ||
[], [A], [B], [C], | ||
[0], [0], [-0.61], [ab], | ||
[1], [1], [-1.22], [cd], | ||
[sum], [1], [-1.830000], [abcd], | ||
)""" | ||
) | ||
assert result == expected | ||
|
||
|
||
def test_concat_recursion(styler): | ||
df = styler.data | ||
styler1 = styler | ||
styler2 = Styler(df.agg(["sum"]), uuid_len=0, precision=3) | ||
styler3 = Styler(df.agg(["sum"]), uuid_len=0, precision=4) | ||
result = styler1.concat(styler2.concat(styler3)).to_typst() | ||
expected = dedent( | ||
"""\ | ||
#table( | ||
columns: 4, | ||
[], [A], [B], [C], | ||
[0], [0], [-0.61], [ab], | ||
[1], [1], [-1.22], [cd], | ||
[sum], [1], [-1.830], [abcd], | ||
[sum], [1], [-1.8300], [abcd], | ||
)""" | ||
) | ||
assert result == expected | ||
|
||
|
||
def test_concat_chain(styler): | ||
df = styler.data | ||
styler1 = styler | ||
styler2 = Styler(df.agg(["sum"]), uuid_len=0, precision=3) | ||
styler3 = Styler(df.agg(["sum"]), uuid_len=0, precision=4) | ||
result = styler1.concat(styler2).concat(styler3).to_typst() | ||
expected = dedent( | ||
"""\ | ||
#table( | ||
columns: 4, | ||
[], [A], [B], [C], | ||
[0], [0], [-0.61], [ab], | ||
[1], [1], [-1.22], [cd], | ||
[sum], [1], [-1.830], [abcd], | ||
[sum], [1], [-1.8300], [abcd], | ||
)""" | ||
) | ||
assert result == expected |
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