-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app_4.py
More file actions
executable file
·56 lines (48 loc) · 1.27 KB
/
Copy pathflask_app_4.py
File metadata and controls
executable file
·56 lines (48 loc) · 1.27 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
#!/usr/bin/env python3
# -*- utf-8 -*-
#
# https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
#
# Karl.Lv@outlook.com, KarlLv@126.com
# 09 August, 2017
#
from datetime import date
import time
from flask import Flask
from flask import jsonify
from flask import url_for
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
def make_public_task(task):
new_task = {}
for field in task:
if field == 'id':
new_task['uri'] = url_for('get_tasks', task_id=task['id'],
_external=True)
else:
new_task[field] = task[field]
return new_task
@app.route('/')
def index():
now = time.strftime('%Y-%m-%d %H:%M:%S %A %B %Z',
time.localtime(time.time()))
str = "Hello Python3, it is " + now
return str
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': [make_public_task(task) for task in tasks]})
if __name__ == '__main__':
app.run(debug=True)