forked from medha4/recipebookproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpage.py
109 lines (90 loc) · 4.6 KB
/
webpage.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, render_template, request
from fuzzywuzzy import fuzz
app = Flask(__name__)
class Recipe: #created the recipe class
def __init__(self, name, description, ingredients, steps):
self.name = name
self.description = description
self.ingredients = ingredients
self.steps = steps
r1 = Recipe("apple pie", "pie with apples", "apples, flour, cinnamon, sugar", ["cut the apples", "mix sugar and cinnamon", "roast them"]) #hard coded a new recipe
r2 = Recipe("pumpkin pie", "pie with pumpkins", "pumpkin, flour, cinnamon, sugar", ["cut the pumpkin", "mix sugar and cinnamon", "roast them"]) #hard coded a new recipe
r3 = Recipe("pecan pie", "pie with pecans", "pecans, flour, cinnamon, sugar", ["open the pecan box", "mix sugar and cinnamon", "roast them"]) #hard coded a new recipe
list_of_recipes = [r1, r2, r3] #hard coded list of recipes
def searchBySubstring(recipe_name): #if the recipe the user is searching for matches a substring within another recipe then it selects the first recipe that matches
recipe_num = 0
for recipe in list_of_recipes:
recipe_num+=1
if(recipe_name.lower() in recipe.name.lower()):
return True, recipe, recipe_num
return False, -1, -1
def searchByFuzzy(recipe_name): #selects and returns the recipe that is slightly off from other recipes
recipe_num = 0
for recipe in list_of_recipes:
recipe_num+=1
if(fuzz.ratio(recipe_name.lower(), recipe.name.lower()) >= 70): #if the fuzzy ratio is 70% or higher, then it will return the recipe
return True, recipe, recipe_num
return False, -1
@app.route("/")
def home():
return render_template('home.html')
# @app.route('/browse', methods=['GET', 'POST'])
# def browse():
# if request.method == 'POST':
# return render_template('browse.html', shortcode=request.form['shortcode'])
# elif request.method == 'GET':
# return 'A GET request was made'
# else:
# return 'Not a valid request method for this route'
@app.route("/task", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
task = request.form.to_dict()["options"]
if task == "create":
return render_template('create.html')
elif task == "search":
return render_template('search.html')
elif task == "browse":
return render_template('browse.html', list_of_recipes=list_of_recipes)
else:
return "Error: Unable to Retrieve Your Requested Option"
elif request.method == 'GET':
return render_template('home.html', form=form)
@app.route("/create", methods=['POST'])
def create_a_recipe():
recipe_dict = request.form.to_dict()
list_of_recipes.append(Recipe(recipe_dict['recipename'], recipe_dict['recipedescription'], recipe_dict['recipeingredients'], recipe_dict['recipesteps'].split(","))) #adds the recipe object to the internal list of recipes
return render_template('viewrecipe.html', name=recipe_dict['recipename'], description=recipe_dict['recipedescription'], ingredients=recipe_dict['recipeingredients'], steps=recipe_dict['recipesteps'].split(","))
@app.route("/displayrecipe", methods=['POST'])
def display_the_recipe():
print("display")
print(request.form.to_dict())
recipe_name = request.form.to_dict()["options"]
print(recipe_name)
for recipe in list_of_recipes:
if recipe_name in recipe.name:
return render_template('viewrecipe.html', name=recipe.name, description=recipe.description, ingredients=recipe.ingredients, steps=recipe.steps)
return "Error: Recipe Not Found"
@app.route("/search", methods=['POST'])
def search_for_recipe():
recipe_name = request.form.to_dict()['search']
x = False
recipe_num = 0
for recipe in list_of_recipes:
recipe_num+=1
if(recipe.name.lower() == recipe_name.lower()): #search for a recipe exactly by name
x = True
selected_recipe = recipe
return render_template('viewrecipe.html', name=selected_recipe.name, description=selected_recipe.description, ingredients=selected_recipe.ingredients, steps=selected_recipe.steps)
if x == False:
x, selected_recipe, recipe_num = searchBySubstring(recipe_name) #search by substring matching
if not x:
x, selected_recipe, recipe_num = searchByFuzzy(recipe_name) #search by fuzzy searching
if not x:
return "Error: Recipe Not Found"
else:
return render_template('viewrecipe.html', name=selected_recipe.name, description=selected_recipe.description, ingredients=selected_recipe.ingredients, steps=selected_recipe.steps)
else:
return render_template('viewrecipe.html', name=selected_recipe.name, description=selected_recipe.description, ingredients=selected_recipe.ingredients, steps=selected_recipe.steps)
if __name__ == "__main__":
app.run()