Skip to content

Commit 41198e5

Browse files
committed
ENH: Add to_typst method to Styler
1 parent a15a4b5 commit 41198e5

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

pandas/io/formats/style.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,99 @@ def to_latex(
12281228
)
12291229
return save_to_buffer(latex, buf=buf, encoding=encoding)
12301230

1231+
@overload
1232+
def to_typst(
1233+
self,
1234+
buf: FilePath | WriteBuffer[str],
1235+
*,
1236+
encoding: str | None = ...,
1237+
sparse_index: bool | None = ...,
1238+
sparse_columns: bool | None = ...,
1239+
max_rows: int | None = ...,
1240+
max_columns: int | None = ...,
1241+
) -> None: ...
1242+
1243+
@overload
1244+
def to_typst(
1245+
self,
1246+
buf: None = ...,
1247+
*,
1248+
encoding: str | None = ...,
1249+
sparse_index: bool | None = ...,
1250+
sparse_columns: bool | None = ...,
1251+
max_rows: int | None = ...,
1252+
max_columns: int | None = ...,
1253+
) -> str: ...
1254+
1255+
@Substitution(buf=buffering_args, encoding=encoding_args)
1256+
def to_typst(
1257+
self,
1258+
buf: FilePath | WriteBuffer[str] | None = None,
1259+
*,
1260+
encoding: str | None = None,
1261+
sparse_index: bool | None = None,
1262+
sparse_columns: bool | None = None,
1263+
max_rows: int | None = None,
1264+
max_columns: int | None = None,
1265+
) -> str | None:
1266+
"""
1267+
Write Styler to a file, buffer or string in Typst format.
1268+
1269+
Parameters
1270+
----------
1271+
%(buf)s
1272+
%(encoding)s
1273+
sparse_index : bool, optional
1274+
Whether to sparsify the display of a hierarchical index. Setting to False
1275+
will display each explicit level element in a hierarchical key for each row.
1276+
Defaults to ``pandas.options.styler.sparse.index`` value.
1277+
sparse_columns : bool, optional
1278+
Whether to sparsify the display of a hierarchical index. Setting to False
1279+
will display each explicit level element in a hierarchical key for each
1280+
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
1281+
max_rows : int, optional
1282+
The maximum number of rows that will be rendered. Defaults to
1283+
``pandas.options.styler.render.max_rows``, which is None.
1284+
max_columns : int, optional
1285+
The maximum number of columns that will be rendered. Defaults to
1286+
``pandas.options.styler.render.max_columns``, which is None.
1287+
1288+
Rows and columns may be reduced if the number of total elements is
1289+
large. This value is set to ``pandas.options.styler.render.max_elements``,
1290+
which is 262144 (18 bit browser rendering).
1291+
1292+
Returns
1293+
-------
1294+
str or None
1295+
If `buf` is None, returns the result as a string. Otherwise returns `None`.
1296+
1297+
See Also
1298+
--------
1299+
DataFrame.to_typst : Write a DataFrame to a file, buffer or string in Typst format.
1300+
1301+
Examples
1302+
--------
1303+
>>> df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
1304+
>>> df.style.to_typst()
1305+
'#table(\\n columns: 3,\\n [], [A], [B],\\n\\n [0], [1], [3],\\n [1], [2], [4],\\n)'
1306+
"""
1307+
obj = self._copy(deepcopy=True)
1308+
1309+
if sparse_index is None:
1310+
sparse_index = get_option("styler.sparse.index")
1311+
if sparse_columns is None:
1312+
sparse_columns = get_option("styler.sparse.columns")
1313+
1314+
text = obj._render_typst(
1315+
sparse_columns=sparse_columns,
1316+
sparse_index=sparse_index,
1317+
max_rows=max_rows,
1318+
max_cols=max_columns,
1319+
)
1320+
return save_to_buffer(
1321+
text, buf=buf, encoding=(encoding if buf is not None else None)
1322+
)
1323+
12311324
@overload
12321325
def to_html(
12331326
self,

pandas/io/formats/style_render.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class StylerRenderer:
7777
template_html_table = env.get_template("html_table.tpl")
7878
template_html_style = env.get_template("html_style.tpl")
7979
template_latex = env.get_template("latex.tpl")
80+
template_typst = env.get_template("typst.tpl")
8081
template_string = env.get_template("string.tpl")
8182

8283
def __init__(
@@ -232,6 +233,21 @@ def _render_latex(
232233
d.update(kwargs)
233234
return self.template_latex.render(**d)
234235

236+
def _render_typst(
237+
self,
238+
sparse_index: bool,
239+
sparse_columns: bool,
240+
max_rows: int | None = None,
241+
max_cols: int | None = None,
242+
**kwargs,
243+
) -> str:
244+
"""
245+
Render a Styler in typst format
246+
"""
247+
d = self._render(sparse_index, sparse_columns, max_rows, max_cols)
248+
d.update(kwargs)
249+
return self.template_typst.render(**d)
250+
235251
def _render_string(
236252
self,
237253
sparse_index: bool,

pandas/io/formats/templates/typst.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#table(
2+
columns: {{ head[0] | length }},
3+
{% for r in head %}
4+
{% for c in r %}[{% if c["is_visible"] %}{{ c["display_value"] }}{% endif %}],{% if not loop.last %} {% endif%}{% endfor %}
5+
6+
{% endfor %}
7+
8+
{% for r in body %}
9+
{% for c in r %}[{% if c["is_visible"] %}{{ c["display_value"] }}{% endif %}],{% if not loop.last %} {% endif%}{% endfor %}
10+
11+
{% endfor %}
12+
)

0 commit comments

Comments
 (0)