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"
def read_json() -> dict | None:
def read_json() -> dict:
try:
with open("./src/json/ingredients.json", "rt") as file:
return json.load(file)
except:
print("Could not find or read file.")
return {}
def update_json(meals: dict):
def update_json(meals: dict) -> None:
try:
with open("./src/json/ingredients.json", "w") as file:
json.dump(meals, file, indent=4)
except Exception as 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()
selected_meals = {}
if meal_json:
for meal in meals:
if meal in meal_json:
selected_meals[meal] = meal_json[meal]
return selected_meals
return selected_meals
def combine_ingredients(meals: dict) -> dict:
combined_ingredients = {}

View File

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