diff --git a/app_9/__pycache__/app.cpython-36.pyc b/app_9/__pycache__/app.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..36157c048ca7cee25fb7a9fac47caf3e442510db Binary files /dev/null and b/app_9/__pycache__/app.cpython-36.pyc differ diff --git a/app_9/app.py b/app_9/app.py new file mode 100644 index 0000000000000000000000000000000000000000..74ada1a37249e10c5ae8fc84100811bbd9f25fa5 --- /dev/null +++ b/app_9/app.py @@ -0,0 +1,26 @@ +import os +import requests + +from flask import Flask, jsonify, render_template, request +from flask_socketio import SocketIO, emit + +app = Flask(__name__) +app.config["SECRET_KEY"] = os.getenv("SECRET_KEY") +socketio = SocketIO(app) + +# A list which is used to save the history vote +history = [] + +@app.route("/") +def index(): + return render_template("index.html") + +@socketio.on('submit vote') +def vote(data): + selection = data["selection"] + history.append("selection: " + selection) + emit('announce vote', {"selection": selection}, broadcast=True) + print(history) + +if __name__ == '__main__': + socketio.run(app) \ No newline at end of file diff --git a/app_9/static/js/index.js b/app_9/static/js/index.js new file mode 100644 index 0000000000000000000000000000000000000000..3f403d958b131c44799a997c601bd2c86f78767c --- /dev/null +++ b/app_9/static/js/index.js @@ -0,0 +1,26 @@ + +document.addEventListener('DOMContentLoaded', () => { + + // Connect to websocket + var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port); + + // When connected, configure buttons + socket.on('connect', () => { + + // Each button should emit a "submit vote" event + document.querySelectorAll('button').forEach(button => { + button.onclick = () => { + const selection = button.dataset.vote; + socket.emit('submit vote', {'selection': selection}); + }; + }); + }); + + // When a new vote is announced, add to the unordered list + socket.on('announce vote', data => { + const li = document.createElement('li'); + li.innerHTML = `Vote recorded: ${data.selection}`; + document.querySelector('#votes').append(li); + }); + +}); \ No newline at end of file diff --git a/app_9/templates/index.html b/app_9/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..09b764c9ca55004187f9b53045eb5ef4a7610610 --- /dev/null +++ b/app_9/templates/index.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html> + <head> + <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script> + <script src="{{ url_for('static', filename='js/index.js') }}"></script> + <title>Vote</title> + </head> + <body> + <ul id="votes"> + </ul> + <hr> + <button data-vote="yes">Yes</button> + <button data-vote="no">No</button> + <button data-vote="maybe">Maybe</button> + </body> +</html> \ No newline at end of file