-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
50 lines (41 loc) · 1.19 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
# ./app.py
from flask import Flask, render_template, request, jsonify
from pusher import Pusher
import json
# create flask app
app = Flask(__name__)
# configure pusher object
pusher = Pusher(
app_id='YOUR_APP_ID',
key='YOUR_APP_KEY',
secret='YOUR_APP_SECRET',
cluster='YOUR_APP_CLUSTER',
ssl=True
)
# index route, shows index.html view
@app.route('/')
def index():
return render_template('index.html')
# endpoint for storing todo item
@app.route('/add-todo', methods = ['POST'])
def addTodo():
data = json.loads(request.data) # load JSON data from request
pusher.trigger('todo', 'item-added', data) # trigger `item-added` event on `todo` channel
return jsonify(data)
# endpoint for deleting todo item
@app.route('/remove-todo/<item_id>')
def removeTodo(item_id):
data = {'id': item_id }
pusher.trigger('todo', 'item-removed', data)
return jsonify(data)
# endpoint for updating todo item
@app.route('/update-todo/<item_id>', methods = ['POST'])
def updateTodo(item_id):
data = {
'id': item_id,
'completed': json.loads(request.data).get('completed', 0)
}
pusher.trigger('todo', 'item-updated', data)
return jsonify(data)
# run Flask app in debug mode
app.run(debug=True)