question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditionally styling by HTML classes

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
TomAugspurgercommented, 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.

1reaction
attack68commented, Feb 4, 2021

this is closed by PR #36159

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS | Conditional Rules - GeeksforGeeks
CSS Conditional Rules are nothing but a feature of CSS in which the CSS style is applied based on a specific condition.
Read more >
How to conditionally apply a CSS class? [duplicate]
Variables in CSS should be declared within a CSS selector that defines its scope. For a global scope you can use either the...
Read more >
Logic Hop Conditional CSS
Enabling Conditional CSS ; Step 1. Select a Logic Hop Condition ; Step 2. Copy the CSS class ; Step 3. Check the...
Read more >
Applying React Conditional classNames - Pluralsight
Depending on your preferences and needs, you can apply conditional classNames with a ternary or with a library.
Read more >
Use CSS to change the style of each row depending on the ...
In this example, we will create a conditional template that will display different content depending on the referenced-value of a {{marker}}. We will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found