import json def votes(): # Load votes with open('data/votes.json') as file: votes = json.load(file) print(votes) options = {} for vote in votes: # Check if message is json if vote["message"].startswith("{"): message = json.loads(vote["message"]) for key in message: if key in options: options[key] += (int(message[key]) * int(vote["votes"])) else: options[key] = (int(message[key]) * int(vote["votes"])) continue if vote["message"] in options: options[vote["message"]] += vote["votes"] else: options[vote["message"]] = vote["votes"] labels = list(options.keys()) data = list(options.values()) chart_data = { "type": "pie", "data": { "labels": labels, "datasets": [{ "label": "Votes", "backgroundColor": ["rgb(17,255,69)", "rgb(255,0,0)", "rgb(0,0,255)", "rgb(255,255,0)", "rgb(255,0,255)", "rgb(0,255,255)", "rgb(128,0,128)"], "data": data }] }, "options": { "maintainAspectRatio": True, "legend": { "display": True, "labels": { "fontStyle": "normal" } }, "title": { "fontStyle": "bold" } } } html = '' html += f'' return html def options(options): html = '' html += f''' ''' html += '' html += f''' ''' return html