Skip to content

BUG: Pandas Styler HTML not rendering properly #58872

Closed
@trendyllama

Description

@trendyllama

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd


matrix = np.random.randint(10000, size=(40, 30))

df = pd.DataFrame(matrix)


style = df.style.highlight_between(left=8000, inclusive='neither')
style = style.hide(axis='index')
style = style.format(precision=2)
html_str = style.to_html()

or...

style = df.style.background_gradient(cmap = cm,axis=None)
html_str = style.to_html()


fix...

import textwrap
html_str = textwrap.fill(html_str, width=200)

Issue Description

When using pandas styler objects the HTML they render has lines that extend beyond 1000 characters causing ids to truncate unexpectedly and break formatting styles. This is fine for most browsers but can cause problems for emails - there should be an option to control this from within the styler framework.

I use pandas 1,4 as my daily driver, but this behavior occurs on 2.2 as well.

Here is an example of the truncation at line column 1000 of an email message.

#T_4858f_row14_col16, #T!
f_row18_col21, #T_4858f_!

Here is the trucation at line column 1

_4858f_row14_col17,
 row18_col22, #T_48

As you can see the ids are being cut off, thus breaking the color background formatting.

Fix

The temporary fix is using the textwrap module to trucate the lines properly at a lower character threshold - 200 and 250 characters seemed to work for me.

Expected Behavior

I would expect every line of the render html to not extend beyond a certain character limit and truncate each line effectively without breaking mid word.

Installed Versions

1.4.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsNeeds DiscussionRequires discussion from core team before further actionStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions