awacke1's picture
Update app.py
c94158d
raw
history blame
5.12 kB
import streamlit as st
import csv
import os
import random
def save_data(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():
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!')
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
# Define the states and conditions of interest
states = ["Minnesota", "Florida", "California", "Washington", "Maine", "Texas"]
top_n = 10
# Define the list dictionary of top 10 health conditions descending by cost, with emojis
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 the spending values
total_spending = sum([hc["spending"] for hc in health_conditions])
# Create a DataFrame from the list dictionary
df_top_conditions = pd.DataFrame(health_conditions)
# Create the map graph using Plotly Express
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)"))
# Create the bar chart using Plotly Graph Objects
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))
# Display the map and the bar chart in Streamlit
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)
if __name__ == '__main__':
app()