Creating a Command-Line Script to Add Recipes Manually

Introduction: The Need for Manual Recipe Entry

Welcome back! So far, you have set up your Flask 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 Python 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.

Quick Recall: Models and Database Access

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:

recipe_ingredient = db.Table('recipe_ingredient',
    db.Column('recipe_id', db.Integer, db.ForeignKey('recipe.id'), primary_key=True),
    db.Column('ingredient_id', db.Integer, db.ForeignKey('ingredient.id'), primary_key=True)
)

class Ingredient(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    recipes = db.relationship('Recipe', secondary=recipe_ingredient, back_populates='ingredients')

class Recipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    steps = db.Column(db.Text)
    ingredients = db.relationship('Ingredient', secondary=recipe_ingredient, back_populates='recipes')

You also learned how to use db.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.

Preparing the Script Environment

To create a script that can add recipes, you need to make sure it can access your Flask app and its database models. This means importing the right modules and setting up the app context.

Let’s start by importing the necessary modules and making sure our script can find the Flask app and models:

import os
import sys

# Add the path to the cooking_helper directory so we can import from it
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'cooking_helper')))

from main import create_app
from app.models import Recipe, Ingredient, db
  • os and sys are standard Python modules that help us work with file paths and system settings.
  • sys.path.insert(...) adds the cooking_helper directory to the Python path, so we can import our app and models.
  • We import create_app to set up the Flask app and import our Recipe, Ingredient, and db objects.

Next, we need to make sure our script runs inside the Flask app context. This is important because SQLAlchemy needs the app context to access the database:

app = create_app()
with app.app_context():
    # All database operations go here

By wrapping our code in with app.app_context():, we make sure all database actions work as expected.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal