Week 3 of 16

Prompt Library (File-Based)

Your data finally survives between runs — meet JSON persistence.

Day 13 60 minutes Build

Day 13 of 80

What You'll Build

Every script you've written so far has the same problem: close the terminal and your data is gone. Today you fix that.

You'll build a Prompt Library — a Python script that reads from and writes to a prompts.json file. Every shot description and prompt you save will still be there the next time you run the script.

Why a JSON File?

JSON (JavaScript Object Notation) is a plain-text format that Python can read and write natively using the built-in json module. It stores Python dictionaries and lists as human-readable text — you can open the file in VS Code and read it like a document. It's the simplest possible persistent storage, and it's exactly what you need right now.

By the end of today you'll have two files:

Step 1: Create prompts.json

Start by creating the data file by hand so you have something to load on the first run. Create a new file called prompts.json in the same folder as your script and paste in this starter content:

prompts.json JSON
[
  {
    "platform": "Kling",
    "shot": "Golf swing follow-through",
    "prompt": "Slow-motion golf swing, camera tracks club head through impact, golden hour lighting, cinematic depth of field, 4K"
  },
  {
    "platform": "Runway",
    "shot": "Ocean aerial",
    "prompt": "Aerial drone shot over turquoise ocean, waves breaking on white sand, sunrise, ultra-wide lens, smooth movement"
  }
]
JSON Rules to Know

Step 2: Build prompt_manager.py

Now create a new file called prompt_manager.py in the same folder. Build it section by section using the annotated code below.

The Complete Script

prompt_manager.py Python
# json is a built-in Python module — no pip install needed
import json

# Store the filename in a constant so it's easy to change later
PROMPTS_FILE = "prompts.json"

# ── Load existing prompts from disk ──────────────────────────────────
# We wrap this in try/except because the file might not exist yet
# (e.g., the very first time someone runs the script)
# If the file isn't found, we simply start with an empty list
try:
    with open(PROMPTS_FILE, "r") as f:
        # json.load() reads the file and converts JSON → Python objects
        # Our JSON array becomes a Python list of dictionaries
        prompts = json.load(f)
except FileNotFoundError:
    # File doesn't exist yet — that's fine, start fresh
    prompts = []

# Tell the user how many prompts are saved right now
print(f"You have {len(prompts)} prompts saved.\n")

# ── Show existing prompts ─────────────────────────────────────────────
# enumerate() gives us both the index (i) and the item (p) on each loop
# We add 1 to i so the list shows 1, 2, 3 instead of 0, 1, 2
for i, p in enumerate(prompts):
    print(f"  {i + 1}. [{p['platform']}] {p['shot']}")

# ── Add a new prompt ─────────────────────────────────────────────────
# input() pauses the script and waits for the user to type something
print("\n--- Add a new prompt ---")
platform = input("Platform (Kling / Runway / Veo): ")
shot     = input("Shot description: ")
prompt   = input("Full prompt text: ")

# Build a dictionary with the three fields, matching our JSON structure
new_prompt = {"platform": platform, "shot": shot, "prompt": prompt}

# .append() adds the new dictionary to the end of our list (in memory)
prompts.append(new_prompt)

# ── Save back to file ─────────────────────────────────────────────────
# "w" mode opens the file for writing — it creates the file if it
# doesn't exist, or OVERWRITES it if it does.
# json.dump() converts Python objects → JSON text and writes to the file
# indent=2 makes the output nicely formatted with 2-space indentation
with open(PROMPTS_FILE, "w") as f:
    json.dump(prompts, f, indent=2)

print(f"\nSaved! You now have {len(prompts)} prompts.")

Key Concepts in This Script

Concept What It Does Example
open(file, "r") Opens a file for reading open("prompts.json", "r")
open(file, "w") Opens a file for writing (overwrites) open("prompts.json", "w")
json.load(f) Reads JSON from a file → Python object Returns a list of dicts
json.dump(data, f) Writes Python object → JSON file Saves the updated list
enumerate(list) Loops with both index and value for i, p in enumerate(prompts)
list.append(item) Adds an item to the end of a list prompts.append(new_prompt)

Run It

Open your terminal, navigate to the folder containing both files, and run:

Terminal
$ python prompt_manager.py

You have 2 prompts saved.

  1. [Kling] Golf swing follow-through
  2. [Runway] Ocean aerial

--- Add a new prompt ---
Platform (Kling / Runway / Veo): Veo
Shot description: City timelapse at night
Full prompt text: City traffic timelapse, light trails, skyscrapers, neon reflections on wet pavement, overhead angle

Saved! You now have 3 prompts.

Run the script a second time — you'll see "You have 3 prompts saved." The new prompt persisted.

Verify Persistence Yourself

After running the script, open prompts.json in VS Code. You'll see your new prompt is actually there — not in memory, not in a variable, but written as text in a file on your hard drive. This is exactly what a database does, just with a simpler file format. Everything from SQLite to PostgreSQL is doing this same fundamental operation: serializing data to disk so it survives between program runs.

What If It Goes Wrong?

Common Errors

End of Day Checklist

Tomorrow

Tomorrow and the day after, you turn this into a full menu-driven tool with search, delete, and export. Instead of just running straight through, the script will loop and offer a numbered menu so you can manage your library interactively.