import json from anytree import LevelOrderIter, LevelOrderGroupIter from anytree.importer import DictImporter from sys import argv def build_rows_depth_first(node, rows, leaf_count=0): for child in sorted(node.children, key=lambda x: x.height, reverse=True): leaf_count = build_rows_depth_first(child, rows, leaf_count) colspan = 0 if node.siblings: max_sibling_height = max([s.height for s in node.siblings]) if max_sibling_height > node.height: colspan = max_sibling_height - node.height + 1 if node.is_leaf: cur_row = leaf_count rows[cur_row].append(f'{node.name}') if colspan > 1: rows[cur_row].append(f'') leaf_count += 1 else: child_leaves = len(node.leaves) cur_row = leaf_count - child_leaves rows[cur_row].append(f"{node.name}") return leaf_count if len(argv) < 2: print(f"usage: {argv[0]} ") exit() with open(argv[1], 'r') as f: recipe = json.loads(f.read()) recipe_name = recipe.get("name") recipe_desc = recipe.get("description") importer = DictImporter() sub_recipes = [importer.import_(data) for data in recipe["sub_recipes"]] print(f"

{recipe_name}

") print(f"

{recipe_desc}

") print("

Needs

") print("") print("

Steps

") print("
    ") for sub_recipe in sub_recipes: tasks = [node for node in LevelOrderIter(sub_recipe) if not node.is_leaf] for task in reversed(tasks): print(f"
  1. {task.description if task.description else task.name}
  2. ") print("
") for sub_recipe in sub_recipes: print("") output_rows = [] for i in range(len(sub_recipe.leaves)): output_rows.append([]) build_rows_depth_first(sub_recipe, output_rows) for row in output_rows: print("") for cell in row: print(f" {cell}") print("") print("
")