Conditional styling #1261
-
Hi! On the off chance I missed this in the docs, would you consider support for conditional formatting based on a cell/renderable's value? E.g. if I had a table with a "status" column, I may want "failed" to show up as red and "pass" to show up as green. Right now a similar effect can be achieved by specifying the style of each In short, I'd expect that if |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Not convinced this is a good idea. Rich tables deliberately don't care too much about the contents of the cells, and only needs to know how to render them. Adding this callable would require the Table class to have at least some understanding of the cell contents. I think there may be better ways of achieving this, albeit not quite as transparently. One option would be to create a highlighter that highlights the text "failed" and "pass" differently. See highlighters in the docs. Another option (and my preferred option) would be to create a renderable that renders the appropriate result based on a boolean. Something like this: class Status:
def __init__(self, success:bool) -> None:
self.success = success
def __rich__(self) -> Text:
return Text("pass", style="green") if self.success else Text("fail", style="red") You can then add instances of Status to your table row. If you already have an object containing the status, you could add the |
Beta Was this translation helpful? Give feedback.
Not convinced this is a good idea. Rich tables deliberately don't care too much about the contents of the cells, and only needs to know how to render them. Adding this callable would require the Table class to have at least some understanding of the cell contents.
I think there may be better ways of achieving this, albeit not quite as transparently.
One option would be to create a highlighter that highlights the text "failed" and "pass" differently. See highlighters in the docs.
Another option (and my preferred option) would be to create a renderable that renders the appropriate result based on a boolean. Something like this: