-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (35 loc) · 1012 Bytes
/
Copy pathapp.py
File metadata and controls
49 lines (35 loc) · 1012 Bytes
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
from flask import Flask
from flask import render_template
from flask import request
import requests
import json
app = Flask(__name__)
@app.route('/')
def index():
return render_template('./index.html', test='')
@app.route('/res', methods=['POST', 'GET'])
def res():
search = request.form['search']
url= 'https://api.hatchways.io/assessment/blog/posts?tag='+search
try:
res = requests.get(url).json()
res = res['posts']
#res = json.loads(res)
ilist = []
for i in res:
ilist.append(i)
option = request.form['order']
if option =='asc':
ilist.sort(key=lambda x: x.get('tags'))
elif option == 'dsc':
ilist.sort(key=lambda x: x.get('tags'), reverse=True)
if not ilist:
output = '{<br>'
output = output + '"error": "Tags parameter is required" <br>'
output = output + '}'
return output
return render_template('./res.html', test=ilist)
except requests.ConnectionError:
return "Connection Error"
if __name__ == '__main__':
app.run(debug=True)