Skip to content
Snippets Groups Projects
Commit 2fa6beed authored by Minhao Qiu's avatar Minhao Qiu
Browse files

add total vote function

parent b84678a8
No related branches found
No related tags found
No related merge requests found
...@@ -11,16 +11,21 @@ socketio = SocketIO(app) ...@@ -11,16 +11,21 @@ socketio = SocketIO(app)
# A list which is used to save the history vote # A list which is used to save the history vote
history = [] history = []
votes = {"yes": 0, "no": 0, "maybe": 0}
@app.route("/") @app.route("/")
def index(): def index():
return render_template("index.html") return render_template("index.html", votes=votes)
@socketio.on('submit vote') @socketio.on('submit vote')
def vote(data): def vote(data):
selection = data["selection"] selection = data["selection"]
history.append("selection: " + selection) #history.append("selection: " + selection)
votes[selection] += 1
print(votes)
emit('announce vote', {"selection": selection}, broadcast=True) emit('announce vote', {"selection": selection}, broadcast=True)
print(history) emit("votes total", votes, broadcast=True)
if __name__ == '__main__': if __name__ == '__main__':
socketio.run(app) socketio.run(app, debug=True)
\ No newline at end of file \ No newline at end of file
...@@ -21,6 +21,17 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -21,6 +21,17 @@ document.addEventListener('DOMContentLoaded', () => {
const li = document.createElement('li'); const li = document.createElement('li');
li.innerHTML = `Vote recorded: ${data.selection}`; li.innerHTML = `Vote recorded: ${data.selection}`;
document.querySelector('#votes').append(li); document.querySelector('#votes').append(li);
});
socket.on('votes total', data => {
document.querySelector('#yes').innerHTML = data.yes;
document.querySelector('#no').innerHTML = data.no;
document.querySelector('#maybe').innerHTML = data.maybe;
}); });
}); });
\ No newline at end of file
...@@ -6,8 +6,11 @@ ...@@ -6,8 +6,11 @@
<title>Vote</title> <title>Vote</title>
</head> </head>
<body> <body>
<ul id="votes"> <ul id="votes"></ul>
</ul> <hr>
<div>Yes Votes: <span id="yes">{{ votes["yes"] }}</span></div>
<div>No Votes: <span id="no">{{ votes["no"] }}</span></div>
<div>Maybe Votes: <span id="maybe">{{ votes["maybe"] }}</span></div>
<hr> <hr>
<button data-vote="yes">Yes</button> <button data-vote="yes">Yes</button>
<button data-vote="no">No</button> <button data-vote="no">No</button>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment