Python: Cooking App P-4
Learning : Python, Data-Base, SQlite
Subject: Writing a Cooking Application
INGREDIENT MENU: In this post we will write the Python code for the Ingredient Menu Functions. We have four main Functions :
1. Show all Ingredient.
2. Add Ingredient.
3. Delete Ingredient.
4. Edit Ingredient.
We need the Ingredient List so the user can select an Ingredient for the Recipes they want to write. First we will list down all the Ingredient in the Table.
show_ingredient is a function to list all the ingredient in “ingredients_list” Table, here is the code ..
# Show Ingredient Function def show_ingredient(): os.system("clear") print('\n ====== Show All Ingredient =====') show_sql = c.execute('select * from ingredients_list where i_l_id > 0 order by i_name') for each in c.fetchall() : print (' ',each[0],'....',each[1]) input('\n .. Press any key .. ')
add_ingredient Function will let the user to add more Ingredients to the list.
# Add New Ingredients Function def add_ingredient(): os.system("clear") print('\n ====== Adding New Ingredient =====') ing_name = input('\n\n Enter an Ingredient you want to add: ').capitalize() c.execute ("INSERT INTO ingredients_list (i_name) VALUES(:i_name)",{"i_name":ing_name}) db_conn.commit() input('\n .. One Ingredient Added .. Press any key .. ')
del_ingredient To Delete an Ingredients we need to enter it’s ID, and If we Delete one and that one was used in any recipes, then once we view the recipe the Ingredients will not show there.
# To Delete an Ingredient def del_ingredient(): os.system("clear") print('\n ====== Delete an Ingredient =====\n') c.execute('select * from ingredients_list where i_l_id > 0 order by i_name') for each in c.fetchall() : print (' ',each[0],'....',each[1]) ing_del = input('\n\n Enter an Ingredient ID to Delete: ') if ing_del.isnumeric(): sure_remove = input('\n Please Note that if you Delete this Ingredient it will not show in any recipes that use it.\n Are you sure you want to Remove it [Y,N] ') if sure_remove in ['y','Y']: c.execute ("Delete from ingredients_list where i_l_id = {}".format(ing_del)) db_conn.commit() elif sure_remove in ['n','N']: print('\n You select NOT to remove the Ingredient.') else : print('\n You must select (Y or N).') input('\n .. One Ingredient Deleted .. Press any key .. ') else: print('\n You must Enter a Numeric Ingredient ID.') input('\n .. Press any key .. ')
![]()
edit_ingredient To Edit an Ingredients we need to enter it’s ID, so first we will list down all Ingredients we have then the user will select the one to be edit. Here is the code to do so..
# To Edit an Ingredients. def edit_ingredient(): os.system("clear") print('\n ====== Edit an Ingredient =====') print('\n\n :: List of Ingredients.\n') c.execute('select * from ingredients_list where i_l_id > 0 order by i_name') for each in c.fetchall() : print (' ',each[0],'....',each[1]) ing_edit = input('\n\n Enter an Ingredient ID you want to Change: ') if ing_edit.isnumeric(): c.execute('select i_name from ingredients_list where i_l_id = {}'.format(ing_edit)) print(' You select to change the: ',c.fetchall()) new_ing = input('\n Enter the New Update for it: ') c.execute("update ingredients_list set i_name = '{}' where i_l_id = {}".format(new_ing.capitalize(),int(ing_edit))) db_conn.commit() input('\n .. One Ingredient Updated .. Press any key .. ') else: print('\n You must Enter a Numeric Ingredient ID.') input('\n .. Press any key .. ')
Last thing we will Insert some Ingredients in the “ingredients_list” Table so we can test the functions, for this purpose I will write this piece of code that we can run to Insert some Ingredients.
# To Insert sample ing_list ing_list =["Onion","Spinach","Mushroom","Tomatoes","Lime","Turnip","Snake Beans", "Jalapeno","Baking powder","sugar","Eggs","Vanilla extract","Salt","Flour","Butter", "Oil","Whole milk","Orange juice","Baking soda","Ground cinnamon","Dark chocolate", "Cocoa","Cherry","Cherry jam"] for item in range (len(ing_list)): ing_name = ing_list[item].capitalize() c.execute ("INSERT INTO ingredients_list (i_name) VALUES(:i_name)",{"i_name":ing_name}) db_conn.commit()
Now, we can use the Delete, Edit and Add functions to test the “Ingredients Manager” from the Main Menu, here is some screen shots.
Show Ingredients ![]() |
Add Ingredients![]() |
Delete Ingredients![]() |
Edit Ingredients ![]() |
Note: The Screen-Shots above was before I add Order by clause to the SQL statement.
In Next post we will write the codes for the functions in the ” Recipe Menu”.
To Download my Python code (.py) files Click-Here
By: Ali Radwani