textual icon indicating copy to clipboard operation
textual copied to clipboard

[BUG] Changing Background style via code does not propagate properly into Markdown Widget

Open Zaloog opened this issue 3 months ago • 2 comments

The bug

Changing the background of a Markdown Widget dynamically via code is not changing the background of all Markdown Segments accordingly. Also see example screenshot, where the code segment is still the previous color.

Changing the background via tcss works properly.

MRE:

from textual.app import App, ComposeResult
from textual.widgets import Markdown, Select

EXAMPLE_MARKDOWN = """\
# Markdown Viewer

This is an example of Textual's `MarkdownViewer` widget.


## Features

Markdown syntax and extensions are supported.

- Typography *emphasis*, **strong**, `inline code` etc.
- Headers

## Tables

Tables are displayed in a DataTable widget.

| Name            | Type   | Default | Description                        |
| --------------- | ------ | ------- | ---------------------------------- |
| `show_header`   | `bool` | `True`  | Show the table header              |
| `fixed_rows`    | `int`  | `0`     | Number of fixed rows               |


## Code Blocks

Code blocks are syntax highlighted.


"""


class MarkdownExampleApp(App):
    def compose(self) -> ComposeResult:
        yield Select.from_values(["red", "green", "blue"])
        markdown_viewer = Markdown(EXAMPLE_MARKDOWN)
        yield markdown_viewer

    def on_select_changed(self, event: Select.Changed):
        self.query_one(Markdown).styles.background = event.value

if __name__ == "__main__":
    app = MarkdownExampleApp()
    app.run()
Image

Zaloog avatar Nov 17 '25 16:11 Zaloog

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Nov 17 '25 16:11 github-actions[bot]

This isn't only the Markdown widget either from a quick test.

git bisect points to dca94393e77a9fa05df7b1f88865e1aec8d26054

from textual.app import App, ComposeResult
from textual.containers import CenterMiddle
from textual.widgets import Label, Select


class ExampleApp(App):
    def compose(self) -> ComposeResult:
        yield Select.from_values(["red", "green", "blue"])
        with CenterMiddle(id="color-container"):
            yield Label("Try changing the background color")

    def on_select_changed(self, event: Select.Changed):
        self.query_one("#color-container").styles.background = event.value


if __name__ == "__main__":
    app = ExampleApp()
    app.run()

TomJGooding avatar Nov 17 '25 17:11 TomJGooding