def __init__(self): super().__init__() this_file_path = os.path.dirname(os.path.abspath(__file__)) PyQt5.uic.loadUi(os.path.join(this_file_path, 'ui/gui.ui'), self) self.canvas = MyMplCanvas() self.canvas.setMaximumHeight(500) self.canvas.setMinimumHeight(400) self.vbox_layout = self.layout().itemAt(0) placeholder_widget = self.vbox_layout.itemAt(1).widget() self.vbox_layout.removeWidget(placeholder_widget) self.vbox_layout.insertWidget(1, self.canvas) self.hbox_layout = self.vbox_layout.itemAt(0) self.day_guis = list() self.update_diet_lock = Lock() self.diet = Diet('diet.json') for day in self.diet.days: day_gui = DayGui() self.day_guis.append(day_gui) self.hbox_layout.addWidget(day_gui) day_gui.set_day(day.day_name) for meal in day.meals: for food in meal.foods: day_gui.add_food_to_meal(food, meal.meal_type) self.menu_widget = self.layout().itemAt(1).widget() self.menu_categories_widgets: Dict[FoodType, DraggableListWidget] = dict() for i, food_type in zip(range(1, 14, 3), FoodType): widget_to_remove = self.menu_widget.layout().itemAt(i).widget() self.menu_widget.layout().removeWidget(widget_to_remove) self.menu_categories_widgets[food_type] = DraggableListWidget( rows_are_deletable=False) self.menu_categories_widgets[food_type].setDragDropMode( QtWidgets.QAbstractItemView.DragDropMode.DragOnly) self.menu_widget.layout().insertWidget( i, self.menu_categories_widgets[food_type]) self.menu_categories_widgets[FoodType.placeholder].setMaximumHeight(60) all_edible_foods = [ food for food in get_all_foods_from_db() if food.unit == FoodUnit.item ] all_edible_foods.extend(derived_foods) all_edible_foods = sorted(all_edible_foods, key=lambda x: x.name) for food in all_edible_foods: item = QtWidgets.QListWidgetItem(f'1 x {food.name}') self.menu_categories_widgets[food.food_type].addItem(item) for day_gui in self.day_guis: for widget in day_gui.meal_widgets.values(): widget.diet_changed.connect(self.on_diet_changed)
def get_diet(params): factor = params['factor'] pounds_per_month = params['pounds_per_month'] minimum_price = params[ 'minimum_price'] if 'minimum_price' in params else Diet.minimum_price maximum_price = params[ 'maximum_price'] if 'maximum_price' in params else Diet.maximum_price diet = Diet(factor, pounds_per_month, minimum_price, maximum_price) return { 'factor': diet.factor, 'calories': diet.calories(), 'unit_price': diet.unit_price(), 'total_price': diet.total_price(), }
def test(self): diet = Diet(0.9, 4) self.assertEqual(diet.unit_price(), 11.85) self.assertEqual(diet.total_price(), 47.4) self.assertEqual(diet.factor, 0.9)
def test_default_min_and_max(self): diet = Diet(0.9, 4, 5, 30) # Values of object self.assertEqual(diet.minimum_price, 5.0) self.assertEqual(diet.maximum_price, 30.0)
int(diet_macros_and_price["carbo"]), int(diet_macros_and_price["protein"]), int(diet_macros_and_price["fat"]), int(diet_macros_and_price["price"]) ]) print(t1) print(t2) weight = 80 height = 180 sex = "mezczyzna" diet_price = 100 diet = Diet(weight=weight, height=height, sex=sex, csv_file_with_products="products.csv", price=diet_price) random_diet = diet.create_diet_random_algorithm() greedy_diet = diet.create_diet_greedy_algorithm() demands = diet.makroelements_demand_for_month() compare_diets(demands, diet_price, random_diet, greedy_diet) # while True: # display_menu() # choice = input("Wybierz co chcesz zrobić 1/2/3/4: ") # if choice == "5": # exit(0) # elif choice == "1" or choice == "2" or choice == "3" or choice == "4": # # weight = float(input("Podaj swoją wagę[kg]: ")) # # height = input("Podaj swoj wzrost[cm]: ")
class Gui(QtWidgets.QWidget): def __init__(self): super().__init__() this_file_path = os.path.dirname(os.path.abspath(__file__)) PyQt5.uic.loadUi(os.path.join(this_file_path, 'ui/gui.ui'), self) self.canvas = MyMplCanvas() self.canvas.setMaximumHeight(500) self.canvas.setMinimumHeight(400) self.vbox_layout = self.layout().itemAt(0) placeholder_widget = self.vbox_layout.itemAt(1).widget() self.vbox_layout.removeWidget(placeholder_widget) self.vbox_layout.insertWidget(1, self.canvas) self.hbox_layout = self.vbox_layout.itemAt(0) self.day_guis = list() self.update_diet_lock = Lock() self.diet = Diet('diet.json') for day in self.diet.days: day_gui = DayGui() self.day_guis.append(day_gui) self.hbox_layout.addWidget(day_gui) day_gui.set_day(day.day_name) for meal in day.meals: for food in meal.foods: day_gui.add_food_to_meal(food, meal.meal_type) self.menu_widget = self.layout().itemAt(1).widget() self.menu_categories_widgets: Dict[FoodType, DraggableListWidget] = dict() for i, food_type in zip(range(1, 14, 3), FoodType): widget_to_remove = self.menu_widget.layout().itemAt(i).widget() self.menu_widget.layout().removeWidget(widget_to_remove) self.menu_categories_widgets[food_type] = DraggableListWidget( rows_are_deletable=False) self.menu_categories_widgets[food_type].setDragDropMode( QtWidgets.QAbstractItemView.DragDropMode.DragOnly) self.menu_widget.layout().insertWidget( i, self.menu_categories_widgets[food_type]) self.menu_categories_widgets[FoodType.placeholder].setMaximumHeight(60) all_edible_foods = [ food for food in get_all_foods_from_db() if food.unit == FoodUnit.item ] all_edible_foods.extend(derived_foods) all_edible_foods = sorted(all_edible_foods, key=lambda x: x.name) for food in all_edible_foods: item = QtWidgets.QListWidgetItem(f'1 x {food.name}') self.menu_categories_widgets[food.food_type].addItem(item) for day_gui in self.day_guis: for widget in day_gui.meal_widgets.values(): widget.diet_changed.connect(self.on_diet_changed) # the widget has been modified by the user, so we want to recreate the diet class to reflect the user changes def update_diet_from_widgets(self): self.update_diet_lock.acquire() self.diet.days = list() for day_gui in self.day_guis: day = Day() self.diet.days.append(day) day.day_name = day_gui.day_name.text() # print(day.day_name) for meal_type, meal_widget in day_gui.meal_widgets.items(): old_state = meal_widget.blockSignals(True) meal = Meal() day.meals.append(meal) meal.meal_type = meal_type # print(meal.meal_type.name) model = meal_widget.model() i = 0 while i < model.rowCount(): item_data = model.itemData(model.index(i)) if len(item_data) == 1: s = item_data[0] regexp = re.compile( r'([0-9]+) [a-zA-Z]+ ([a-zA-Z0-9 ]+)') match = re.match(regexp, s) groups = match.groups() assert len(groups) == 2 quantity = int(groups[0]) food_name = groups[1] food = get_food_from_db(food_name) # if the food is derived we add its ingredient to the diet, instead of the food itself, # this is to make explicit what a derived food is made of, and also to allow for the # customization of its ingredients if food.name in [food.name for food in derived_foods]: assert quantity == 1 for component in food.ingredients: meal.foods.append(component) unit = component.unit.name if component.unit.name != 'item' else 'x' s = f'{component.quantity} {unit} {component.name}' item = QtWidgets.QListWidgetItem(s) meal_widget.insertItem(i + 1, item) i += 1 pass else: food.quantity = quantity meal.foods.append(food) # print(food.name) i += 1 i = 0 while i < model.rowCount(): item_data = model.itemData(model.index(i)) if len(item_data) == 1: s = item_data[0] regexp = re.compile( r'([0-9]+) [a-zA-Z]+ ([a-zA-Z0-9 ]+)') match = re.match(regexp, s) groups = match.groups() assert len(groups) == 2 quantity = int(groups[0]) food_name = groups[1] if food_name in [food.name for food in derived_foods]: assert quantity == 1 meal_widget.takeItem(i) break i += 1 # old_state = meal_widget.blockSignals(True) for i in range(meal_widget.count()): row = meal_widget.item(i) row.setFlags(row.flags() | PyQt5.QtCore.Qt.ItemIsEditable) meal_widget.blockSignals(old_state) self.update_diet_lock.release() @PyQt5.QtCore.pyqtSlot() def on_diet_changed(self): self.update_diet_from_widgets() self.diet.save() self.canvas.update_figure(self.diet)
ax1.axhline(400, ls='--', color=legend_colors[0]) ax1.axhline(150, ls='--', color=legend_colors[1]) ax1.axhline(80, ls='--', color=legend_colors[2]) def plot_nutrients_fragmentation(diet: Diet): fig, (ax0, ax1) = plt.subplots(2, 1, constrained_layout=True, figsize=(12, 5)) plot_into_axes(diet, ax0, ax1) plt.show() if __name__ == '__main__': diet = Diet('diet.json') shopping_list = generate_shopping_list(diet) print('\nshopping list:') for k, v in sorted(shopping_list.items()): print(f'{k}: {v}') print('') df = generate_daily_intake_information(diet) for row in df.iterrows(): print(row[0]) for k, v in row[1].items(): print(' ' * 4, end='') print(f'{k}: {round(v)}') plot_nutrients_fragmentation(diet)
def update_figure(self, diet): self.clear_canvas() # at least when testing, I prefer consistency over performance, that's why I reload diet diet = Diet('diet.json') plot_into_axes(diet, self.ax0, self.ax1, self.gray) self.draw()
def compute_initial_figure(self): diet = Diet('diet.json') plot_into_axes(diet, self.ax0, self.ax1, self.gray)