-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add Styler.to_typst()
#60733
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
mroeschke
merged 9 commits into
pandas-dev:main
from
3w36zj6:feature/add-typst-support-to-styler
Feb 1, 2025
Merged
ENH: Add Styler.to_typst()
#60733
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
41198e5
ENH: Add `to_typst` method to `Styler`
3w36zj6 209b2df
TST: Add `Styler.to_typst()` test cases
3w36zj6 dda68e3
STY: Apply Ruff suggestions
3w36zj6 47ff1bf
DOC: Update What's new
3w36zj6 becfb36
DOC: Update reference
3w36zj6 373254c
CI: Add `Styler.template_typst` to validation ignore list
3w36zj6 8f149ab
DOC: Update docstring format for `Styler.to_typst()` example
3w36zj6 10c6c8d
Merge branch 'main' into feature/add-typst-support-to-styler
mroeschke 058327c
DOC: Update versionadded for `Styler.to_typst()` to 3.0.0 in document…
3w36zj6 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 | ||||
---|---|---|---|---|---|---|
|
@@ -1228,6 +1228,111 @@ def to_latex( | |||||
) | ||||||
return save_to_buffer(latex, buf=buf, encoding=encoding) | ||||||
|
||||||
@overload | ||||||
def to_typst( | ||||||
self, | ||||||
buf: FilePath | WriteBuffer[str], | ||||||
*, | ||||||
encoding: str | None = ..., | ||||||
sparse_index: bool | None = ..., | ||||||
sparse_columns: bool | None = ..., | ||||||
max_rows: int | None = ..., | ||||||
max_columns: int | None = ..., | ||||||
) -> None: ... | ||||||
|
||||||
@overload | ||||||
def to_typst( | ||||||
self, | ||||||
buf: None = ..., | ||||||
*, | ||||||
encoding: str | None = ..., | ||||||
sparse_index: bool | None = ..., | ||||||
sparse_columns: bool | None = ..., | ||||||
max_rows: int | None = ..., | ||||||
max_columns: int | None = ..., | ||||||
) -> str: ... | ||||||
|
||||||
@Substitution(buf=buffering_args, encoding=encoding_args) | ||||||
def to_typst( | ||||||
self, | ||||||
buf: FilePath | WriteBuffer[str] | None = None, | ||||||
*, | ||||||
encoding: str | None = None, | ||||||
sparse_index: bool | None = None, | ||||||
sparse_columns: bool | None = None, | ||||||
max_rows: int | None = None, | ||||||
max_columns: int | None = None, | ||||||
) -> str | None: | ||||||
""" | ||||||
Write Styler to a file, buffer or string in Typst format. | ||||||
|
||||||
.. versionadded:: 2.3.0 | ||||||
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.
Suggested change
|
||||||
|
||||||
Parameters | ||||||
---------- | ||||||
%(buf)s | ||||||
%(encoding)s | ||||||
sparse_index : bool, optional | ||||||
Whether to sparsify the display of a hierarchical index. Setting to False | ||||||
will display each explicit level element in a hierarchical key for each row. | ||||||
Defaults to ``pandas.options.styler.sparse.index`` value. | ||||||
sparse_columns : bool, optional | ||||||
Whether to sparsify the display of a hierarchical index. Setting to False | ||||||
will display each explicit level element in a hierarchical key for each | ||||||
column. Defaults to ``pandas.options.styler.sparse.columns`` value. | ||||||
max_rows : int, optional | ||||||
The maximum number of rows that will be rendered. Defaults to | ||||||
``pandas.options.styler.render.max_rows``, which is None. | ||||||
max_columns : int, optional | ||||||
The maximum number of columns that will be rendered. Defaults to | ||||||
``pandas.options.styler.render.max_columns``, which is None. | ||||||
|
||||||
Rows and columns may be reduced if the number of total elements is | ||||||
large. This value is set to ``pandas.options.styler.render.max_elements``, | ||||||
which is 262144 (18 bit browser rendering). | ||||||
|
||||||
Returns | ||||||
------- | ||||||
str or None | ||||||
If `buf` is None, returns the result as a string. Otherwise returns `None`. | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
DataFrame.to_typst : Write a DataFrame to a file, | ||||||
buffer or string in Typst format. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
>>> df = pd.DataFrame({"A": [1, 2], "B": [3, 4]}) | ||||||
>>> df.style.to_typst() # doctest: +SKIP | ||||||
|
||||||
.. code-block:: typst | ||||||
|
||||||
#table( | ||||||
columns: 3, | ||||||
[], [A], [B], | ||||||
|
||||||
[0], [1], [3], | ||||||
[1], [2], [4], | ||||||
) | ||||||
""" | ||||||
obj = self._copy(deepcopy=True) | ||||||
|
||||||
if sparse_index is None: | ||||||
sparse_index = get_option("styler.sparse.index") | ||||||
if sparse_columns is None: | ||||||
sparse_columns = get_option("styler.sparse.columns") | ||||||
|
||||||
text = obj._render_typst( | ||||||
sparse_columns=sparse_columns, | ||||||
sparse_index=sparse_index, | ||||||
max_rows=max_rows, | ||||||
max_cols=max_columns, | ||||||
) | ||||||
return save_to_buffer( | ||||||
text, buf=buf, encoding=(encoding if buf is not None else None) | ||||||
) | ||||||
|
||||||
@overload | ||||||
def to_html( | ||||||
self, | ||||||
|
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 |
---|---|---|
@@ -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 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 |
---|---|---|
@@ -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 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.
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.
Can you move this to 3.0.0?