-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeCookbookSearchbarCode.py
More file actions
59 lines (49 loc) · 1.9 KB
/
RecipeCookbookSearchbarCode.py
File metadata and controls
59 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#This is the code for searching for recipes using the API via python flask for it.
from flask import Flask, render_template, request
import requests
from urllib.parse import unquote
app = Flask(__name__)
API_KEY = 'cd18acd25a7541a5b2dcf54105ea7828'
#Define route for "Home button"
@app.route("/home", methods=['GET'])
def home():
return render_template('index.html', recipes=[], recipe_query='')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
query = request.form.get('recipe_query','')
recipes = search_recipes(query)
return render_template('index.html', recipes=recipes, recipe_query=query)
recipe_query = request.args.get('recipe_query','')
decoded_recipe_query = unquote(recipe_query)
recipes = search_recipes(decoded_recipe_query)
return render_template('index.html', recipes=recipes, recipe_query=decoded_recipe_query)
def search_recipes(query):
url = f'https://api.spoonacular.com/recipes/complexSearch'
params = {
'apiKey': API_KEY,
'query': query,
'number': 10,
'instructionsRequired':True,
'addRecipeInformation':True,
'fillIngredients':True,
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
return data['results']
return[]
#Go to a specific recipe given ID
@app.route('/recipe/<int:recipe_id>')
def view_recipe(recipe_id):
recipe_query = request.args.get('recipe_query','')
url=f'https://api.spoonacular.com/recipes/{recipe_id}/information'
params = {
'apiKey': API_KEY,
}
#Send a GET request to API to get recipe information
response = requests.get(url, params=params)
if response.status_code == 200:
recipe = response.json()
return render_template('view_recipe.html', recipe=recipe, recipe_query=recipe_query)
return "Recipe not found", 404