Skip to content

BUG: style.map() not compatible with CSS string "url(data:..." #59623

Open
@invalidarg

Description

@invalidarg

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 pandas as pd
print(pd.__version__) # 2.2.2

# Creating toy data
data = {
    "country": [ "Canada",  "Denmark"],
    "number": [ 200, 400]
}

def flag_background(country):
    if country == "Denmark":
        return """background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" id="flag-icons-dk" viewBox="0 0 640 480"><path fill="%23c8102e" d="M0 0h640.1v480H0z"/><path fill="%23fff" d="M205.7 0h68.6v480h-68.6z"/><path fill="%23fff" d="M0 205.7h640.1v68.6H0z"/></svg>');"""
    elif country == "Canada":
        return "background-color: red"
        

df = pd.DataFrame(data)
print(
    (
    df
    .style
    .map(flag_background)
    ).to_html()
)

### css in output is broken:
# #T_b3ead_row1_col0 {
#   background-image: url('data;
# }


#### But set_table_styles() works!

print(
    df
    .style
    .set_table_styles([
          {
              'selector': '.col0', 
                'props': 
                    [('background-image', '''url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" id="flag-icons-dk" viewBox="0 0 640 480"><path fill="%23c8102e" d="M0 0h640.1v480H0z"/><path fill="%23fff" d="M205.7 0h68.6v480h-68.6z"/><path fill="%23fff" d="M0 205.7h640.1v68.6H0z"/></svg>')'''), ('background-size', 'contain'),('background-repeat', 'no-repeat'),('background-position', 'center')]
         }
    ])
    .to_html()
)

### css in output is OK:
# #T_106ed .col0 {
#   background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" id="flag-icons-dk" viewBox="0 0 640 480"><path fill="%23c8102e" d="M0 0h640.1v480H0z"/><path fill="%23fff" d="M205.7 0h68.6v480h-68.6z"/><path fill="%23fff" d="M0 205.7h640.1v68.6H0z"/></svg>');
#   background-size: contain;
#   background-repeat: no-repeat;
#   background-position: center;
# }

Issue Description

I am tring to add SVG flags to each country but styler breaks css values with url(data:...

The CSS string returned by the function in style.map must be

property : value ; property2 : value ;

But there is valid CSS that does not follow this pattern.
e.g. this is valid CSS:
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" id="flag-icons-dk" viewBox="0 0 640 480"><path fill="%23c8102e" d="M0 0h640.1v480H0z"/><path fill="%23fff" d="M205.7 0h68.6v480h-68.6z"/><path fill="%23fff" d="M0 205.7h640.1v68.6H0z"/></svg>');

The problem is that pandas finds two consecutive colons : is will replace the second with semicolon ; and then truncate. I.e. the resulting HTML will be

background-image: url('data;

Expected Behavior

Let me input any valid CSS string. Remove validation / truncation since it is not compatible with valid CSS strings.

Installed Versions

/databricks/python/lib/python3.10/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-1067-azure
Version : #76~20.04.1-Ubuntu SMP Thu Jun 13 18:00:23 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.23.5
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : 0.29.32
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions