Description
I noticed that the HTML generated by the Styler also contains CSS rules for explicitly excluded table elements (rows/cols).
I'm aware that not all unused CSS rules can be detected, but I suspect that any CSS rule with an id-selector where the id is not in the table could be omitted.
API breaking implications
I am not aware of what to do and what undesirable side effects this could have.
Additional context
Tested with pandas 1.3.3.
Here are two easy examples:
In this example html
contains only one unused CSS rule:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
html = df.style\
.highlight_max(axis=None)\
.hide_columns(df.columns)\
.hide_columns()\
.hide_index(df.index)\
.hide_index()\
.render()
breakpoint()
But when using background_gradient
, we potential have a huge amount (depending of the size of the df
and the amount of hidden content) of unused CSS rules:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
html = df.style\
.background_gradient() \
.hide_columns(df.columns) \
.hide_columns() \
.hide_index(df.index) \
.hide_index() \
.render()
breakpoint()
Reducing the amount of data in the generated html could also improve other use cases like mentioned here
Is this optimization possible without unwanted side effects and easy to implement? If so, and the benefit is great enough, I could try it out if you give me some code pointers.