PrettyTree

prettypi.pretty_tree module help you to print easily a tree.

class prettypi.pretty_tree.Config(folder_color: Color = None, folder_style: Style = Style.BOLD, file_color: Color = None, file_style: Style = None)

Bases: object

Configuration class for the TreePath class

Parameters:
  • folder_color (Color, optional) – The color of the folder, defaults to None

  • folder_style (Style, optional) – The style of the folder, defaults to Style.BOLD

  • file_color (Color, optional) – The color of the file, defaults to None

  • file_style (Style, optional) – The style of the file, defaults to None

Raises:

ValueError – If arguments are not of the expected type

file_color: Color = None
file_style: Style = None
folder_color: Color = None
folder_style: Style = '\x1b[1m'
class prettypi.pretty_tree.TreeNode(value: str = '', color: Color = None, style: Style = None)

Bases: object

TreeNode class to create a tree.

Features:

  • Use the add_child method to add a child to the node.

  • Add color and style to the node with the set_color and set_style methods.

  • Use the display method to display the tree.

Parameters:
  • value (str) – The value of the node

  • color (Color, optional) – The color of the node, defaults to None

  • style (Style, optional) – The style of the node, defaults to None

Example:

from prettypi.pretty_tree import TreeNode
from prettypi.utils import Color, Style

root = TreeNode("Root", color=Color.RED, style=Style.BOLD)
child1 = TreeNode("Child1", color=Color.GREEN, style=Style.UNDERLINE)
child2 = TreeNode("Child2", color=Color.GREEN, style=Style.UNDERLINE)

child1.add_child(TreeNode("Child1.1"))
child1.add_child(TreeNode("Child1.2"))

root.add_child(child1)
root.add_child(child2)

root.display()
add_child(child: TreeNode | List[TreeNode])

Add a child or a list of children to the node.

Parameters:

child (Union["TreeNode", list["TreeNode"]]) – The child or the list of children to add

Raises:

TypeError – child must be of type TreeNode or list[TreeNode]

Example:

root = TreeNode("Root")

child1 = TreeNode("Child1", color=Color.GREEN, style=Style.UNDERLINE)
child1.add_child(TreeNode("Child1.1"))

root.add_child(child1)
display()

Display the tree.

Example:

root = TreeNode("Root")
root.display()
set_color(color: Color)

Set the color of the node.

Parameters:

color (Color) – The color of the node

Raises:

ValueError – Invalid color

Example:

root = TreeNode("Root")
root.set_color(Color.RED)
set_style(style: Style)

Set the style of the node.

Parameters:

style (Style) – The style of the node

Raises:

ValueError – Invalid style

Example:

root = TreeNode("Root")
root.set_style(Style.BOLD)
class prettypi.pretty_tree.TreePath(path: str, config: ~prettypi.pretty_tree.pretty_tree_path.Config = Config(folder_color=None, folder_style=<Style.BOLD: '\x1b[1m'>, file_color=None, file_style=None))

Bases: TreeNode

TreePath class to create a tree of a path

Features:

  • Use TreePath to create a tree of a path.

  • Use the display method to display the tree.

  • Add color and style to the folder and file with the config parameter.

Parameters:
  • path (str) – The path to create the tree

  • config (Config, optional) – The configuration of the tree, defaults to Config()

Example:

from prettypi.pretty_tree import TreePath
from prettypi.utils import Color, Style

config = Config(folder_color=Color.RED, folder_style=Style.BOLD)
tree = TreePath("path/to/folder", config=config)
tree.display()
display()

Display the tree

Example:

tree = TreePath("path/to/folder")
tree.display()
set_config(config: Config)

Set the configuration of the tree

Parameters:

config (Config) – The configuration of the tree

Example:

from prettypi.pretty_tree import TreePath, Config
from prettypi.utils import Color, Style

config = Config(folder_color=Color.RED, folder_style=Style.BOLD)
tree = TreePath("path/to/folder")
tree.set_config(config)