Skip to main content

Extract recipes from web pages that use JSON-LD structured data and output Markdown, LaTeX, and PDF.

Project description

Recipy

Recipy extracts recipes from web pages using JSON-LD and converts them into Python objects. It also supports generating Markdown, LaTeX, and PDFs.

from recipy.microdata import recipe_from_url

url = "https://www.allrecipes.com/recipe/14231/guacamole/"
recipe = recipe_from_url(url)
if recipe:
    print(recipe.model_dump())

Installation

Install via pip

pip install python-recipy

Install texlive for PDF Generation

Debian/Ubuntu

sudo apt install texlive

macOS

brew install texlive

Examples

Load Recipe from JSON

from recipy.microdata import recipe_from_json

json_data = '''
{
    "name": "Tomato Basil Salad",
    "recipeIngredient": ["2 ripe tomatoes, sliced", "1/4 cup fresh basil leaves, torn"],
    "recipeInstructions": [
        {
            "@type": "HowToSection",
            "name": "Making the Salad",
            "itemListElement": [
                {"@type": "HowToStep", "text": "Arrange the tomato slices on a plate."},
                {"@type": "HowToStep", "text": "Scatter the torn basil leaves over the tomatoes."}
            ]
        },
        {
            "@type": "HowToSection",
            "name": "Preparing the Dressing",
            "itemListElement": [
                {"@type": "HowToStep", "text": "In a small bowl, whisk together the olive oil and balsamic vinegar."},
                {"@type": "HowToStep", "text": "Drizzle the dressing over the tomatoes and basil before serving."}
            ]
        }
    ]
}
'''

recipe = recipe_from_json(json_data)
if recipe:
    print(recipe.model_dump())

Parse Recipe from Markdown

from recipy.markdown import recipe_from_markdown

markdown_content = """
# Tomato Basil Salad

A simple and fresh tomato basil salad.

## Ingredients

### For the Salad

* 2 ripe tomatoes, sliced
* 1/4 cup fresh basil leaves, torn

### For the Dressing

* 2 tablespoons olive oil
* 1 tablespoon balsamic vinegar

## Instructions

### Making the Salad

1. Arrange the tomato slices on a plate.
2. Scatter the torn basil leaves over the tomatoes.

### Preparing the Dressing

1. In a small bowl, whisk together the olive oil and balsamic vinegar.
2. Drizzle the dressing over the tomatoes and basil before serving.

## Notes

Serve immediately for the best flavor.
"""

recipe = recipe_from_markdown(markdown_content)
if recipe:
    print(recipe.model_dump())

Markdown Structure

  • The recipe title must be an H1 (# Title).
  • Ingredients must be under an H2 heading ## Ingredients, with optional H3 subheadings for ingredient groups.
  • Instructions must be under an H2 heading ## Instructions, with optional H3 subheadings for instruction groups.
  • Notes can be included under an H2 heading ## Notes.

Convert Recipe to PDF

from recipy.pdf import recipe_to_pdf
from recipy.models import Recipe, IngredientGroup, InstructionGroup

recipe = Recipe(
    title="Tomato Basil Salad",
    description="A simple and fresh tomato basil salad.",
    ingredient_groups=[
        IngredientGroup(name="For the Salad", ingredients=["2 ripe tomatoes, sliced", "1/4 cup fresh basil leaves, torn"]),
        IngredientGroup(name="For the Dressing", ingredients=["2 tablespoons olive oil", "1 tablespoon balsamic vinegar"])
    ],
    instruction_groups=[
        InstructionGroup(name="Making the Salad", instructions=["Arrange the tomato slices on a plate.", "Scatter the torn basil leaves over the tomatoes."]),
        InstructionGroup(name="Preparing the Dressing", instructions=["In a small bowl, whisk together the olive oil and balsamic vinegar.", "Drizzle the dressing over the tomatoes and basil before serving."])
    ]
)

pdf_content = recipe_to_pdf(recipe)
with open("recipe.pdf", "wb") as f:
    f.write(pdf_content)

Convert Recipe to LaTeX

from recipy.latex import recipe_to_latex
from recipy.models import Recipe, IngredientGroup, InstructionGroup

recipe = Recipe(
    title="Tomato Basil Salad",
    description="A simple and fresh tomato basil salad.",
    ingredient_groups=[
        IngredientGroup(name="For the Salad", ingredients=["2 ripe tomatoes, sliced", "1/4 cup fresh basil leaves, torn"]),
        InstructionGroup(name="For the Dressing", ingredients=["2 tablespoons olive oil", "1 tablespoon balsamic vinegar"])
    ],
    instruction_groups=[
        InstructionGroup(name="Making the Salad", instructions=["Arrange the tomato slices on a plate.", "Scatter the torn basil leaves over the tomatoes."]),
        InstructionGroup(name="Preparing the Dressing", instructions=["In a small bowl, whisk together the olive oil and balsamic vinegar.", "Drizzle the dressing over the tomatoes and basil before serving."])
    ]
)

latex_content = recipe_to_latex(recipe)
print(latex_content)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_recipy-0.1.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

python_recipy-0.1.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file python_recipy-0.1.2.tar.gz.

File metadata

  • Download URL: python_recipy-0.1.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for python_recipy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f5b8118178695d67f1bb94b12a2b505987bd4daa5e2e924048f893b8720206d9
MD5 45003a4669eab0fc090efb01020163cd
BLAKE2b-256 2c8417cf5eed483eb6c49f5d41b7ddfc4a7004b7bfc8967c12dbc5f84e4199a2

See more details on using hashes here.

File details

Details for the file python_recipy-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for python_recipy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 103bc622362b7f02272c71b4e3af4907def89369ebaa83190daa599dc8a6da2d
MD5 de9ec0ad87a758ef292ec89085224179
BLAKE2b-256 60ddfde48786e88f6b074b0a12175b01eab5f6ad2faef2e5e89268cf1409e03f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page