Method: rich.pretty.install
Calls: 2, Exceptions: 2, Paths: 1Back
Path 1: 2 calls (1.0)
Console (2)
'ignore' (2)
False (2)
False (2)
None (2)
None (2)
None (1) 1 (1)
False (2)
NameError (2)
1def install(
2 console: Optional["Console"] = None,
3 overflow: "OverflowMethod" = "ignore",
4 crop: bool = False,
5 indent_guides: bool = False,
6 max_length: Optional[int] = None,
7 max_string: Optional[int] = None,
8 max_depth: Optional[int] = None,
9 expand_all: bool = False,
10) -> None:
11 """Install automatic pretty printing in the Python REPL.
12
13 Args:
14 console (Console, optional): Console instance or ``None`` to use global console. Defaults to None.
15 overflow (Optional[OverflowMethod], optional): Overflow method. Defaults to "ignore".
16 crop (Optional[bool], optional): Enable cropping of long lines. Defaults to False.
17 indent_guides (bool, optional): Enable indentation guides. Defaults to False.
18 max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
19 Defaults to None.
20 max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
21 max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
22 expand_all (bool, optional): Expand all containers. Defaults to False.
23 max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
24 """
25 from rich import get_console
26
27 console = console or get_console()
28 assert console is not None
29
30 def display_hook(value: Any) -> None:
31 """Replacement sys.displayhook which prettifies objects with Rich."""
32 if value is not None:
33 assert console is not None
34 builtins._ = None # type: ignore[attr-defined]
35 console.print(
36 value
37 if _safe_isinstance(value, RichRenderable)
38 else Pretty(
39 value,
40 overflow=overflow,
41 indent_guides=indent_guides,
42 max_length=max_length,
43 max_string=max_string,
44 max_depth=max_depth,
45 expand_all=expand_all,
46 ),
47 crop=crop,
48 )
49 builtins._ = value # type: ignore[attr-defined]
50
51 try: # pragma: no cover
52 ip = get_ipython() # type: ignore[name-defined]
53 from IPython.core.formatters import BaseFormatter
54
55 class RichFormatter(BaseFormatter): # type: ignore[misc]
56 pprint: bool = True
57
58 def __call__(self, value: Any) -> Any:
59 if self.pprint:
60 return _ipy_display_hook(
61 value,
62 console=get_console(),
63 overflow=overflow,
64 indent_guides=indent_guides,
65 max_length=max_length,
66 max_string=max_string,
67 max_depth=max_depth,
68 expand_all=expand_all,
69 )
70 else:
71 return repr(value)
72
73 # replace plain text formatter with rich formatter
74 rich_formatter = RichFormatter()
75 ip.display_formatter.formatters["text/plain"] = rich_formatter
76 except Exception:
77 sys.displayhook = display_hook