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 JSON

from recipy.microdata import recipe_to_json
from recipy.models import Recipe, IngredientGroup, InstructionGroup

recipe = Recipe(
    title="Tomato Basil Salad",
    description="A simple and fresh tomato basil salad.",
    ingredient_groups=[
        IngredientGroup(title="For the Salad",
                        ingredients=["2 ripe tomatoes, sliced", "1/4 cup fresh basil leaves, torn"]),
        IngredientGroup(title="For the Dressing",
                        ingredients=["2 tablespoons olive oil", "1 tablespoon balsamic vinegar"])
    ],
    instruction_groups=[
        InstructionGroup(title="Making the Salad", instructions=["Arrange the tomato slices on a plate.",
                                                                "Scatter the torn basil leaves over the tomatoes."]),
        InstructionGroup(title="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."])
    ]
)
json_data = recipe_to_json(recipe)
print(json_data)

Convert Recipe to PDF

from recipy.microdata import recipe_from_url
from recipy.pdf import recipe_to_pdf

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

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

Convert Recipe to LaTeX

from recipy.microdata import recipe_from_url
from recipy.latex import recipe_to_latex

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

if recipe:
    latex_content = recipe_to_latex(recipe)
    print(latex_content)

Recipe Model

from recipy.models import Recipe, IngredientGroup, InstructionGroup, Review, Meta, Rating

recipe = Recipe(
    title="Tomato Basil Salad",
    description="A simple, fresh salad perfect for summer.",
    ingredient_groups=[
        IngredientGroup(
            title="For the Salad",
            ingredients=[
                "2 ripe tomatoes, sliced",
                "1/4 cup fresh basil leaves, torn"
            ]
        ),
        IngredientGroup(
            title="For the Dressing",
            ingredients=[
                "2 tablespoons olive oil",
                "1 tablespoon balsamic vinegar"
            ]
        )
    ],
    instruction_groups=[
        InstructionGroup(
            title="Making the Salad",
            instructions=[
                "Arrange the tomato slices on a plate.",
                "Scatter the torn basil leaves over the tomatoes."
            ]
        ),
        InstructionGroup(
            title="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."
            ]
        )
    ],
    notes="Serve immediately for the best flavor.",
    reviews=[
        Review(
            author="Jane Doe",
            body="This salad is so fresh and delicious!",
            rating=4.5
        ),
        Review(
            author="John Smith",
            body="Simple yet tasty. I added some mozzarella for extra flavor.",
            rating=4.0
        )
    ],
    image_urls=[
        "https://example.com/tomato_basil_salad_small.jpg",
        "https://example.com/tomato_basil_salad_medium.jpg",
        "https://example.com/tomato_basil_salad_large.jpg"
    ],
    rating=Rating(value=4.3, count=28),
    meta=Meta(
        prep_time_minutes=10,
        cook_time_minutes=0,
        total_time_minutes=10,
        recipe_yield="2 servings"
    )
)

print(recipe.model_dump())

Supported Features by Format

JSON-LD Markdown LaTeX
Feature Input Output Input Output Output
Title
Description
Ingredient Groups
Ingredients
Instruction Groups
Instructions
Images
Rating
Reviews
Metadata
Notes

License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

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.3.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

python_recipy-0.1.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_recipy-0.1.3.tar.gz
  • Upload date:
  • Size: 13.4 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.3.tar.gz
Algorithm Hash digest
SHA256 3a3635bb6e19da4e815218c8d22d6b61cba2b243c91c6eb7c6a08d5eac5388fa
MD5 2ec1927dc319959dfc57a6dbdb9cae8b
BLAKE2b-256 a4a6c148f467ec8cd4e2dc63179b74bedd5ebc99c019b17caf3b4e3e66beea17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_recipy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c62ac8625f3ddf25a35af40baf91315d246fa4bee6f07c6f3ee3053a8de1c1d9
MD5 e1830ddfaf11daa24bd4781cb1b496b2
BLAKE2b-256 336b9bb813a93cc2bf3f6f45c88138a5abc5fc9b141e54e157fd6b12ddf71f7b

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