Cooking Up a Website with Python, Spreadsheets, and APIs
In 2022, Sahil and I started BCBCBath, a cookbook club in Bangalore, as a way to connect with fellow food lovers and explore diverse cuisines. Each month, members vote on a cookbook, cook a dish from it at home, and gather for a themed potluck. Initially, I was updating the menu manually with Obsidian to publish it online. As the club grew, manually updating the website became time-consuming, so I decided to automate the process using Python.
Here’s how the automation works:
- BCBCBath members track potluck details like date, cookbook title, attendees, and dishes in a Google Spreadsheet. Each potluck gets its own sheet.
- The Python script uses the
requests
library to fetch the spreadsheet data as CSV. It parses the CSV data using thecsv
module and extracts relevant information for each potluck. - To get additional cookbook details, the script utilizes the
amazon_paapi
library to interact with the Amazon Product Advertising API. It fetches the cover image, author name, and purchase link for each cookbook. - The extracted data is used to create instances of a custom
PotluckMetadata
class, which serves as a structured representation of each potluck’s information. - The script then generates an HTML page for each potluck using the Jinja templating engine. It passes the
PotluckMetadata
instances to predefined Jinja templates, which render the content into HTML. - To create paginated index pages, the script divides the potlucks into chunks based on a configurable
potlucks_per_page
value. It generates an index page for each chunk, displaying a grid of potluck cards with cover images and dates. - The generated HTML files are written to the website directory
- The script logs its progress and any errors using the
logging
module, making it easier to diagnose issues. - Configuration variables like API credentials, output paths, and website metadata are stored in a separate
config.py
file, promoting code reusability and maintainability. - A
main()
function orchestrates the entire process, handling the flow of data retrieval, HTML generation, and error handling.
With this automated setup, updating the BCBCBath website is as simple as having members edit the spreadsheet and running the Python script. The script fetches the latest data, generates fresh HTML pages, and updates the website in minutes.
Building this automation script was a great opportunity to leverage Python’s rich ecosystem and practice working with libraries like requests
, csv
, jinja2
, and amazon_paapi
.
I’m sure there are better ways to execute this. Happy to chat @unitechy