Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,18 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
"A 24-year-old man, an information technology professional, gets himself tested for serum immunoglobulin M (IgM) levels. 💻🧪",
|
10 |
-
"A 33-year-old woman who was recently involved in a motor vehicle accident presents to a medical clinic for a follow-up visit. 🚗🏥",
|
11 |
-
],
|
12 |
-
'votes': [0, 0, 0]
|
13 |
-
})
|
14 |
|
15 |
# Function to vote on a story
|
16 |
def vote_story(index):
|
17 |
st.session_state.df.loc[index, 'votes'] += 1
|
|
|
18 |
|
19 |
# Function to break story into sentences
|
20 |
def break_into_sentences(story):
|
@@ -27,6 +25,26 @@ def display_story(index, row):
|
|
27 |
st.text(sentence)
|
28 |
if st.button(f"Vote for Story {index + 1}"):
|
29 |
vote_story(index)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Main app
|
32 |
st.title('Medical Story Voting 🗳️')
|
@@ -42,3 +60,11 @@ for index, row in st.session_state.df.iterrows():
|
|
42 |
# Display total votes
|
43 |
st.markdown("### 📊 Vote Summary")
|
44 |
st.table(st.session_state.df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
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 🗳️')
|
|
|
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()
|