File size: 2,623 Bytes
dfc9fe7 a061b45 628a01b a061b45 628a01b a061b45 ad081e3 628a01b a061b45 2a58b4b a061b45 2a58b4b ad081e3 628a01b 2a58b4b 628a01b a061b45 ad081e3 2a58b4b a061b45 ad081e3 628a01b 2a58b4b 628a01b |
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 71 72 73 74 75 76 77 78 79 |
import streamlit as st
import pandas as pd
import time
import matplotlib.pyplot as plt
# Function to persist and reload the dataframe
def persist_and_reload(df):
df.to_csv('votes.csv', index=False)
new_df = pd.read_csv('votes.csv')
return new_df
# Function to vote on a story
def vote_story(index):
st.session_state.df.loc[index, 'votes'] += 1
st.session_state.df = persist_and_reload(st.session_state.df)
# Function to break story into sentences
def break_into_sentences(story):
return story.split(". ")
# Function to extract high info words
def extract_high_info_words(sentence):
words = sentence.split(" ")
return [word for word in words if len(word) > 6]
# Function to display story and voting
def display_story(index, row):
sentences = break_into_sentences(row['story'])
for sentence in sentences:
st.text(sentence)
high_info_words = []
for sentence in sentences:
high_info_words.extend(extract_high_info_words(sentence))
if st.button(f"Vote for: {' '.join(high_info_words)}"):
vote_story(index)
# Initialize session state for the vote counts
if 'df' not in st.session_state:
try:
st.session_state.df = pd.read_csv('votes.csv')
except FileNotFoundError:
st.session_state.df = pd.DataFrame({
'story': [
"A 45-year-old man presents with a long history of ulcers on the bottom of his feet.",
"A 24-year-old man, an information technology professional, gets himself tested for serum immunoglobulin M (IgM) levels.",
"A 33-year-old woman who was recently involved in a motor vehicle accident presents to a medical clinic for a follow-up visit.",
],
'votes': [0, 0, 0]
})
st.session_state.df = persist_and_reload(st.session_state.df)
# Main app
st.title('Medical Story Voting 🗳️')
# Display stories and voting buttons
for index, row in st.session_state.df.iterrows():
display_story(index, row)
# Display total votes
st.markdown("### 📊 Vote Summary")
st.table(st.session_state.df)
# Show all votes in one graph
fig, ax = plt.subplots()
ax.barh(st.session_state.df.index, st.session_state.df['votes'])
ax.set_yticks(st.session_state.df.index)
ax.set_yticklabels(st.session_state.df['story'])
st.pyplot(fig)
# Reload votes from file every 30 seconds
if 'last_updated' not in st.session_state:
st.session_state.last_updated = time.time()
if time.time() - st.session_state.last_updated > 30:
st.session_state.df = pd.read_csv('votes.csv')
st.session_state.last_updated = time.time()
|