Archive
Python: Cooking App P-6
Learning : Python, Data-Base, SQlite
Subject: Writing a Cooking Application
RECIPES MENU: Adding a Recipe.To Add a recipe we need to work on three tables, the user will not know that, the user dose’t care what happening in the application, he/she want the cooking information to be saved and retrieved whenever required.To do this we First will ask the user to Enter some basic information about the Recipe, such as Name, Date selecting it’s type like is it a Lunch or Dinner and other thing we will see it later while writing the code, then we need to enter the Ingredients of each Recipes, the Ingredients are store in another DB-Table based on our DB-design, so we will list down all the Ingredients and ask the user to select from the list by typing the ID’s of those they want, and if an ingredient does not exist in the list the user will write it.Finally we will ask the user to write the cooking steps.This is not easy way to do it using Terminal-screen or console (Command line application CLIs), we will convert this application to GUI (Graphical User Interface) later after we finish the application.
Now, let’s start with the basic information about the Recipes. In Basic Information we have:
Recipe Name: To give a Name to the Recipe.
Recipe Date: The user can Write a date.
Recipe Type: The user will select one of: [Breakfast, Dinner, Lunch, Snack, Sweet]
Recipe Details: Other Details such as the time needed for cooking, other notes.
Recipe Tags: Tags will not be available in this version, but we will work on it when we start doing the GUI version.
So let’s start writing the code..
# Adding New Recipe def add_recipe(): os.system("clear") print('\n ====== Adding New Recipe =====') print('\n First let''s Enter some Information about the Recipe..') r_name = input('\n\n Enter The Recipe Name: ').capitalize() r_date = input('\n\n Enter The Date in dd.mm.yyyy format: ') # Now to select the Recipe Type. print('\n A Recipe Type can be:\n 1.Breakfast.\n 2.Dinner.\n 3.Lunch.\n 4.Snack.\n 5.Sweet. ') r_type = False while r_type == False : rec_type = input('\n\n Select a Type [1,2,3,4,5]: ') if rec_type =='1' : rec_type ='Breakfast' r_type = True elif rec_type =='2' : rec_type ='Dinner' r_type = True elif rec_type =='3' : rec_type ='Lunch' r_type = True elif rec_type =='4' : rec_type ='Snack' r_type = True elif rec_type =='5' : rec_type ='Sweet' r_type = True else : print('\n You have to select from the List') r_details = input ('\n Enter Short information for the Recipe, like \n Time for cooking,\n Country of this Meal,\n Other short details.\n [ USE (,) to SEPARATE EACH INFO ]\n\n Enter here: ') c.execute ("INSERT INTO recipes (r_name, r_date, r_type, r_details ) VALUES(:r_name,:r_date,:r_type, :r_details)",{"r_name":r_name,"r_date":r_date,"r_type":rec_type, "r_details":r_details}) db_conn.commit()
Now, we save the Basic Recipe Information and we have the Recipe ID (PK) and we will use it as a Foreign Key in Recipe-Ingredient and Recipe-Steps. With Recipe-Ingredient we will list down all the recipes and let the user to enter the Id’s of the one’s they will use with there recipe. If the needed Ingredient is not in the list, the user will insert it and it will be added to the Ingredient list. Here is the code for this section. .
#This part of code continuing the above part. os.system("clear") # Select the Ingredient for the Recipe. print('\n Now, Select the Ingredients of your Recipe:') print("\n If the Ingredient is not in the List you will Add it shortly, FIRST select the exist ones\n write it''s ID Separated by ( , ) ") print('\n List of Ingredients:') list_of_ing() ing_l = input('\n Enter the Ingredients ID Seperated by ( , ) ') if input ('\n Is there any more Ingredients you want to add? [Y,N]: ') in ['y','Y'] : new_ing = input('\n Enter the Ingredients Seperated by ( , ): ') new_ing = new_ing.split(",") # To get max ingredients ID. c.execute ("select max(i_l_id) from ingredients_list") max_ing_id = c.fetchall()[0][0] ing_l2 =[] # To add the new ingredients to the ingredients_list . for each in range(len(new_ing)) : c.execute ("INSERT INTO ingredients_list (i_name) VALUES(:i_name)",{"i_name":new_ing[each].capitalize() })#ing_name}) db_conn.commit() ing_l2.append (str(max_ing_id+(each+1))) ing_l=ing_l.split(",") ing_l2=ing_l + ing_l2 # To link the ingredients to the recipe. for each in range (len(ing_l2)): c.execute ("INSERT INTO rec_ingredient (i_l_id, r_id) VALUES(:i_l_id, :r_id)",{"i_l_id":ing_l2[each],"r_id":rec_max }) db_conn.commit()
Now, we have the all Ingredients of the Recipe, also we have the ID or the Recipe, so we can link the Recipe information to the cooking Steeps. Here the user will write the cooking steps until the user press (Q,q) and Enter.
#This part of code continuing the above part. print('\n Now we can Write the Cooking Steps, Press Enter After each Step. When you finish just enter (q). ') stop = False while stop == False : c_step = input('\n Enter a Cooking Step: [Press Enter After each Step. q to Finish] :\n Write Here : ') if c_step not in ['q','Q'] : c.execute("INSERT Into recipes_steps (r_id , rec_steps) values (:r_id, :rec_steps)",{"r_id":rec_max,"rec_steps":c_step}) db_conn.commit() else : stop = True input('\n .. One Recipe Added .. Press any key .. ')
Here are some screen shots of the code and running out-put.
![]() |
![]() |
In Next Part: In the next coming part we will work on the Deleting Recipe.
To Download my Python code (.py) files Click-Here
By: Ali Radwani