|
import streamlit as st |
|
import pandas as pd |
|
import time |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def persist_and_reload(df): |
|
df.to_csv('votes.csv', index=False) |
|
new_df = pd.read_csv('votes.csv') |
|
return new_df |
|
|
|
|
|
def vote_story(index): |
|
st.session_state.df.loc[index, 'votes'] += 1 |
|
st.session_state.df = persist_and_reload(st.session_state.df) |
|
|
|
|
|
def break_into_sentences(story): |
|
return story.split(". ") |
|
|
|
|
|
def display_story(index, row): |
|
sentences = break_into_sentences(row['story']) |
|
for sentence in sentences: |
|
st.text(sentence) |
|
if st.button(f"Vote for Story {index + 1}"): |
|
vote_story(index) |
|
|
|
|
|
fig, ax = plt.subplots() |
|
ax.barh(['Votes'], [row['votes']]) |
|
st.pyplot(fig) |
|
|
|
|
|
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) |
|
|
|
|
|
st.title('Medical Story Voting 🗳️') |
|
|
|
|
|
for index, row in st.session_state.df.iterrows(): |
|
col1, col2 = st.columns([3,1]) |
|
with col1: |
|
display_story(index, row) |
|
with col2: |
|
st.write(f"Votes: {row['votes']}") |
|
|
|
|
|
st.markdown("### 📊 Vote Summary") |
|
st.table(st.session_state.df) |
|
|
|
|
|
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() |
|
|