Method: rich.pretty._ipy_display_hook
Calls: 6, Exceptions: 1, Paths: 4Back
Path 1: 3 calls (0.5)
test_ipy_display_hook__repr_html.
Console (3)
'ignore' (3)
False (3)
False (3)
None (3)
None (3)
None (3)
False (3)
None (3)
1def _ipy_display_hook(
2 value: Any,
3 console: Optional["Console"] = None,
4 overflow: "OverflowMethod" = "ignore",
5 crop: bool = False,
6 indent_guides: bool = False,
7 max_length: Optional[int] = None,
8 max_string: Optional[int] = None,
9 max_depth: Optional[int] = None,
10 expand_all: bool = False,
11) -> None:
12 # needed here to prevent circular import:
13 from ._inspect import is_object_one_of_types
14 from .console import ConsoleRenderable
15
16 # always skip rich generated jupyter renderables or None values
17 if _safe_isinstance(value, JupyterRenderable) or value is None:
18 return
19
20 console = console or get_console()
21 if console.is_jupyter:
22 # Delegate rendering to IPython if the object (and IPython) supports it
23 # https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
24 ipython_repr_methods = [
25 "_repr_html_",
26 "_repr_markdown_",
27 "_repr_json_",
28 "_repr_latex_",
29 "_repr_jpeg_",
30 "_repr_png_",
31 "_repr_svg_",
32 "_repr_mimebundle_",
33 ]
34 for repr_method in ipython_repr_methods:
35 method = getattr(value, repr_method, None)
36 if inspect.ismethod(method):
37 # Calling the method ourselves isn't ideal. The interface for the `_repr_*_` methods
38 # specifies that if they return None, then they should not be rendered
39 # by the notebook.
40 try:
41 repr_result = method()
42 except Exception:
43 continue # If the method raises, treat it as if it doesn't exist, try any others
44 if repr_result is not None:
45 return # Delegate rendering to IPython
46
47 # When in a Jupyter notebook let's avoid the display of some specific classes,
48 # as they result in the rendering of useless and noisy lines such as `<Figure size 432x288 with 1 Axes>`.
49 # What does this do?
50 # --> if the class has "matplotlib.artist.Artist" in its hierarchy for example, we don't render it.
51 if is_object_one_of_types(value, JUPYTER_CLASSES_TO_NOT_RENDER):
52 return
53
54 # certain renderables should start on a new line
55 if _safe_isinstance(value, ConsoleRenderable):
56 console.line()
57
58 console.print(
59 value
60 if _safe_isinstance(value, RichRenderable)
61 else Pretty(
62 value,
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 margin=12,
70 ),
71 crop=crop,
72 new_line_start=True,
73 )
Path 2: 1 calls (0.17)
test_ipy_display_hook__no_special_repr_methods.
Console (1)
'ignore' (1)
False (1)
False (1)
None (1)
None (1)
None (1)
False (1)
1def _ipy_display_hook(
2 value: Any,
3 console: Optional["Console"] = None,
4 overflow: "OverflowMethod" = "ignore",
5 crop: bool = False,
6 indent_guides: bool = False,
7 max_length: Optional[int] = None,
8 max_string: Optional[int] = None,
9 max_depth: Optional[int] = None,
10 expand_all: bool = False,
11) -> None:
12 # needed here to prevent circular import:
13 from ._inspect import is_object_one_of_types
14 from .console import ConsoleRenderable
15
16 # always skip rich generated jupyter renderables or None values
17 if _safe_isinstance(value, JupyterRenderable) or value is None:
18 return
19
20 console = console or get_console()
21 if console.is_jupyter:
22 # Delegate rendering to IPython if the object (and IPython) supports it
23 # https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
24 ipython_repr_methods = [
25 "_repr_html_",
26 "_repr_markdown_",
27 "_repr_json_",
28 "_repr_latex_",
29 "_repr_jpeg_",
30 "_repr_png_",
31 "_repr_svg_",
32 "_repr_mimebundle_",
33 ]
34 for repr_method in ipython_repr_methods:
35 method = getattr(value, repr_method, None)
36 if inspect.ismethod(method):
37 # Calling the method ourselves isn't ideal. The interface for the `_repr_*_` methods
38 # specifies that if they return None, then they should not be rendered
39 # by the notebook.
40 try:
41 repr_result = method()
42 except Exception:
43 continue # If the method raises, treat it as if it doesn't exist, try any others
44 if repr_result is not None:
45 return # Delegate rendering to IPython
46
47 # When in a Jupyter notebook let's avoid the display of some specific classes,
48 # as they result in the rendering of useless and noisy lines such as `<Figure size 432x288 with 1 Axes>`.
49 # What does this do?
50 # --> if the class has "matplotlib.artist.Artist" in its hierarchy for example, we don't render it.
51 if is_object_one_of_types(value, JUPYTER_CLASSES_TO_NOT_RENDER):
52 return
53
54 # certain renderables should start on a new line
55 if _safe_isinstance(value, ConsoleRenderable):
56 console.line()
57
58 console.print(
59 value
60 if _safe_isinstance(value, RichRenderable)
61 else Pretty(
62 value,
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 margin=12,
70 ),
71 crop=crop,
72 new_line_start=True,
73 )
Path 3: 1 calls (0.17)
Text (1)
Console (1)
'ignore' (1)
False (1)
False (1)
None (1)
None (1)
None (1)
False (1)
TypeError (1)
1def _ipy_display_hook(
2 value: Any,
3 console: Optional["Console"] = None,
4 overflow: "OverflowMethod" = "ignore",
5 crop: bool = False,
6 indent_guides: bool = False,
7 max_length: Optional[int] = None,
8 max_string: Optional[int] = None,
9 max_depth: Optional[int] = None,
10 expand_all: bool = False,
11) -> None:
12 # needed here to prevent circular import:
13 from ._inspect import is_object_one_of_types
14 from .console import ConsoleRenderable
15
16 # always skip rich generated jupyter renderables or None values
17 if _safe_isinstance(value, JupyterRenderable) or value is None:
18 return
19
20 console = console or get_console()
21 if console.is_jupyter:
22 # Delegate rendering to IPython if the object (and IPython) supports it
23 # https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
24 ipython_repr_methods = [
25 "_repr_html_",
26 "_repr_markdown_",
27 "_repr_json_",
28 "_repr_latex_",
29 "_repr_jpeg_",
30 "_repr_png_",
31 "_repr_svg_",
32 "_repr_mimebundle_",
33 ]
34 for repr_method in ipython_repr_methods:
35 method = getattr(value, repr_method, None)
36 if inspect.ismethod(method):
37 # Calling the method ourselves isn't ideal. The interface for the `_repr_*_` methods
38 # specifies that if they return None, then they should not be rendered
39 # by the notebook.
40 try:
41 repr_result = method()
42 except Exception:
43 continue # If the method raises, treat it as if it doesn't exist, try any others
44 if repr_result is not None:
45 return # Delegate rendering to IPython
46
47 # When in a Jupyter notebook let's avoid the display of some specific classes,
48 # as they result in the rendering of useless and noisy lines such as `<Figure size 432x288 with 1 Axes>`.
49 # What does this do?
50 # --> if the class has "matplotlib.artist.Artist" in its hierarchy for example, we don't render it.
51 if is_object_one_of_types(value, JUPYTER_CLASSES_TO_NOT_RENDER):
52 return
53
54 # certain renderables should start on a new line
55 if _safe_isinstance(value, ConsoleRenderable):
56 console.line()
57
58 console.print(
59 value
60 if _safe_isinstance(value, RichRenderable)
61 else Pretty(
62 value,
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 margin=12,
70 ),
71 crop=crop,
72 new_line_start=True,
73 )
Path 4: 1 calls (0.17)
test_ipy_display_hook__classes_to_not_render.
Console (1)
'ignore' (1)
False (1)
False (1)
None (1)
None (1)
None (1)
False (1)
None (1)
1def _ipy_display_hook(
2 value: Any,
3 console: Optional["Console"] = None,
4 overflow: "OverflowMethod" = "ignore",
5 crop: bool = False,
6 indent_guides: bool = False,
7 max_length: Optional[int] = None,
8 max_string: Optional[int] = None,
9 max_depth: Optional[int] = None,
10 expand_all: bool = False,
11) -> None:
12 # needed here to prevent circular import:
13 from ._inspect import is_object_one_of_types
14 from .console import ConsoleRenderable
15
16 # always skip rich generated jupyter renderables or None values
17 if _safe_isinstance(value, JupyterRenderable) or value is None:
18 return
19
20 console = console or get_console()
21 if console.is_jupyter:
22 # Delegate rendering to IPython if the object (and IPython) supports it
23 # https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
24 ipython_repr_methods = [
25 "_repr_html_",
26 "_repr_markdown_",
27 "_repr_json_",
28 "_repr_latex_",
29 "_repr_jpeg_",
30 "_repr_png_",
31 "_repr_svg_",
32 "_repr_mimebundle_",
33 ]
34 for repr_method in ipython_repr_methods:
35 method = getattr(value, repr_method, None)
36 if inspect.ismethod(method):
37 # Calling the method ourselves isn't ideal. The interface for the `_repr_*_` methods
38 # specifies that if they return None, then they should not be rendered
39 # by the notebook.
40 try:
41 repr_result = method()
42 except Exception:
43 continue # If the method raises, treat it as if it doesn't exist, try any others
44 if repr_result is not None:
45 return # Delegate rendering to IPython
46
47 # When in a Jupyter notebook let's avoid the display of some specific classes,
48 # as they result in the rendering of useless and noisy lines such as `<Figure size 432x288 with 1 Axes>`.
49 # What does this do?
50 # --> if the class has "matplotlib.artist.Artist" in its hierarchy for example, we don't render it.
51 if is_object_one_of_types(value, JUPYTER_CLASSES_TO_NOT_RENDER):
52 return
53
54 # certain renderables should start on a new line
55 if _safe_isinstance(value, ConsoleRenderable):
56 console.line()
57
58 console.print(
59 value
60 if _safe_isinstance(value, RichRenderable)
61 else Pretty(
62 value,
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 margin=12,
70 ),
71 crop=crop,
72 new_line_start=True,
73 )