Welcome back! So far, you have set up your app, designed your database models, and learned how to safely reset your database. Now, let’s take the next step: adding recipes to your app.
While it’s possible to add recipes automatically or through an API, sometimes you need a simple way to enter recipes by hand. This is especially useful for testing, quick data entry, or when you want to add a recipe that you found or created yourself. In this lesson, you’ll learn how to build a script that lets you add recipes directly from the command line. This script will prompt you for the recipe name, ingredients, and steps, then save everything to your database.
By the end of this lesson, you’ll be able to run a script, enter a new recipe, and see it appear in your app’s database. This is a practical tool that will help you test and grow your cooking app.
Before we dive in, let’s quickly remind ourselves of the key models and how we interact with the database. You’ve already created Recipe and Ingredient models using SQLAlchemy. These models are connected by a many-to-many relationship, which means a recipe can have many ingredients, and an ingredient can belong to many recipes.
Here’s a quick reminder of what these models look like:
You also learned how to use a session to add and commit changes to your database. In this lesson, we’ll use these same models and session methods to save new recipes and ingredients.
To create a script that can add recipes, you need to make sure it can access your app and its database models. This means importing the right modules and setting up the environment.
Let’s start by importing the necessary modules and making sure our script can find the models and database session:
osandsysare standard modules that help you work with file paths and system settings.- The script adjusts the import path so it can access the project’s modules.
- The session and models are imported so the script can interact with the database.
When you want to perform database operations, you create a session using SessionLocal() and close it when done.
Now, let’s build the part of the script that asks the user for recipe details. We want to collect three things: the recipe name, a list of ingredients, and the steps.
We start by asking for the recipe name:
- The script displays a prompt and waits for the user to type something.
.strip()removes any extra spaces from the beginning or end.- If the user doesn’t enter a name, an error is printed and the script stops.
Next, we ask for ingredients, one per line. The user can press Enter on an empty line to finish:
- The script uses a loop to keep asking for ingredients until the user enters a blank line.
- Each ingredient is added to a list.
- If the list is empty, an error is printed and the script stops.
Finally, we ask for the steps. The user can type as many lines as they want and type END on a line by itself to finish:
- This function collects each line of the steps until the user types
END. - All lines are joined together into a single string.
Here’s what it looks like when you run this part of the script:
Now that we have the recipe details, let’s save them to the database. We need to:
- Create a new
Recipeobject. - For each ingredient, check if it already exists. If not, create it.
- Link each ingredient to the recipe.
- Add the recipe to the session and commit.
Here’s how we do it:
Let’s break this down:
- A session is created to interact with the database.
- The script checks if a recipe with the same name already exists (case-insensitive).
- If it exists, a warning is printed and the script stops.
- A new
Recipeobject is created with the name and steps. - For each ingredient name:
- The name is cleaned up with
.strip().lower()to avoid duplicates like "Sugar" and "sugar." - The script checks if the ingredient already exists in the database.
- If it doesn’t exist, a new
Ingredientis created and added to the session. - The ingredient is added to the recipe’s
ingredientslist.
- The name is cleaned up with
If everything works, you’ll see:
In this lesson, you learned how to build a script that lets you add recipes manually to your cooking app’s database. You saw how to:
- Set up your script to access the app’s database models
- Prompt the user for recipe details and validate their input
- Create or reuse ingredients and link them to a new recipe
- Save everything to the database and confirm success
This script is a practical tool for quickly adding recipes, testing your app, or entering your own creations. Now that you understand how it works, you’re ready to try it yourself in the practice exercises. Focus on running the script, entering different recipes, and checking your database to see the results. This hands-on practice will help you get comfortable with scripting and database operations in your app. Good luck!
