Skip to content

Conditionally styling by HTML classes #14226

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

Closed
dov opened this issue Sep 15, 2016 · 5 comments
Closed

Conditionally styling by HTML classes #14226

dov opened this issue Sep 15, 2016 · 5 comments
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string

Comments

@dov
Copy link

dov commented Sep 15, 2016

Currently the DataFrame.Styler class provides for adding of css key/value attribute pairs. But it is sometimes preferable to add a HTML class to a cell instead, and then let the styling be done by an external css file.

As an example consider the following DataFrame:

df = pd.DataFrame([[1,2],
                  [-5,1]],
                  columns=['A','B'])

As an example, we would like to attach the class "negative" to the -5 and receive the following HTML:

<html><table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>A</th>
      <th>B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td class="negative">-5</td>
      <td>1</td>
    </tr>
  </tbody>
</table></html>

Regarding how to implement this, here are a couple of ideas:

  1. Add a flag by_class to apply() and applymap() that instructs the styler that the "function" will return a classes instead of css strings.
  2. Add new functions apply_class() and applymap_class().
  3. Create a new class StyleClass() and use isinstance(ret,StyleClass) to determine whether to modify the style directly or by adding additional classes.

IMO, I believe that it would be a better interface of styler that apply and applymap were to return classes by default and then optionally provide a css style sheet that can be resolved when turning the style into html. Such an approach would also be easier to adapt to additional backends, e.g. LaTeX. The disadvantage though is that it would break backwards compatibility, and that it would make styling for iPython (the main usecase?) more complicated.

@TomAugspurger TomAugspurger added Output-Formatting __repr__ of pandas objects, to_string IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Sep 15, 2016
@TomAugspurger TomAugspurger added this to the 0.20.0 milestone Sep 15, 2016
@jreback jreback modified the milestones: 0.20.0, Next Major Release Mar 23, 2017
@8one6
Copy link

8one6 commented Aug 11, 2017

I think this would be a fantastic addition.

I think either the by_class arg/kwarg in apply() and applymap approach or the new apply_class()/applymap_class() approach make good sense and seem very intuitive.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 11, 2017

This should be terribly difficult to add, if anyone is interested in implementing it.

I'd prefer the apply_class method, over adding arguments to apply / applymap.

@attack68
Copy link
Contributor

I agree with the above and think it would be very useful in general.

I use styling quite extensibly to add formatting logic to user-dynamic tables, and that aspect works well, but it requires some quite extensive coding to get it working efficiently for large tables.

On thing that would be handy is a set_column_styles() method.

I currently have this workaround

table_styles = # some existing setup styles
column_styles = get_column_styles(df, props, subset)
s.set_table_styles(table_styles.update(column_styles))

def set_column_styles(df, props, subset=None):
    if subset is None:
         subset = df.columns
    styles = list()
    for col in subset:
        styles.append({'selector': '.col' + str(df.columns.get_loc(col)), 'props': props})
     return styles

I don't really have the ability yet to try and integrate this and do the github push, but it would be great if the above could be redefined as:

s.set_table_styles(table_styles)
s.set_column_styles(subset, props)

@jihwans

This comment has been minimized.

@attack68
Copy link
Contributor

attack68 commented Feb 4, 2021

this is closed by PR #36159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

7 participants