Skip to content

Commit 1744cd9

Browse files
authored
Merge pull request #6040 from Textualize/pretty-fix
fix for pretty
2 parents bfbe564 + 5bfd0e4 commit 1744cd9

File tree

3 files changed

+187
-19
lines changed

3 files changed

+187
-19
lines changed

src/textual/widgets/_pretty.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
from rich.pretty import Pretty as PrettyRenderable
88

9-
from textual.widget import Widget
9+
from textual.widgets import Static
1010

1111

12-
class Pretty(Widget):
12+
class Pretty(Static):
1313
"""A pretty-printing widget.
1414
1515
Used to pretty-print any object.
@@ -37,27 +37,13 @@ def __init__(
3737
id: The ID of the pretty in the DOM.
3838
classes: The CSS classes of the pretty.
3939
"""
40-
super().__init__(
41-
name=name,
42-
id=id,
43-
classes=classes,
44-
)
45-
self._renderable = PrettyRenderable(object)
46-
47-
def render(self) -> PrettyRenderable:
48-
"""Render the pretty-printed object.
49-
50-
Returns:
51-
The rendered pretty-print.
52-
"""
53-
return self._renderable
40+
super().__init__(name=name, id=id, classes=classes)
41+
self.update(object)
5442

5543
def update(self, object: Any) -> None:
5644
"""Update the content of the pretty widget.
5745
5846
Args:
5947
object: The object to pretty-print.
6048
"""
61-
self._renderable = PrettyRenderable(object)
62-
self.clear_cached_dimensions()
63-
self.refresh(layout=True)
49+
super().update(PrettyRenderable(object))
Lines changed: 150 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,3 +4538,35 @@ def compose(self) -> ComposeResult:
45384538
)
45394539

45404540
assert snap_compare(StreamApp())
4541+
4542+
4543+
def test_pretty_auto(snap_compare):
4544+
"""Test that pretty works with auto dimensions.
4545+
4546+
You should see 'Hello World' including strings, 3 times in a purple box, top left.
4547+
4548+
"""
4549+
from textual.widgets import Pretty
4550+
4551+
class Demo(App):
4552+
CSS = """
4553+
Vertical {
4554+
background: blue 30%;
4555+
height: auto;
4556+
width: auto;
4557+
}
4558+
4559+
Pretty {
4560+
width: auto;
4561+
height: auto;
4562+
background: red 30%;
4563+
}
4564+
"""
4565+
4566+
def compose(self) -> ComposeResult:
4567+
with Vertical():
4568+
yield Pretty("hello world")
4569+
yield Pretty("hello world")
4570+
yield Pretty("hello world")
4571+
4572+
assert snap_compare(Demo())

0 commit comments

Comments
 (0)