Skip to content

PERF: styler uuid control and security #36345

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 9 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Performance improvements
- Performance improvements when creating Series with dtype `str` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`)
- Performance improvement in :meth:`GroupBy.agg` with the ``numba`` engine (:issue:`35759`)
- Performance improvement in :meth:`GroupBy.transform` with the ``numba`` engine (:issue:`36240`)
- ``Styler`` uuid method altered to compress data transmission over web whilst maintaining reasonably low table collision probability (:issue:`36345`)

.. ---------------------------------------------------------------------------

Expand Down
12 changes: 9 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Tuple,
Union,
)
from uuid import uuid1
from uuid import uuid4

import numpy as np

Expand Down Expand Up @@ -73,6 +73,9 @@ class Styler:
List of {selector: (attr, value)} dicts; see Notes.
uuid : str, default None
A unique identifier to avoid CSS collisions; generated automatically.
uuid_len : int, default 5
If ``uuid`` is not specified, the length of the ``uuid`` to randomly generate
in characters, max is 32.
caption : str, default None
Caption to attach to the table.
table_attributes : str, default None
Expand Down Expand Up @@ -128,6 +131,7 @@ class Styler:

* Blank cells include ``blank``
* Data cells include ``data``

"""

loader = jinja2.PackageLoader("pandas", "io/formats/templates")
Expand All @@ -140,6 +144,7 @@ def __init__(
precision: Optional[int] = None,
table_styles: Optional[List[Dict[str, List[Tuple[str, str]]]]] = None,
uuid: Optional[str] = None,
uuid_len: int = 5,
caption: Optional[str] = None,
table_attributes: Optional[str] = None,
cell_ids: bool = True,
Expand All @@ -159,7 +164,8 @@ def __init__(
self.index = data.index
self.columns = data.columns

self.uuid = uuid
self.uuid_len = uuid_len if uuid_len < 33 else 32
self.uuid = (uuid or uuid4().hex[: self.uuid_len]) + "_"
self.table_styles = table_styles
self.caption = caption
if precision is None:
Expand Down Expand Up @@ -248,7 +254,7 @@ def _translate(self):
precision = self.precision
hidden_index = self.hidden_index
hidden_columns = self.hidden_columns
uuid = self.uuid or str(uuid1()).replace("-", "_")
uuid = self.uuid
ROW_HEADING_CLASS = "row_heading"
COL_HEADING_CLASS = "col_heading"
INDEX_NAME_CLASS = "index_name"
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,24 @@ def test_colspan_w3(self):
s = Styler(df, uuid="_", cell_ids=False)
assert '<th class="col_heading level0 col0" colspan="2">l0</th>' in s.render()

@pytest.mark.parametrize("len_", [1, 5, 32])
def test_uuid_len(self, len_):
# GH 36345
df = pd.DataFrame(data=[["A"]])
s = Styler(df, uuid_len=len_, cell_ids=False).render()
strt = s.find('id="T_')
end = s[strt + 6 :].find('"')
assert end == len_ + 1

@pytest.mark.parametrize("len_", [33, 100])
def test_uuid_max_len(self, len_):
# GH 36345
df = pd.DataFrame(data=[["A"]])
s = Styler(df, uuid_len=len_, cell_ids=False).render()
strt = s.find('id="T_')
end = s[strt + 6 :].find('"')
assert end == 32 + 1


@td.skip_if_no_mpl
class TestStylerMatplotlibDep:
Expand Down