-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (67 loc) · 2.11 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
import os
from flask import Flask, render_template, request, redirect, jsonify
from flask_socketio import SocketIO, emit
import json
app = Flask(__name__)
app.config['SECRET_KEY'] = '86d119405b0d0a506506f0ei963ce914'
socketio = SocketIO(app)
def load_photos():
with open('photos.json') as f:
data = json.load(f)
return data['photos']
def upvote_photo(photo_id):
with open('photos.json') as f:
data = json.load(f)
for photo in data['photos']:
if photo['id'] == photo_id:
photo['votes'] += 1
socketio.emit('vote_update', photo, broadcast=True)
break
with open('photos.json', 'w') as f:
json.dump(data, f)
@app.route('/')
def display_gallery():
photos = load_photos()
sorted_photos = sorted(photos, key=lambda x: x['votes'], reverse=True)
return render_template('index.html', photos=sorted_photos)
@app.route('/upvote', methods=['POST'])
def handle_upvote():
photo_id = request.form['photo_id']
photo_id = int(photo_id)
upvote_photo(photo_id)
photos = load_photos()
socketio.emit('vote_update', photos, broadcast=True)
return jsonify(photos)
@socketio.on('connect')
def handle_connect():
photos = load_photos()
emit('load_photos', photos)
@socketio.on('update_photo')
def update_photo():
print('updating photos')
photos = load_photos()
emit('load_photos', photos)
def write_to_json(photo_id,photo_votes):
with open('photos.json') as f:
data = json.load(f)
for photo in data['photos']:
if photo['id'] == photo_id:
photo['votes'] = photo_votes
break
with open('photos.json', 'w') as f:
json.dump(data, f)
@socketio.on('vote_update')
def handle_vote_update(photo_data):
photo_id = photo_data['id']
photo_votes = photo_data['votes']
photo_votes=int(photo_votes)+1
write_to_json(photo_id,photo_votes)
# script = f"document.querySelector('#photo-{photo_id} .vote-count').textContent = '{photo_votes}';"
script={
'id': photo_id,
'votes': photo_votes
}
emit('update_vote', script, broadcast=True)
if __name__ == '__main__':
app.run(debug=True)
socketio.run(app, host='192.168.1.100', port=int(os.environ.get('PORT', 5000)))