File size: 3,180 Bytes
8000de8
 
 
 
 
5f46932
8000de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f46932
932dcb5
 
 
5f46932
22a2701
5f46932
932dcb5
 
 
 
5f46932
932dcb5
 
 
 
 
 
 
 
 
 
8000de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
932dcb5
db01a3e
 
 
 
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
import streamlit as st
import pandas as pd
import json
import os

def display_table(vote_data):
    st.title("The Great Debate: Vote on the Funniest Questions!")

    data = [
        (1, "πŸ˜‚", "How many cups of coffee do you need to function like a normal human being?", "[Wikipedia](https://en.wikipedia.org/wiki/Coffee)"),
        (2, "πŸ€”", "If animals could talk, which species do you think would be the most annoying?", "[Wikipedia](https://en.wikipedia.org/wiki/Animal_communication)"),
        (3, "🀫", "What's the craziest conspiracy theory you've ever heard?", "[Wikipedia](https://en.wikipedia.org/wiki/Conspiracy_theory)"),
        (4, "🀣", "What's the worst pickup line you've ever heard or used?", "[Wikipedia](https://en.wikipedia.org/wiki/Pick-up_line)"),
        (5, "😜", "If you were a superhero, what would your superpower be?", "[Wikipedia](https://en.wikipedia.org/wiki/Superpower_(ability))"),
        (6, "🀯", "If you could time travel, what period in history would you go to and why?", "[Wikipedia](https://en.wikipedia.org/wiki/Time_travel)"),
        (7, "😝", "What's the weirdest thing you've ever eaten?", "[Wikipedia](https://en.wikipedia.org/wiki/List_of_delicacies)"),
        (8, "πŸ€ͺ", "What's the most embarrassing thing that's ever happened to you in public?", "[Wikipedia](https://en.wikipedia.org/wiki/Embarrassment)"),
        (9, "😈", "If you could be any movie villain, who would you choose and why?", "[Wikipedia](https://en.wikipedia.org/wiki/Villain)"),
        (10, "πŸ™ƒ", "What's the most useless talent you have?", "[Wikipedia](https://en.wikipedia.org/wiki/Talent_(human))"),
    ]

    for row in data:
        question_id = f"Question {row[0]}"
        emoji, title, description = row[1], row[2], row[3]
        upvotes, downvotes = count_votes(vote_data, question_id)

        col1, col2, col3, col4 = st.columns([1, 3, 1, 1])

        col1.write(emoji)
        col2.write(f"{title}\n{description}")
        col3.write(f"πŸ‘ {upvotes}")
        col4.write(f"πŸ‘Ž {downvotes}")

        upvote_button = col3.button(f"Upvote {question_id}")
        downvote_button = col4.button(f"Downvote {question_id}")

        if upvote_button:
            update_vote_log(question_id, 'upvote')
            st.experimental_rerun()

        if downvote_button:
            update_vote_log(question_id, 'downvote')
            st.experimental_rerun()

def update_vote_log(term, vote_type):
    with open('vote.log.txt', 'a') as f:
        f.write(json.dumps({'term': term, 'vote': vote_type}) + '\n')

def load_vote_log():
    vote_data = []

    if os.path.exists('vote.log.txt'):
        with open('vote.log.txt', 'r') as f:
            for line in f.readlines():
                vote_data.append(json.loads(line.strip()))
    return vote_data

def count_votes(vote_data, term):
    upvotes = sum(1 for vote in vote_data if vote['term'] == term and vote['vote'] == 'upvote')
    downvotes = sum(1 for vote in vote_data if vote['term'] == term and vote['vote'] == 'downvote')
    return upvotes, downvotes

def main():
    vote_data = load_vote_log()

    display_table(vote_data)

if __name__ == "__main__":
    main()