Cocktail Database using CocktailDB, Templater and Python


I want to show forth how I made and manage a cocktail database using CocktailDB, Templater and Python.

The way I set it up does not allow for use of it without python, it could have been used using QuickAdd and JS, but I did not know how to manage that.

Shout out to

So how do you achieve this?


You need Python and Templater plugin. In python you need to have the following packages installed.

  • requests
  • json
  • pyperclip
  • sys

You start with this simple python code and the reason why I did this in Python, for the request function.

# imports
import requests
import json
import pyperclip
import sys

# Argument tree
if sys.argv[1].startswith('1'):
    r = requests.get("")
    r = requests.get("" + sys.argv[1])

r_json = r.json()["drinks"]

# Some of the other key-value pairs from the TheCocktailDB query I don't want.
# Just comment, uncomment or and more if you want.
remove = ["strDrinkAlternate", 
        #   'strCategory', 

for drink in r_json:
    i = 1

    # Removes the key-value pairs
    drink["strDrink"] = drink["strDrink"].replace("'", "")
    drink["strInstructions"] = drink["strInstructions"].replace("'", "")

    for rem in remove:
        del drink[rem]

    # Removes the extra ingredient key-value pairs
    while i <= 15:
        if drink[f"strIngredient{i}"] == None:
            del drink[f"strIngredient{i}"]
            del drink[f"strMeasure{i}"]

        i += 1
# Converts to json text
json_text = json.dumps(r_json)

# Puts the json into your clipboard

Since the infomation has been put into the clipboard, we can continue onward into Templater, but before you need to setup some settings in Templater to make this work.

You need to add a User Function in Templater, in the settings tab.

You will need to replace the "40 - Obsidian/Scripts/" with the path to the code, and replace py to your devices, python startup. Go in the terminal and type py, py3 or others and check if python pops up.


We need to set up the Templater script.

The code is real messy

const drinks = await tp.system.suggester(["Search:", "Random"], [0,1], false, "What do you choose");
if (drinks === null) {
  // User cancelled the suggestion dialog without making a selection. Handle appropriately.
} else if (drinks === 1) {
  await tp.user.cocktail({rand_drink: drinks});
} else {
  const search = await tp.system.prompt("What cocktail you want to search for?", null, false, false);
  await tp.user.cocktail({rand_drink: search});
let text = await tp.system.clipboard();

const obj = JSON.parse(text); 
var arr_name = [];
let ingredients = "";
let ing = "";
let mes = "";
for (let i = 0; i < obj.length; i++) {
const selectedKey = await tp.system.suggester(arr_name, obj, false, "Select a cocktail: ");
if (selectedKey === null) {
  // User cancelled the suggestion dialog without making a selection. Handle appropriately.
} else {
  if (await tp.file.exists("/60 - Food & Drinks/Cocktails/" + selectedKey.strDrink) === true) {
  } else {
    await tp.file.move("/60 - Food & Drinks/Cocktails/" + selectedKey.strDrink);
    for (let i = 1; i <= 15; i++) { 
      // assuming a maximum of 15 ingredients/measures as seen in similar APIs 
      const ingredient = selectedKey[`strIngredient${i}`]; 
      const measure = selectedKey[`strMeasure${i}`]; 
      if (ingredient) { 
        ingredients += `- [x] ${ingredient}, ${measure || ''}\n`; 
        ing += `- "[[${ingredient}]]"\n`
        mes += `- "${measure}"\n`

tags:  drink/cocktail 
image:  <% selectedKey.strDrinkThumb %> 
<% await ing %>
<% await mes %>
source: TheCocktailDB
howTo: <% selectedKey.strInstructions %>


# ``
![](<% selectedKey.strDrinkThumb %>)


 > [!tip] Tips
 > Placeholder

## Ingredients
- Glass
	-  <% selectedKey.strGlass %> 

<% await ingredients %>


## Steps:
<% selectedKey.strInstructions %>

This should be easy if you have used Templater before. Add this into your template folder and use the Templater: Create new note from template function to make a new note.


Here is the note and code for the dataview for the beginning images, note that you need to have the correct css, to make it look the same.

  - drink/homepage
  - cards
banner: ""
banner_y: 0.45
# Cocktails

let pages = dv.pages('#drink/cocktail AND -"40 - Obsidian"');
dv.table(["Image", "Title", "Rating", "Method", "Ingredients", "HowTo"], pages
		.sort(p =>, 'asc')
		.map(p => 
			["![](" + p.image + ")",,
			"Rated as ⭐ " + p.Rating,
			p.Method,, index) => `${item}, ${p.amount[index]}`), 

Final words

This is a very messy tutorial and setup, but I just wanted to post it so people could use it or follow its example. If you have any good ideas or tips to how I could either improve upon this concept or just about the post in general, leave a comment.

Otherwise, good night.

1 Like