Skip to content

REF: type change escape in Styler.format to str to allow "html" and "latex" #41619

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 16 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
rename escape: escape_html
  • Loading branch information
attack68 committed May 22, 2021
commit daee4c623627da66f875ea9959e733307ac5ddd0
6 changes: 3 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Styler(StylerRenderer):

.. versionadded:: 1.3.0

escape : bool, default False
escape_html : bool, default False
Replace the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in cell display
strings with HTML-safe sequences.

Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(
uuid_len: int = 5,
decimal: str = ".",
thousands: str | None = None,
escape: bool = False,
escape_html: bool = False,
):
super().__init__(
data=data,
Expand All @@ -194,7 +194,7 @@ def __init__(
formatter=None,
precision=precision,
na_rep=na_rep,
escape=escape,
escape_html=escape_html,
decimal=decimal,
thousands=thousands,
)
Expand Down
20 changes: 11 additions & 9 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import pandas.core.common as com

jinja2 = import_optional_dependency("jinja2", extra="DataFrame.style requires jinja2.")
from markupsafe import escape as escape_html # markupsafe is jinja2 dependency
from markupsafe import escape as escape_html_f # markupsafe is jinja2 dependency

BaseFormatter = Union[str, Callable]
ExtFormatter = Union[BaseFormatter, Dict[Any, Optional[BaseFormatter]]]
Expand Down Expand Up @@ -403,7 +403,7 @@ def format(
precision: int | None = None,
decimal: str = ".",
thousands: str | None = None,
escape: bool = False,
escape_html: bool = False,
) -> StylerRenderer:
"""
Format the text display value of cells.
Expand Down Expand Up @@ -438,7 +438,7 @@ def format(

.. versionadded:: 1.3.0

escape : bool, default False
escape_html : bool, default False
Replace the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in cell display
string with HTML-safe sequences. Escaping is done before ``formatter``.

Expand Down Expand Up @@ -517,7 +517,9 @@ def format(
Using a ``formatter`` with HTML ``escape`` and ``na_rep``.

>>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
>>> s = df.style.format('<a href="a.com/{0}">{0}</a>', escape=True, na_rep="NA")
>>> s = df.style.format(
... '<a href="a.com/{0}">{0}</a>', escape_html=True, na_rep="NA"
... )
>>> s.render()
...
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
Expand All @@ -533,7 +535,7 @@ def format(
decimal == ".",
thousands is None,
na_rep is None,
escape is False,
escape_html is False,
)
):
self._display_funcs.clear()
Expand All @@ -555,7 +557,7 @@ def format(
precision=precision,
decimal=decimal,
thousands=thousands,
escape=escape,
escape_html=escape_html,
)
for ri in ris:
self._display_funcs[(ri, ci)] = format_func
Expand Down Expand Up @@ -720,7 +722,7 @@ def wrapper(x):
def _str_escape_html(x):
"""if escaping html: only use on str, else return input"""
if isinstance(x, str):
return escape_html(x)
return escape_html_f(x)
return x


Expand All @@ -730,7 +732,7 @@ def _maybe_wrap_formatter(
precision: int | None = None,
decimal: str = ".",
thousands: str | None = None,
escape: bool = False,
escape_html: bool = False,
) -> Callable:
"""
Allows formatters to be expressed as str, callable or None, where None returns
Expand All @@ -751,7 +753,7 @@ def _maybe_wrap_formatter(
raise TypeError(f"'formatter' expected str or callable, got {type(formatter)}")

# Replace HTML chars if escaping
if escape:
if escape_html:
func_1 = lambda x: func_0(_str_escape_html(x))
else:
func_1 = func_0
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/formats/style/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ def test_format_clear(styler):

def test_format_escape():
df = DataFrame([['<>&"']])
s = Styler(df, uuid_len=0).format("X&{0}>X", escape=False)
s = Styler(df, uuid_len=0).format("X&{0}>X", escape_html=False)
expected = '<td id="T__row0_col0" class="data row0 col0" >X&<>&">X</td>'
assert expected in s.render()

# only the value should be escaped before passing to the formatter
s = Styler(df, uuid_len=0).format("X&{0}>X", escape=True)
s = Styler(df, uuid_len=0).format("X&{0}>X", escape_html=True)
ex = '<td id="T__row0_col0" class="data row0 col0" >X&&lt;&gt;&amp;&#34;>X</td>'
assert ex in s.render()


def test_format_escape_na_rep():
# tests the na_rep is not escaped
df = DataFrame([['<>&"', None]])
s = Styler(df, uuid_len=0).format("X&{0}>X", escape=True, na_rep="&")
s = Styler(df, uuid_len=0).format("X&{0}>X", escape_html=True, na_rep="&")
ex = '<td id="T__row0_col0" class="data row0 col0" >X&&lt;&gt;&amp;&#34;>X</td>'
expected2 = '<td id="T__row0_col1" class="data row0 col1" >&</td>'
assert ex in s.render()
Expand All @@ -130,11 +130,11 @@ def test_format_escape_na_rep():

def test_format_escape_floats(styler):
# test given formatter for number format is not impacted by escape
s = styler.format("{:.1f}", escape=True)
s = styler.format("{:.1f}", escape_html=True)
for expected in [">0.0<", ">1.0<", ">-1.2<", ">-0.6<"]:
assert expected in s.render()
# tests precision of floats is not impacted by escape
s = styler.format(precision=1, escape=True)
s = styler.format(precision=1, escape_html=True)
for expected in [">0<", ">1<", ">-1.2<", ">-0.6<"]:
assert expected in s.render()

Expand Down