-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
330 lines (261 loc) · 9.32 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from flask import Flask, render_template, request, redirect, url_for, logging, session, flash, jsonify
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from flask_mysqldb import MySQL
from passlib.hash import sha256_crypt
from functools import wraps
from database import dbusers
# for rest api
from flask_keras_rest_api import load_model, prepare_image
from PIL import Image
import io
from keras.applications import imagenet_utils
# initialize the model
model = None
graph = None
app = Flask(__name__)
app.debug=True
db_users = dbusers() # Hiding database info from others
# Config MySQL
app.config['MYSQL_HOST'] = db_users['host']
app.config['MYSQL_USER'] = db_users['user']
app.config['MYSQL_PASSWORD'] = db_users['password']
app.config['MYSQL_DB'] = db_users['db']
app.config['MYSQL_CURSORCLASS'] = db_users['cursor']
app.secret_key = 'verySecret#123'
# Init MySQL
mysql = MySQL(app)
# About Route/Page
@app.route('/add_numbers')
def add_numbers():
print("add_numbers is called")
a = request.args.get('a', 0, type=int)
b = request.args.get('b', 0, type=int)
print(a)
print(b)
return jsonify(result=a+b)
# Home route
@app.route('/')
def home():
# Create Cursor
cur = mysql.connection.cursor()
# Get articles
result = cur.execute("SELECT * FROM articles")
articles = cur.fetchall()
cur.close()
if result > 0:
return render_template('home.html', articles = articles)
else:
msg = 'No Articles Found'
return render_template('home.html')
#Single Article
@app.route('/article/<string:id>/')
def article(id):
# Create cursor
cur = mysql.connection.cursor()
# Get article
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article = cur.fetchone()
return render_template('article.html', article=article)
# About Route/Page
@app.route('/about')
def about():
return render_template('about.html')
#Register Form Class
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Password do not match')
])
confirm = PasswordField('Confirm Password')
# Register route
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
# Create Cursor
cur = mysql.connection.cursor()
# Execute query
cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
flash('Successful. You are registered. You may login.', 'success')
return redirect(url_for('login', _external=True))
return render_template('register.html', form=form)
# Login Route
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get Form Fields
username = request.form['username']
password_candidate = request.form['password']
# Create Cursor
cur = mysql.connection.cursor()
# Execute SQL
result = cur.execute('SELECT * FROM users WHERE username= %s', [username])
# print(data['user'])
if result > 0:
# Get stored hash
data = cur.fetchone()
password = data['password']
print(data['password'])
# Compare Password
if sha256_crypt.verify(password_candidate, password):
#Passed
session['logged_in'] = True
session['username'] = username
print(session['logged_in'])
flash('You are logged in.', 'success')
# return render_template('about.html')
return redirect(url_for('dashboard', _external=True))
else:
error = 'Invalid login'
return render_template('login.html', error=error)
else:
error = 'Username not found'
return render_template('login.html', error=error)
return render_template('login.html')
# Check if user looged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized! Please login.', 'danger')
return redirect(url_for('login'))
return wrap
# Dashboard
@app.route('/dashboard')
@is_logged_in
def dashboard():
# Create Cursor
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles")
articles = cur.fetchall()
cur.close()
if result > 0:
return render_template('dashboard.html', articles=articles)
else:
msg = 'No Articles Found'
return render_template('dashboard.html', msg=msg)
# Article Form Class
class ArticleForm(Form):
title = StringField('Title', [validators.Length(min=1, max=200)])
content = TextAreaField('Content', [validators.Length(min=30)])
# Add article
@app.route('/add_article', methods=['GET', 'POST'])
@is_logged_in
def add_article():
form = ArticleForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
content = form.content.data
# Create Cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("INSERT INTO articles(title, content, author) VALUES(%s, %s, %s)", (title, content, session['username']))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Successful! Article has been added', 'success')
return redirect(url_for('dashboard'))
else:
return render_template('add_article.html', form=form)
# Edit article
@app.route('/edit_article/<string:id>', methods=['GET', 'POST'])
@is_logged_in
def edit_article(id):
# Create cursor
cur = mysql.connection.cursor()
# Get article by id
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article = cur.fetchone()
cur.close()
# Get form
form = ArticleForm(request.form)
# Populate article form fields
form.title.data = article['title']
form.content.data = article['content']
if request.method == 'POST' and form.validate():
title = request.form['title']
content = request.form['content']
# Create Cursor
cur = mysql.connection.cursor()
app.logger.info(title)
# Execute
cur.execute ("UPDATE articles SET title=%s, content=%s WHERE id=%s",(title, content, id))
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
flash('Article Updated', 'success')
return redirect(url_for('dashboard'))
return render_template('edit_article.html', form=form)
# Logout Route
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You successfully logged out', 'success')
# return render_template('login.html')
return redirect(url_for('login'))
# Delete Article
@app.route('/delete_article/<string:id>', methods=['POST'])
@is_logged_in
def delete_article(id):
# Create cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("DELETE FROM articles WHERE id = %s", [id])
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
print("Deteting post " + id)
flash('Article Deleted', 'success')
return redirect(url_for('dashboard'))
# predict dog breed api route.
@app.route("/api/predict_dog_breed", methods=['POST'])
def predict_dog_breed():
# initialize the data dictionary that will be returned from the
# view
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if request.method == "POST":
if request.files.get("image"):
# read the image in PIL format
image = request.files["image"].read()
image = Image.open(io.BytesIO(image))
# preprocess the image and prepare it for classification
image = prepare_image(image, target=(224, 224))
# classify the input image and then initialize the list
# of predictions to return to the client
global model, graph
with graph.as_default():
preds = model.predict(image)
results = imagenet_utils.decode_predictions(preds)
data["predictions"] = []
# loop over the results and add them to the list of
# returned predictions
for (imagenetID, label, prob) in results[0]:
r = {"label": label, "probability": float(prob)}
data["predictions"].append(r)
# indicate that the request was a success
data["success"] = True
# return the data dictionary as a JSON response
return jsonify(data)
if __name__ == '__main__':
# call to load
model, graph = load_model(model, graph)
# graph = tf.get_default_graph()
app.run()