awacke1 commited on
Commit
8000de8
β€’
1 Parent(s): edec496

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Streamlit.Funny.Feedback.Upvote.Downvote
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import json
5
+ import os
6
+
7
+ def display_table():
8
+ st.title("The Great Debate: Vote on the Funniest Questions!")
9
+
10
+ data = [
11
+ (1, "πŸ˜‚", "How many cups of coffee do you need to function like a normal human being?", "[Wikipedia](https://en.wikipedia.org/wiki/Coffee)"),
12
+ (2, "πŸ€”", "If animals could talk, which species do you think would be the most annoying?", "[Wikipedia](https://en.wikipedia.org/wiki/Animal_communication)"),
13
+ (3, "🀫", "What's the craziest conspiracy theory you've ever heard?", "[Wikipedia](https://en.wikipedia.org/wiki/Conspiracy_theory)"),
14
+ (4, "🀣", "What's the worst pickup line you've ever heard or used?", "[Wikipedia](https://en.wikipedia.org/wiki/Pick-up_line)"),
15
+ (5, "😜", "If you were a superhero, what would your superpower be?", "[Wikipedia](https://en.wikipedia.org/wiki/Superpower_(ability))"),
16
+ (6, "🀯", "If you could time travel, what period in history would you go to and why?", "[Wikipedia](https://en.wikipedia.org/wiki/Time_travel)"),
17
+ (7, "😝", "What's the weirdest thing you've ever eaten?", "[Wikipedia](https://en.wikipedia.org/wiki/List_of_delicacies)"),
18
+ (8, "πŸ€ͺ", "What's the most embarrassing thing that's ever happened to you in public?", "[Wikipedia](https://en.wikipedia.org/wiki/Embarrassment)"),
19
+ (9, "😈", "If you could be any movie villain, who would you choose and why?", "[Wikipedia](https://en.wikipedia.org/wiki/Villain)"),
20
+ (10, "πŸ™ƒ", "What's the most useless talent you have?", "[Wikipedia](https://en.wikipedia.org/wiki/Talent_(human))"),
21
+ ]
22
+
23
+ st.table(pd.DataFrame(data, columns=['Question', 'Emoji', 'Title', 'Description']))
24
+
25
+ def update_vote_log(term, vote_type):
26
+ with open('vote.log.txt', 'a') as f:
27
+ f.write(json.dumps({'term': term, 'vote': vote_type}) + '\n')
28
+
29
+ def load_vote_log():
30
+ vote_data = []
31
+
32
+ if os.path.exists('vote.log.txt'):
33
+ with open('vote.log.txt', 'r') as f:
34
+ for line in f.readlines():
35
+ vote_data.append(json.loads(line.strip()))
36
+
37
+ return vote_data
38
+
39
+ def count_votes(vote_data, term):
40
+ upvotes = sum(1 for vote in vote_data if vote['term'] == term and vote['vote'] == 'upvote')
41
+ downvotes = sum(1 for vote in vote_data if vote['term'] == term and vote['vote'] == 'downvote')
42
+ return upvotes, downvotes
43
+
44
+ def main():
45
+ display_table()
46
+
47
+ terms = [
48
+ 'Question 1',
49
+ 'Question 2',
50
+ 'Question 3',
51
+ 'Question 4',
52
+ 'Question 5',
53
+ 'Question 6',
54
+ 'Question 7',
55
+ 'Question 8',
56
+ 'Question 9',
57
+ 'Question 10'
58
+ ]
59
+
60
+ vote_data = load_vote_log()
61
+
62
+ with st.sidebar:
63
+ st.title("User Controls")
64
+ for term in terms:
65
+ st.write(term)
66
+ upvotes, downvotes = count_votes(vote_data, term)
67
+ st.write(f"Total upvotes: {upvotes}")
68
+ st.write(f"Total downvotes: {downvotes}")
69
+
70
+ upvote_button = st.button(f"πŸ‘ Upvote {term}")
71
+ downvote_button = st.button(f"πŸ‘Ž Downvote {term}")
72
+
73
+ if upvote_button:
74
+ update_vote_log(term, 'upvote')
75
+ st.experimental_rerun()
76
+
77
+ if downvote_button:
78
+ update_vote_log(term, 'downvote')
79
+ st.experimental_rerun()
80
+
81
+ if name == "main":
82
+ main()
83
+