from anytree import Node, RenderTree from anytree.exporter import JsonExporter def print_ingredients(): for i, node in ingredients.items(): print(f" {i}.) {node.result}") ingredients = {} ingredient = "start" i = 0 while ingredient: print_ingredients() ingredient = input("Add ingredient: ") if ingredient: ingredients[i] = Node(ingredient, result=ingredient) i += 1 task = "start" while task: print_ingredients() task = input("Add task: ") if task: needs = input("List needs (comma-separated): ") description = input("Detailed description: ") result = input("Result of task (e.g. chopped carrots): ") new_task = Node(task, description=description, result=result) ingredients[i] = new_task i += 1 for e in [int(key.strip()) for key in needs.split(",")]: ingredients.pop(e).parent = new_task for pre, _, node in RenderTree(new_task): print(f"{pre}{node.name}") name = input("Recipe name: ") if name: name = name.replace(" ", "_") with open(f"{name}.json", 'w') as f: JsonExporter(indent=2).write(new_task, f)