minor changes

This commit is contained in:
Nicholas
2025-11-22 16:00:23 -05:00
parent 75cd42ed30
commit 7347b53bfe
2 changed files with 27 additions and 24 deletions

View File

@@ -7,28 +7,29 @@ load_dotenv()
file_path = os.getenv("file_path") if os.getenv("file_path") else "./checklist.md" file_path = os.getenv("file_path") if os.getenv("file_path") else "./checklist.md"
def read_json() -> dict | None: def read_json() -> dict:
try: try:
with open("./src/json/ingredients.json", "rt") as file: with open("./src/json/ingredients.json", "rt") as file:
return json.load(file) return json.load(file)
except: except:
print("Could not find or read file.") print("Could not find or read file.")
return {}
def update_json(meals: dict): def update_json(meals: dict) -> None:
try: try:
with open("./src/json/ingredients.json", "w") as file: with open("./src/json/ingredients.json", "w") as file:
json.dump(meals, file, indent=4) json.dump(meals, file, indent=4)
except Exception as e: except Exception as e:
print(f"Unable to write data to file. {e}") print(f"Unable to write data to file. {e}")
def get_selected_meals(meals: dict) -> dict | None: def get_selected_meals(meals: dict) -> dict:
meal_json = read_json() meal_json = read_json()
selected_meals = {} selected_meals = {}
if meal_json: if meal_json:
for meal in meals: for meal in meals:
if meal in meal_json: if meal in meal_json:
selected_meals[meal] = meal_json[meal] selected_meals[meal] = meal_json[meal]
return selected_meals return selected_meals
def combine_ingredients(meals: dict) -> dict: def combine_ingredients(meals: dict) -> dict:
combined_ingredients = {} combined_ingredients = {}

View File

@@ -3,10 +3,6 @@ from FileHandler import read_json, update_json
import flet as ft import flet as ft
meal_json = read_json() meal_json = read_json()
global new_ingredient, new_quantity, new_units
new_ingredient = ""
new_quantity = ""
new_units = ""
def is_number(s): def is_number(s):
try: try:
@@ -57,19 +53,22 @@ def builder(page):
return row return row
def update_ingredients(self, name): def update_ingredients(self, name):
if not self.new_ingredient or not self.new_quantity or not is_number(self.new_quantity): pass
return # Doesn't work yet
ingredient = { # if not self.new_ingredient or not self.new_quantity or not is_number(self.new_quantity):
self.new_ingredient: # return
{
"quantity":float(self.new_quantity), # ingredient = {
"units": self.new_units if self.new_units != "" else None # self.new_ingredient:
} # {
} # "quantity":float(self.new_quantity),
meal_json[name] = ingredient # "units": self.new_units if self.new_units != "" else None
update_json(meal_json) # }
page.update() # }
# meal_json[name] = ingredient
# update_json(meal_json)
# page.update()
def show_new_meal(self): def show_new_meal(self):
self.new_meal = {} self.new_meal = {}
@@ -98,14 +97,17 @@ def builder(page):
alignment=ft.MainAxisAlignment.SPACE_EVENLY, alignment=ft.MainAxisAlignment.SPACE_EVENLY,
width=300, width=300,
height=100,)) height=100,))
page.update() page.update()
def add_new_meal(self, selector_body): def add_new_meal(self, selector_body):
meal = selector_body.controls[0].value meal = selector_body.controls[0].value
ing = [] ing, qua, uni = [], [], []
qua = []
uni = []
for row in selector_body.controls[1].controls: for row in selector_body.controls[1].controls:
#skip blank row
if row.controls[0].value == "" or row.controls[1].value == "":
continue
ing.append(row.controls[0].value) ing.append(row.controls[0].value)
qua.append(row.controls[1].value) qua.append(row.controls[1].value)
uni.append(row.controls[2].value if row.controls[2].value != "" else None) uni.append(row.controls[2].value if row.controls[2].value != "" else None)
@@ -114,7 +116,7 @@ def builder(page):
for i in range(len(ing)): for i in range(len(ing)):
meal_json[meal][ing[i]] = { 'quantity': qua[i], 'units': uni[i] } meal_json[meal][ing[i]] = { 'quantity': qua[i], 'units': uni[i] }
#write changes to the ingredients.json file
update_json(meal_json) update_json(meal_json)
self.show_new_meal() self.show_new_meal()