- CodeCraft by Dr. Christine Lee
- Posts
- Cook Up Code: DIY Recipe Engine! 🍲💻
Cook Up Code: DIY Recipe Engine! 🍲💻
Build Your Own Recipe Recommendation Engine with PySimpleGUI!
Recipe Recommendation System
Ready to create something super useful and fun?
In this post, we’re going to walk you through building a Recipe Recommendation Engine using PySimpleGUI!
This app will suggest recipes based on the ingredients you have on hand, and it can even consider dietary preferences.
Perfect for beginners, this step-by-step guide will make sure you understand each part of the code and feel confident in creating your own awesome app.
Step 1: Setting Up Your Environment
Before we start coding, you’ll need to have Python and PySimpleGUI installed on your computer.
If you haven’t installed PySimpleGUI yet, you can do so by running:
pip install PySimpleGUI
Step 2: Planning the Recipe Recommendation Flow
Our Recipe Recommendation Engine will follow a simple yet effective flow:
Ingredient Input: Users will enter the ingredients they have on hand.
Recipe Evaluation: The app will check which recipes can be made with the available ingredients.
Recommendation: The app will display the best recipes based on the ingredients and any dietary preferences.
Step 3: Creating the User Interface
Let’s start by creating the interface where users can enter their ingredients.
Here’s the code to set up the basic layout:
import PySimpleGUI as sg
# Sample data: Recipes and their required ingredients
recipes = {
"Pasta Primavera": ["pasta", "tomato", "zucchini", "garlic"],
"Fruit Salad": ["apple", "banana", "orange", "grapes"],
"Omelette": ["eggs", "cheese", "onion", "pepper"],
"Guacamole": ["avocado", "lime", "onion", "tomato"]
}
# Layout for the GUI
layout = [
[sg.Text('Enter the ingredients you have:')],
[sg.Input(key='Ingredients')],
[sg.Button('Recommend Recipes'), sg.Button('Exit')],
[sg.Text('Recommended Recipes:'), sg.Listbox(values=[], size=(40, 10), key='Recommendations')]
]
# Create the Window
window = sg.Window('Recipe Recommendation Engine', layout)
# Event loop to process "events"
while True:
event, values = window.read()
if event sg.WIN_CLOSED or event 'Exit':
break
if event == 'Recommend Recipes':
user_ingredients = values['Ingredients'].lower().split(',')
user_ingredients = [ingredient.strip() for ingredient in user_ingredients]
# Find matching recipes
recommended = []
for recipe, ingredients in recipes.items():
if all(ingredient in user_ingredients for ingredient in ingredients):
recommended.append(recipe)
window['Recommendations'].update(recommended)
window.close()
Step 4: Explaining the Code
Let’s break down what’s happening in the code:
recipes dictionary:
This holds some sample recipes and the ingredients required to make them.
layout:
This defines the structure of the GUI. We have an input field for the user to enter their ingredients, a button to recommend recipes, and a listbox to display the recommended recipes.
window and event loop:
The event loop waits for the user to interact with the app. When the “Recommend Recipes” button is clicked, the app checks which recipes can be made with the given ingredients and updates the recommendations listbox.
Step 5: Enhancing the Logic
Right now, our app only recommends recipes if you have all the necessary ingredients.
Let’s enhance it to suggest recipes even if you’re missing one or two ingredients, so it’s more forgiving:
# Enhanced recipe recommendation logic
if event == 'Recommend Recipes':
user_ingredients = values['Ingredients'].lower().split(',')
user_ingredients = [ingredient.strip() for ingredient in user_ingredients]
# Find matching recipes
recommended = []
for recipe, ingredients in recipes.items():
missing = [ingredient for ingredient in ingredients if ingredient not in user_ingredients]
if len(missing) <= 1: # Allow one missing ingredient
recommended.append(f"{recipe} (Missing: {', '.join(missing)})" if missing else recipe)
window['Recommendations'].update(recommended)
Step 6: Adding Dietary Preferences (Optional)
You can further customise the app by allowing users to select dietary preferences like vegetarian, vegan, or gluten-free.
Add a dropdown menu for dietary preferences, filter the recipes accordingly, and you’ll have a more personalised experience.
Wrapping Up!
In this post, you've learned how to create a Recipe Recommendation Engine using PySimpleGUI, from setting up the user interface to implementing the logic for recipe matching.
You’ve also enhanced the app to be more flexible and beginner-friendly.
Coding with a Smile 🤣 😂
List Length Learning:
Figuring out that lists start at index 0 can feel like discovering the Earth isn’t flat. It’s a fundamental shift that makes everything suddenly make sense.
Recommended Resources 📚
Learn more from a security-focused AI enthusiast who shares a constant flow of unique insights, analysis, tools, and strategies on how to create a successful and meaningful life in an AI-driven world.
Let’s Inspire Future AI Coders Together! ☕
I’m excited to continue sharing my passion for Python programming and AI with you all. If you’ve enjoyed the content and found it helpful, do consider supporting my work with a small gift. Just click the link below to make a difference – it’s quick, easy, and every bit helps and motivates me to keep creating awesome contents for you.
Thank you for being amazing!
What’s Next? 📅
In the next post, we’ll explore advanced features like saving favorite recipes, filtering by dietary needs, and even adding images of the dishes to make your app even more engaging.
Stay tuned, and happy coding! 👩🍳👨🍳
Ready for More Python Fun? 📬
Subscribe to our newsletter now and get a free Python cheat sheet! 📑 Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep learning, keep coding 👩💻👨💻, and keep discovering new possibilities! 💻✨
Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!
Stay tuned for our next exciting project in the following edition!
Happy coding!🚀📊✨
🎉 We want to hear from you! 🎉 How do you feel about our latest newsletter? Your feedback will help us make it even more awesome! |