Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a82f4f5
update escaping for html
attack68 Mar 14, 2021
51bda2c
update escaping for html
attack68 Mar 14, 2021
a323f93
tests for format_escape
attack68 Mar 14, 2021
64a54d3
Merge remote-tracking branch 'upstream/master' into escape_html_forma…
attack68 Mar 15, 2021
e01eeb5
check and docs fix
attack68 Mar 15, 2021
2348165
versionadded and whatsnew
attack68 Mar 15, 2021
27f39eb
only use escape on str
attack68 Mar 15, 2021
c1b29c3
refactor
attack68 Mar 15, 2021
0008af2
add decimal and thousands options to format function
attack68 Mar 21, 2021
02ba51e
add decimal and thousands options to format function
attack68 Mar 21, 2021
b07de09
docs
attack68 Mar 21, 2021
8ea4e0a
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Mar 23, 2021
2202a7c
tests with parameters
attack68 Mar 23, 2021
45dd2f1
var names
attack68 Mar 23, 2021
fb5dc2f
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Mar 27, 2021
863dd6c
move format tests
attack68 Mar 27, 2021
131b928
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Mar 31, 2021
b82e0b1
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Apr 13, 2021
a0b7fd6
restructure formatter wrappers
attack68 Apr 13, 2021
c35b1ee
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Apr 14, 2021
70f1e2b
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Apr 15, 2021
d834f8f
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Apr 18, 2021
19d862b
Merge remote-tracking branch 'upstream/master' into format_decimal_th…
attack68 Apr 20, 2021
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
Prev Previous commit
Next Next commit
check and docs fix
  • Loading branch information
attack68 committed Mar 15, 2021
commit e01eeb51a33ada34235185ccb482413d3c081b80
23 changes: 12 additions & 11 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from pandas.core.indexes.api import Index

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

BaseFormatter = Union[str, Callable]
ExtFormatter = Union[BaseFormatter, Dict[Any, Optional[BaseFormatter]]]
Expand Down Expand Up @@ -624,7 +624,7 @@ def format(
0 MISS 1.000 A
1 2.000 MISS 3.000

Using a format specification on consistent column dtypes
Using a ``formatter`` specification on consistent column dtypes

>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1])
0 1 2
Expand All @@ -647,24 +647,24 @@ def format(
0 MISS 1.00 A
1 2.0 PASS 3.00

Using a callable formatting function
Using a callable ``formatter`` function.

>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
>>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
0 1 2
0 MISS 1.0000 STRING
1 2.0 MISS FLOAT

Using a formatter with HTML ``escape``.
Using a ``formatter`` with HTML ``escape`` and ``na_rep``.

>>> df = pd.DataFrame([['<div></div>', '"A&B"']])
>>> s = df.style.format('<a href="a.com/{0}">{0}</a>', escape=True)
>>> 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.render()
...
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
<td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
<td .. >NA</td>
...

"""
if all(
(
Expand Down Expand Up @@ -2204,11 +2204,12 @@ def _maybe_wrap_formatter(
raise TypeError(f"'formatter' expected str or callable, got {type(formatter)}")

if na_rep is None:
na_func = formatter_func
return (lambda x: formatter_func(escape_func(x))) if escape else formatter_func
else:
na_func = lambda x: na_rep if pd.isna(x) else formatter_func(x)

return (lambda x: na_func(escape_func(x))) if escape else na_func
if escape:
return lambda x: na_rep if pd.isna(x) else formatter_func(escape_func(x))
else:
return lambda x: na_rep if pd.isna(x) else formatter_func(x)


def _maybe_convert_css_to_tuples(style: CSSProperties) -> CSSList:
Expand Down
13 changes: 11 additions & 2 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,14 +650,23 @@ def test_format_clear(self):
def test_format_escape(self):
df = DataFrame([['<>&"']])
s = Styler(df, uuid_len=0).format("X&{0}>X", escape=False)
ex = '<td id="T__row0_col0" class="data row0 col0" >X&<>&">X</td>'
assert ex in s.render()
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)
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(self):
# tests the na_rep is not escaped
df = DataFrame([['<>&"', None]])
s = Styler(df, uuid_len=0).format("X&{0}>X", escape=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()
assert expected2 in s.render()

def test_nonunique_raises(self):
df = DataFrame([[1, 2]], columns=["A", "A"])
msg = "style is not supported for non-unique indices."
Expand Down