Create backupapp.py
Browse files- backupapp.py +70 -0
backupapp.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import time
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Function to persist and reload the dataframe
|
| 7 |
+
def persist_and_reload(df):
|
| 8 |
+
df.to_csv('votes.csv', index=False)
|
| 9 |
+
new_df = pd.read_csv('votes.csv')
|
| 10 |
+
return new_df
|
| 11 |
+
|
| 12 |
+
# Function to vote on a story
|
| 13 |
+
def vote_story(index):
|
| 14 |
+
st.session_state.df.loc[index, 'votes'] += 1
|
| 15 |
+
st.session_state.df = persist_and_reload(st.session_state.df)
|
| 16 |
+
|
| 17 |
+
# Function to break story into sentences
|
| 18 |
+
def break_into_sentences(story):
|
| 19 |
+
return story.split(". ")
|
| 20 |
+
|
| 21 |
+
# Function to display story and voting
|
| 22 |
+
def display_story(index, row):
|
| 23 |
+
sentences = break_into_sentences(row['story'])
|
| 24 |
+
for sentence in sentences:
|
| 25 |
+
st.text(sentence)
|
| 26 |
+
if st.button(f"Vote for Story {index + 1}"):
|
| 27 |
+
vote_story(index)
|
| 28 |
+
|
| 29 |
+
# Display a small bar chart for vote count
|
| 30 |
+
fig, ax = plt.subplots()
|
| 31 |
+
ax.barh(['Votes'], [row['votes']])
|
| 32 |
+
st.pyplot(fig)
|
| 33 |
+
|
| 34 |
+
# Initialize session state for the vote counts
|
| 35 |
+
if 'df' not in st.session_state:
|
| 36 |
+
try:
|
| 37 |
+
st.session_state.df = pd.read_csv('votes.csv')
|
| 38 |
+
except FileNotFoundError:
|
| 39 |
+
st.session_state.df = pd.DataFrame({
|
| 40 |
+
'story': [
|
| 41 |
+
"A 45-year-old man presents with a long history of ulcers on the bottom of his feet. 👣🚑",
|
| 42 |
+
"A 24-year-old man, an information technology professional, gets himself tested for serum immunoglobulin M (IgM) levels. 💻🧪",
|
| 43 |
+
"A 33-year-old woman who was recently involved in a motor vehicle accident presents to a medical clinic for a follow-up visit. 🚗🏥",
|
| 44 |
+
],
|
| 45 |
+
'votes': [0, 0, 0]
|
| 46 |
+
})
|
| 47 |
+
st.session_state.df = persist_and_reload(st.session_state.df)
|
| 48 |
+
|
| 49 |
+
# Main app
|
| 50 |
+
st.title('Medical Story Voting 🗳️')
|
| 51 |
+
|
| 52 |
+
# Display stories and voting buttons
|
| 53 |
+
for index, row in st.session_state.df.iterrows():
|
| 54 |
+
col1, col2 = st.columns([3,1])
|
| 55 |
+
with col1:
|
| 56 |
+
display_story(index, row)
|
| 57 |
+
with col2:
|
| 58 |
+
st.write(f"Votes: {row['votes']}")
|
| 59 |
+
|
| 60 |
+
# Display total votes
|
| 61 |
+
st.markdown("### 📊 Vote Summary")
|
| 62 |
+
st.table(st.session_state.df)
|
| 63 |
+
|
| 64 |
+
# Reload votes from file every 30 seconds
|
| 65 |
+
if 'last_updated' not in st.session_state:
|
| 66 |
+
st.session_state.last_updated = time.time()
|
| 67 |
+
|
| 68 |
+
if time.time() - st.session_state.last_updated > 30:
|
| 69 |
+
st.session_state.df = pd.read_csv('votes.csv')
|
| 70 |
+
st.session_state.last_updated = time.time()
|