Spaces:
Build error
Build error
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() | |