-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (112 loc) · 3.68 KB
/
app.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# - coding: utf-8 --
from flask import Flask, render_template, request, json
from flaskext.mysql import MySQL
from cosine import *
app = Flask(__name__)
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'sparks'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
@app.route("/")
def main():
return render_template('index.html')
@app.route("/loadBooks", methods=['POST', 'GET'])
def loadBooks():
_name = request.form['name']
print(_name)
_type = request.form['searchtype']
print(_type)
conn = mysql.connect()
cursor = conn.cursor()
books= []
if _type == "general" :
data = relevance(_name)
if len(data) != 0:
# conn.commit()
for d in data:
print(d[0])
id = d[0]
value = d[1]
if value != 0.0:
cursor.callproc('loadbooks', [id])
book = cursor.fetchall()
print(book[0])
if len(book) != 0:
conn.commit()
books.append(book[0])
# print(books)
else:
return render_template('error.html', name=_name, text = "No Data Found")
elif _type == "book":
cursor.callproc('loadbookbyname', [_name])
data = cursor.fetchall()
# print(data)
if len(data) != 0:
conn.commit()
for d in data:
print("data")
print(d)
if len(books) == 0:
books.append(d)
else:
for book in books:
if book[0]==d[0]:
continue
else:
print("books")
books.append(d)
print(books)
else:
return render_template('error.html', name=_name, text = "No Data Found")
elif _type == "author" :
print("author")
cursor.callproc('loadbookbyauthor', [_name])
data = cursor.fetchall()
# print(data)
if len(data) != 0:
conn.commit()
for d in data:
print("data")
print(d)
if len(books) == 0:
books.append(d)
else:
for book in books:
if book[0]==d[0]:
continue
else:
print("books")
books.append(d)
# print(books)
else:
return render_template('error.html', name=_name, text = "No Data Found")
# print(data)
if len(data) != 0:
conn.commit()
if len(books) == 0:
return render_template('error.html', name=_name, text = "No Data Found")
else:
return render_template('book.html', name=_name, data = books)
else:
return render_template('error.html', name=_name, text = "No Data Found")
@app.route("/loadBookDetails/<id>", methods=['POST', 'GET'])
def loadBookDetails(id):
print(id)
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('loadbookdetails', [id])
data = cursor.fetchall()
print(data)
if len(data) != 0:
conn.commit()
return render_template('loadbookdetails.html', data = data)
else:
return json.dumps({'error': 'No data Found'})
@app.route("/books")
def books():
return render_template('book.html')
if __name__ == "__main__":
app.run(debug=True)