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:
objectConfiguration class for the TreePath class
- Parameters:
- Raises:
ValueError – If arguments are not of the expected type
- class prettypi.pretty_tree.TreeNode(value: str = '', color: Color = None, style: Style = None)
Bases:
objectTreeNode 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:
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()
- 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:
TreeNodeTreePath 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)