mirror of
https://github.com/kennethreitz/simplemind.git
synced 2026-06-05 22:50:18 +00:00
Refactor recipe printing for better formatting and styling
This commit is contained in:
@@ -1,34 +1,68 @@
|
||||
from pydantic import BaseModel
|
||||
import simplemind as sm
|
||||
from _context import simplemind as sm
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
|
||||
|
||||
class InstructionStep(BaseModel):
|
||||
step_number: int
|
||||
instruction: str
|
||||
|
||||
|
||||
class RecipeIngredient(BaseModel):
|
||||
name: str
|
||||
quantity: float
|
||||
unit: str
|
||||
|
||||
|
||||
class Recipe(BaseModel):
|
||||
name: str
|
||||
ingredients: list[RecipeIngredient]
|
||||
instructions: list[InstructionStep]
|
||||
|
||||
|
||||
def __str__(self) -> str:
|
||||
output = f"\n=== {self.name.upper()} ===\n\n"
|
||||
|
||||
output += "INGREDIENTS:\n"
|
||||
console = Console(record=True, file=None)
|
||||
|
||||
# Create formatted title with more emphasis
|
||||
title = Text("✨ " + self.name.upper() + " ✨", style="bold blue")
|
||||
|
||||
# Format ingredients with better structure
|
||||
ingredients_text = Text("\n📝 INGREDIENTS:\n", style="bold green")
|
||||
for ing in self.ingredients:
|
||||
output += f"• {ing.quantity} {ing.unit} {ing.name}\n"
|
||||
|
||||
output += "\nINSTRUCTIONS:\n"
|
||||
# Format numbers to avoid floating decimals when whole numbers
|
||||
quantity = int(ing.quantity) if ing.quantity.is_integer() else ing.quantity
|
||||
ingredients_text.append(f" • {quantity} {ing.unit} ", style="bright_white")
|
||||
ingredients_text.append(f"{ing.name}\n", style="italic bright_white")
|
||||
|
||||
# Format instructions with better spacing and styling
|
||||
instructions_text = Text("\n👩🍳 INSTRUCTIONS:\n", style="bold yellow")
|
||||
for step in self.instructions:
|
||||
output += f"{step.step_number}. {step.instruction}\n"
|
||||
|
||||
return output
|
||||
|
||||
instructions_text.append(
|
||||
f"\n {step.step_number}. ", style="bold bright_white"
|
||||
)
|
||||
instructions_text.append(f"{step.instruction}", style="bright_white")
|
||||
|
||||
# Combine all text
|
||||
full_text = Text.assemble(
|
||||
ingredients_text, instructions_text, "\n"
|
||||
) # Added extra newline
|
||||
|
||||
# Create panel with enhanced styling
|
||||
panel = Panel(
|
||||
full_text,
|
||||
title=title,
|
||||
border_style="blue",
|
||||
padding=(1, 2), # Add padding (vertical, horizontal)
|
||||
expand=False, # Don't expand to full terminal width
|
||||
title_align="center",
|
||||
)
|
||||
|
||||
# Render the panel to string without printing
|
||||
with console.capture() as capture:
|
||||
console.print(panel)
|
||||
return capture.get()
|
||||
|
||||
|
||||
recipe = sm.generate_data(
|
||||
"Write a recipe for chocolate chip cookies",
|
||||
@@ -63,4 +97,3 @@ print(recipe)
|
||||
# 7. Drop by rounded tablespoon onto ungreased cookie sheets.
|
||||
# 8. Bake for 9 to 11 minutes, or until edges are golden.
|
||||
# 9. Let cool on the cookie sheet for a few minutes before transferring to wire racks to cool completely.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user