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

  • Ubuntu 24.04
sudo apt update
sudo apt install texlive
  • Fedora 40
sudo dnf install texlive
  • Arch Linux
sudo pacman -S texlive-most

Examples

Load Recipe from JSON

from recipy.microdata import recipe_from_json

json_data = '''
{
    "name": "Chocolate Cake",
    "recipeIngredient": ["2 cups flour", "1 cup sugar", "1/2 cup cocoa powder"],
    "recipeInstructions": [
        {"@type": "HowToStep", "text": "Preheat the oven to 350°F (175°C)."},
        {"@type": "HowToStep", "text": "Mix dry ingredients."},
        {"@type": "HowToStep", "text": "Add wet ingredients and mix until smooth."},
        {"@type": "HowToStep", "text": "Pour batter into a greased pan and bake for 30 minutes."}
    ]
}
'''

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 = """
# Chocolate Cake

A simple and delicious chocolate cake recipe.

## Ingredients

### For the Cake

* 2 cups flour
* 1 cup sugar
* 1/2 cup cocoa powder

### For the Frosting

* 1/2 cup butter
* 1/4 cup cocoa powder
* 2 cups powdered sugar

## Instructions

### Making the Cake

1. Preheat the oven to 350°F (175°C).
2. Mix dry ingredients.
3. Add wet ingredients and mix until smooth.
4. Pour batter into a greased pan and bake for 30 minutes.

### Making the Frosting

1. Melt the butter.
2. Stir in cocoa powder.
3. Gradually add powdered sugar until smooth.

## Notes

Let the cake cool completely before frosting.
"""

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="Chocolate Cake",
    ingredient_groups=[
        IngredientGroup(name=None, ingredients=["2 cups flour", "1 cup sugar"])
    ],
    instruction_groups=[
        InstructionGroup(name=None, instructions=["Mix ingredients", "Bake for 30 minutes"])
    ]
)
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="Chocolate Cake",
    ingredient_groups=[
        IngredientGroup(name=None, ingredients=["2 cups flour", "1 cup sugar"])
    ],
    instruction_groups=[
        InstructionGroup(name=None, instructions=["Mix ingredients", "Bake for 30 minutes"])
    ]
)

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_recipy-0.1.1.tar.gz
  • Upload date:
  • Size: 11.1 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.1.tar.gz
Algorithm Hash digest
SHA256 ca965e6c48cae41f12682e4d7e6ec132646360314d6d9af3a7de966dcc03d89c
MD5 28059214821c761a0ff00aaf606d035a
BLAKE2b-256 2fda2949f3441fa3deacd8d522240e43a8858eb37c32d018241b5d4af3df1cb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_recipy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54e88e9c605fed487742ec651c74345cd3a2d7161ee0daf4bc8913ac83b36064
MD5 d29c4cd04444f1262d050e51b604ed03
BLAKE2b-256 7584dcd57abaaff89dabe2ea39eae6bfbab078ab2b47d09fcc6c3184c35c8742

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