DataTable conditional formatting? #2101
Unanswered
TomJGooding
asked this question in
Q&A
Replies: 1 comment 5 replies
-
After spending some time reading other discussions and the docs in more detail, I've realised that you can use Rich console markup to apply formatting to Perhaps there is still a way of formatting table cells with CSS that I'm missing? My updated code below is very inefficient as it creates then immediately updates each cell, but I'm still trying to get to grips with this! ...
def on_mount(self) -> None:
table = self.query_one(DataTable)
rows = iter(ROWS)
table.add_columns(*next(rows))
table.add_rows(rows)
self.format_results()
def format_results(self) -> None:
table = self.query_one(DataTable)
result_cells = table.get_column_at(1)
for idx, result in enumerate(result_cells):
cell_coordinate = Coordinate(idx, 1)
if int(result) >= 90:
table.update_cell_at(
cell_coordinate,
f"[green]{result}[/green]",
)
elif int(result) <= 30:
table.update_cell_at(
cell_coordinate,
f"[red]{result}[/red]",
)
else:
table.update_cell_at(
cell_coordinate,
f"[dark_orange]{result}[/dark_orange]",
)
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How might you approach applying some form of 'conditional formatting' to a DataTable?
Below is a contrived example of a table with student exam results data. Let's say that results above 90% should be formatted in green, below 30% in red, and any results in between in orange.
I'm still not sure I fully understand just how to construct a DataTable - hopefully the docs for this might be expanded at some point?
Many thanks in advance for any advice!
Beta Was this translation helpful? Give feedback.
All reactions