Skip to content

Commit 0325b5e

Browse files
committed
Updated pretty_print.py example to use ppretty().
1 parent a49094a commit 0325b5e

1 file changed

Lines changed: 5 additions & 24 deletions

File tree

examples/pretty_print.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
11
#!/usr/bin/env python3
2-
"""A simple example demonstrating how to pretty print JSON data in a cmd2 app using rich."""
3-
4-
from rich.json import JSON
2+
"""A simple example demonstrating how to pretty print data."""
53

64
import cmd2
75

86
EXAMPLE_DATA = {
97
"name": "John Doe",
108
"age": 30,
119
"address": {"street": "123 Main St", "city": "Anytown", "state": "CA"},
12-
"hobbies": ["reading", "hiking", "coding"],
10+
"hobbies": ["reading", "hiking", "coding", "cooking", "running", "painting", "music", "photography", "cycling"],
1311
}
1412

1513

1614
class Cmd2App(cmd2.Cmd):
1715
def __init__(self) -> None:
1816
super().__init__()
19-
self.data = EXAMPLE_DATA
20-
21-
def do_normal(self, _) -> None:
22-
"""Display the data using the normal poutput method."""
23-
self.poutput(self.data)
24-
25-
def do_pretty(self, _) -> None:
26-
"""Display the JSON data in a pretty way using rich."""
2717

28-
json_renderable = JSON.from_data(
29-
self.data,
30-
indent=2,
31-
highlight=True,
32-
skip_keys=False,
33-
ensure_ascii=False,
34-
check_circular=True,
35-
allow_nan=True,
36-
default=None,
37-
sort_keys=False,
38-
)
39-
self.poutput(json_renderable)
18+
def do_pretty(self, _: cmd2.Statement) -> None:
19+
"""Print an object using ppretty()."""
20+
self.ppretty(EXAMPLE_DATA)
4021

4122

4223
if __name__ == '__main__':

0 commit comments

Comments
 (0)