Spaces:
Runtime error
Runtime error
Create app.py
Browse files
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 |
+
|