import json from anytree import Node, RenderTree from anytree.exporter import DictExporter from recipe import Recipe def print_nodes(): for i, node in nodes.items(): print(f" {i}.) {node.result}") nodes = {} i = 0 node = "start" while node: print_nodes() node = input("Add ingredient/task: ") if node: needs = "" if nodes: needs = input("List needs (comma-separated): ") description = input("Detailed description: ") if needs: result = input("Result of node (e.g. chopped carrots): ") new_node = Node(node, description=description, result=result) for key in needs.split(","): try: nodes.pop(int(key.strip())).parent = new_node except KeyError: print(f"<{key.strip()}> was not a valid ingredient or task") else: new_node = Node(node, description=description, result=node) nodes[i] = new_node i += 1 else: for key, tree_node in nodes.items(): print(key, end=" ") for pre, _, render_node in RenderTree(tree_node): print(f"{pre}{render_node.name}") confirm = input("Done adding ingredients and tasks? (y/N): ") if not confirm or not confirm.lower().startswith("y"): node = "continue" name = "" try: while not name: name = input("Recipe name (required to save, ctrl-c to exit): ") except KeyboardInterrupt: print("Exiting...") exit(0) description = input("Recipe description: ") servings = input("Servings: ") prep_time = input("Prep time: ") cook_time = input("Cook time: ") recipe = Recipe(name, description, servings, prep_time, cook_time) for _, node in nodes.items(): recipe.add_sub_recipe(node) filename = f"{name.replace(' ', '_').lower()}.json" print(f"Writing to {filename}...") with open(filename, 'w') as f: f.write(recipe.to_json()) print("Done!")