recipes_for_engineers/make_tree.py

51 lines
995 B
Python

from anytree import Node, RenderTree
from anytree.exporter import JsonExporter
def print_ingredients():
for i, node in ingredients.items():
print(f" {i}.) {node.name}")
ingredients = {}
ingredient = "start"
i = 0
while ingredient:
print_ingredients()
ingredient = input("Add ingredient: ")
if ingredient:
ingredients[i] = Node(ingredient)
i += 1
task = "start"
while task:
print_ingredients()
task = input("Add task: ")
if task:
needs = input("List needs (comma-separated): ")
new_task = Node(task)
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)