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 'votes' not in vote: continue weight = int(vote["votes"]) / 100 if vote["message"].startswith("{"): message = json.loads(vote["message"]) for key in message: if key in options: options[key] += (int(message[key]) * weight) else: options[key] = (int(message[key]) * weight) continue if vote["message"] in options: options[vote["message"]] += weight * 100 else: options[vote["message"]] = weight * 100 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