Replies: 1 comment 3 replies
-
The problem is that the There might be a better way, but here's a possible workaround: from textual.app import App, ComposeResult
from textual.content import Content
from textual.widgets import DataTable, Static
class HelloApp(App[None]):
def compose(self) -> ComposeResult:
yield Static(
Content.from_markup(
"Changing theme should match: [$accent]Accent color[/]",
)
)
yield DataTable()
def populate_table(self) -> None:
table = self.query_one(DataTable)
table.clear(True)
table.add_column("Column name")
# Color is not applied
table.add_row(
Content.from_markup("Some text. [$accent]Accent color[/]. More text.")
)
# Doesn't work
table.add_row(
Content.from_markup("Some text. [#ffa62b]Accent color[/]. More text.")
)
# The problem is that the DataTable will render cell data as Rich Text,
# so Textual Content isn't converted correctly.
#
# Instead, you can get the theme's accent color and provide the cell
# data as simple markup.
accent = self.app.current_theme.accent
table.add_row(f"Some text. [{accent}]Accent color[/]. More text.")
def on_mount(self) -> None:
self.populate_table()
# But now the accent color is "hard-coded" in the cell, so won't update
# when the theme is changed.
#
# Unfortunately we'll need to re-populate the table when the theme changes.
# Here's how you might subscribe to the `theme_changed_signal`.
self.app.theme_changed_signal.subscribe(self, lambda _: self.populate_table())
if __name__ == "__main__":
HelloApp().run() |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to understand what am I doing wrong, but it looks like DataTable cells have very limited styling options.
Beta Was this translation helpful? Give feedback.
All reactions