rich.pretty

class rich.pretty.Node(key_repr='', value_repr='', open_brace='', close_brace='', empty='', last=False, is_tuple=False, is_namedtuple=False, children=None, separator=', ')[source]

A node in a repr tree. May be atomic or a container.

Parameters
Return type

None

check_length(start_length, max_length)[source]

Check the length fits within a limit.

Parameters
  • start_length (int) – Starting length of the line (indent, prefix, suffix).

  • max_length (int) – Maximum length.

Returns

True if the node can be rendered within max length, otherwise False.

Return type

bool

iter_tokens()[source]

Generate tokens for this node.

Return type

Iterable[str]

render(max_width=80, indent_size=4, expand_all=False)[source]

Render the node to a pretty repr.

Parameters
  • max_width (int, optional) – Maximum width of the repr. Defaults to 80.

  • indent_size (int, optional) – Size of indents. Defaults to 4.

  • expand_all (bool, optional) – Expand all levels. Defaults to False.

Returns

A repr string of the original object.

Return type

str

class rich.pretty.Pretty(_object, highlighter=None, *, indent_size=4, justify=None, overflow=None, no_wrap=False, indent_guides=False, max_length=None, max_string=None, max_depth=None, expand_all=False, margin=0, insert_line=False)[source]

A rich renderable that pretty prints an object.

Parameters
  • _object (Any) – An object to pretty print.

  • highlighter (HighlighterType, optional) – Highlighter object to apply to result, or None for ReprHighlighter. Defaults to None.

  • indent_size (int, optional) – Number of spaces in indent. Defaults to 4.

  • justify (JustifyMethod, optional) – Justify method, or None for default. Defaults to None.

  • overflow (OverflowMethod, optional) – Overflow method, or None for default. Defaults to None.

  • no_wrap (Optional[bool], optional) – Disable word wrapping. Defaults to False.

  • indent_guides (bool, optional) – Enable indentation guides. Defaults to False.

  • max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None.

  • max_string (int, optional) – Maximum length of string before truncating, or None to disable. Defaults to None.

  • max_depth (int, optional) – Maximum depth of nested data structures, or None for no maximum. Defaults to None.

  • expand_all (bool, optional) – Expand all containers. Defaults to False.

  • margin (int, optional) – Subtrace a margin from width to force containers to expand earlier. Defaults to 0.

  • insert_line (bool, optional) – Insert a new line if the output has multiple new lines. Defaults to False.

Return type

None

rich.pretty.install(console=None, overflow='ignore', crop=False, indent_guides=False, max_length=None, max_string=None, expand_all=False)[source]

Install automatic pretty printing in the Python REPL.

Parameters
  • console (Console, optional) – Console instance or None to use global console. Defaults to None.

  • overflow (Optional[OverflowMethod], optional) – Overflow method. Defaults to “ignore”.

  • crop (Optional[bool], optional) – Enable cropping of long lines. Defaults to False.

  • indent_guides (bool, optional) – Enable indentation guides. Defaults to False.

  • max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None.

  • max_string (int, optional) – Maximum length of string before truncating, or None to disable. Defaults to None.

  • expand_all (bool, optional) – Expand all containers. Defaults to False.

  • max_frames (int) – Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.

Return type

None

rich.pretty.is_expandable(obj)[source]

Check if an object may be expanded by pretty print.

Parameters

obj (Any) –

Return type

bool

rich.pretty.pprint(_object, *, console=None, indent_guides=True, max_length=None, max_string=None, max_depth=None, expand_all=False)[source]

A convenience function for pretty printing.

Parameters
  • _object (Any) – Object to pretty print.

  • console (Console, optional) – Console instance, or None to use default. Defaults to None.

  • max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None.

  • max_string (int, optional) – Maximum length of strings before truncating, or None to disable. Defaults to None.

  • max_depth (int, optional) – Maximum depth for nested data structures, or None for unlimited depth. Defaults to None.

  • indent_guides (bool, optional) – Enable indentation guides. Defaults to True.

  • expand_all (bool, optional) – Expand all containers. Defaults to False.

Return type

None

rich.pretty.pretty_repr(_object, *, max_width=80, indent_size=4, max_length=None, max_string=None, max_depth=None, expand_all=False)[source]

Prettify repr string by expanding on to new lines to fit within a given width.

Parameters
  • _object (Any) – Object to repr.

  • max_width (int, optional) – Desired maximum width of repr string. Defaults to 80.

  • indent_size (int, optional) – Number of spaces to indent. Defaults to 4.

  • max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None.

  • max_string (int, optional) – Maximum length of string before truncating, or None to disable truncating. Defaults to None.

  • max_depth (int, optional) – Maximum depth of nested data structure, or None for no depth. Defaults to None.

  • expand_all (bool, optional) – Expand all containers regardless of available width. Defaults to False.

Returns

A possibly multi-line representation of the object.

Return type

str

rich.pretty.traverse(_object, max_length=None, max_string=None, max_depth=None)[source]

Traverse object and generate a tree.

Parameters
  • _object (Any) – Object to be traversed.

  • max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None.

  • max_string (int, optional) – Maximum length of string before truncating, or None to disable truncating. Defaults to None.

  • max_depth (int, optional) – Maximum depth of data structures, or None for no maximum. Defaults to None.

Returns

The root of a tree structure which can be used to render a pretty repr.

Return type

Node