Spaces:
Build error
Build error
File size: 5,266 Bytes
fdfcd02 3887b17 f28151c fdfcd02 f28151c fdfcd02 3887b17 fdfcd02 3887b17 fdfcd02 f28151c c6795cd f28151c c6795cd f28151c c6795cd f28151c d45b3ad c94158d |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import streamlit as st
import csv
import os
import random
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
def save_data(name, email, phone):
if not os.path.exists('community.csv'):
with open('community.csv', mode='w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Name', 'Email', 'Phone'])
with open('community.csv', mode='a') as csv_file:
fieldnames = ['Name', 'Email', 'Phone']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if os.stat('community.csv').st_size == 0:
writer.writeheader()
writer.writerow({'Name': name, 'Email': email, 'Phone': phone})
with open(f'{name}.txt', mode='w') as file:
file.write(f'Name: {name}\nEmail: {email}\nPhone: {phone}\n')
def reset_data():
os.remove('community.csv')
for file in os.listdir():
if file.endswith('.txt'):
os.remove(file)
def show_data():
count = 0
with open('community.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
count += 1
if count == 3:
st.write(f'{row["Name"]}: {row["Email"]} - {row["Phone"]}')
count = 0
def app():
states = ["Minnesota", "Florida", "California", "Washington", "Maine", "Texas"]
top_n = 10
health_conditions = [
{"condition": "π Heart disease", "emoji": "π", "spending": 214.3},
{"condition": "π€ Trauma-related disorders", "emoji": "π", "spending": 198.6},
{"condition": "π¦ Cancer", "emoji": "ποΈ", "spending": 171.0},
{"condition": "π§ Mental disorders", "emoji": "π§", "spending": 150.8},
{"condition": "𦴠Osteoarthritis and joint disorders", "emoji": "π₯", "spending": 142.4},
{"condition": "π Diabetes", "emoji": "π©Έ", "spending": 107.4},
{"condition": "π« Chronic obstructive pulmonary disease and asthma", "emoji": "π«", "spending": 91.0},
{"condition": "π©Ί Hypertension", "emoji": "π", "spending": 83.9},
{"condition": "π¬ Hyperlipidemia", "emoji": "π¬", "spending": 83.9},
{"condition": "𦴠Back problems", "emoji": "π§", "spending": 67.0}
]
total_spending = sum([hc["spending"] for hc in health_conditions])
df_top_conditions = pd.DataFrame(health_conditions)
locations = ["CA", "FL", "MN", "WA", "ME", "TX"]
fig_map = px.choropleth(locations=locations, locationmode="USA-states", color=[1, 2, 3, 4, 5, 6], scope="usa")
#fig_map.update_layout(geo=dict(bgcolor="rgba(0,0,0,0)", lakecolor="rgb(255, 255, 255)")
fig_map.update_layout(geo=dict(bgcolor= "rgba(0, 0, 0, 0)", lakecolor="rgb(255, 255, 255)"))
fig_bar = go.Figure(go.Bar(x=df_top_conditions["emoji"] + " " + df_top_conditions["condition"], y=df_top_conditions["spending"], marker_color="#1f77b4"))
fig_bar.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)",
yaxis_title="Spending ($B)", plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=False)))
# fig_bar.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)",
# yaxis_title="Spending ($B)", plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=False))
col1, col2 = st.columns([2, 1])
with col1:
st.plotly_chart(fig_map, use_container_width=True)
with col2:
st.plotly_chart(fig_bar, use_container_width=True)
st.title('Community Hub Form')
name = st.text_input('Name', key='name_input')
email = st.text_input('Email', key='email_input')
phone = st.text_input('Phone', key='phone_input')
if st.button('Submit', key='submit_button'):
save_data(name, email, phone)
st.success('Form submitted!')
if st.button('Reset', key='reset_button'):
reset_data()
st.success('Data reset!')
if st.button('Show data', key='show_data_button'):
show_data()
st.write('Emails/SMS')
with open('community.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
name = row['Name']
email = row['Email']
phone = row['Phone']
st.write(f'{name}: {email} - {phone}')
if st.button('Reply', key=f'reply_{name}'):
st.text_input(f'Reply to {name}', key=f'reply_input_{name}')
if st.button(f'Vote up {name}', key=f'vote_up_{name}'):
st.success(f'{name} has been voted up!')
if st.button(f'No thanks to {name}', key=f'vote_down_{name}'):
st.warning(f'{name} has been voted down!')
with open(f'{name}.txt', mode='w') as file:
file.write(f'Life points: 0\n')
st.write('')
st.write('Add tip')
tip = st.text_input('Tip', key='tip_input')
if st.button('Submit tip', key='submit_tip_button'):
emoji = random.choice(['π', 'π', 'π', 'π‘'])
with open(f'{name}.txt', mode='a') as file:
file.write(f'Tip' + ': {tip} {emoji}\nLife points: 10\n')
st.success('Tip submitted!')
if name == 'main':
app()
|