awacke1's picture
Update app.py
22a2701
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()