awacke1 commited on
Commit
1d136fa
Β·
1 Parent(s): 9550c1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -105
app.py CHANGED
@@ -1,125 +1,63 @@
1
  import streamlit as st
2
- import csv
3
- import os
4
- import random
5
  import pandas as pd
 
6
  import plotly.graph_objects as go
7
  import plotly.express as px
8
 
9
- def save_data(name, email, phone):
10
- if not os.path.exists('community.csv'):
11
- with open('community.csv', mode='w') as csv_file:
12
- writer = csv.writer(csv_file)
13
- writer.writerow(['Name', 'Email', 'Phone'])
14
- with open('community.csv', mode='a') as csv_file:
15
- fieldnames = ['Name', 'Email', 'Phone']
16
- writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
17
- if os.stat('community.csv').st_size == 0:
18
- writer.writeheader()
19
- writer.writerow({'Name': name, 'Email': email, 'Phone': phone})
20
- with open(f'{name}.txt', mode='w') as file:
21
- file.write(f'Name: {name}\nEmail: {email}\nPhone: {phone}\n')
22
-
23
- def reset_data():
24
- os.remove('community.csv')
25
- for file in os.listdir():
26
- if file.endswith('.txt'):
27
- os.remove(file)
28
-
29
- def show_data():
30
- count = 0
31
- with open('community.csv', mode='r') as csv_file:
32
- csv_reader = csv.DictReader(csv_file)
33
- for row in csv_reader:
34
- count += 1
35
- if count == 3:
36
- st.write(f'{row["Name"]}: {row["Email"]} - {row["Phone"]}')
37
- count = 0
38
-
39
- def app():
40
- states = ["Minnesota", "Florida", "California", "Washington", "Maine", "Texas"]
41
  top_n = 10
42
 
 
43
  health_conditions = [
44
- {"condition": "πŸ’” Heart disease", "emoji": "πŸ’—", "spending": 214.3},
45
- {"condition": "πŸ€• Trauma-related disorders", "emoji": "πŸš‘", "spending": 198.6},
46
- {"condition": "πŸ¦€ Cancer", "emoji": "πŸŽ—οΈ", "spending": 171.0},
47
- {"condition": "🧠 Mental disorders", "emoji": "🧘", "spending": 150.8},
48
- {"condition": "🦴 Osteoarthritis and joint disorders", "emoji": "πŸ₯", "spending": 142.4},
49
- {"condition": "πŸ’‰ Diabetes", "emoji": "🩸", "spending": 107.4},
50
- {"condition": "🫁 Chronic obstructive pulmonary disease and asthma", "emoji": "πŸ«€", "spending": 91.0},
51
- {"condition": "🩺 Hypertension", "emoji": "πŸ’‰", "spending": 83.9},
52
- {"condition": "πŸ”¬ Hyperlipidemia", "emoji": "πŸ”¬", "spending": 83.9},
53
- {"condition": "🦴 Back problems", "emoji": "🧍", "spending": 67.0}
54
  ]
55
 
56
- total_spending = sum([hc["spending"] for hc in health_conditions])
57
-
58
  df_top_conditions = pd.DataFrame(health_conditions)
59
 
60
- locations = ["CA", "FL", "MN", "WA", "ME", "TX"]
61
- fig_map = px.choropleth(locations=locations, locationmode="USA-states", color=[1, 2, 3, 4, 5, 6], scope="usa")
62
- #fig_map.update_layout(geo=dict(bgcolor="rgba(0,0,0,0)", lakecolor="rgb(255, 255, 255)")
63
-
64
- fig_map.update_layout(geo=dict(bgcolor= "rgba(0, 0, 0, 0)", lakecolor="rgb(255, 255, 255)"))
65
-
66
- fig_bar = go.Figure(go.Bar(x=df_top_conditions["emoji"] + " " + df_top_conditions["condition"], y=df_top_conditions["spending"], marker_color="#1f77b4"))
67
- fig_bar.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)",
68
- yaxis_title="Spending ($B)", plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=False))
69
-
70
-
71
- # fig_bar.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)",
72
- # yaxis_title="Spending ($B)", plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=False))
73
-
74
- col1, col2 = st.columns([2, 1])
75
- with col1:
76
- st.plotly_chart(fig_map, use_container_width=True)
77
- with col2:
78
- st.plotly_chart(fig_bar, use_container_width=True)
79
-
80
- st.title('Community Hub Form')
81
 
82
- name = st.text_input('Name', key='name_input')
83
- email = st.text_input('Email', key='email_input')
84
- phone = st.text_input('Phone', key='phone_input')
 
 
85
 
86
- if st.button('Submit', key='submit_button'):
87
- save_data(name, email, phone)
88
- st.success('Form submitted!')
 
 
 
 
89
 
90
- if st.button('Reset', key='reset_button'):
91
- reset_data()
92
- st.success('Data reset!')
93
 
94
- if st.button('Show data', key='show_data_button'):
95
- show_data()
 
 
96
 
97
- st.write('Emails/SMS')
98
- with open('community.csv', mode='r') as csv_file:
99
- csv_reader = csv.DictReader(csv_file)
100
- for row in csv_reader:
101
- name = row['Name']
102
- email = row['Email']
103
- phone = row['Phone']
104
- st.write(f'{name}: {email} - {phone}')
105
- if st.button('Reply', key=f'reply_{name}'):
106
- st.text_input(f'Reply to {name}', key=f'reply_input_{name}')
107
- if st.button(f'Vote up {name}', key=f'vote_up_{name}'):
108
- st.success(f'{name} has been voted up!')
109
- if st.button(f'No thanks to {name}', key=f'vote_down_{name}'):
110
- st.warning(f'{name} has been voted down!')
111
- with open(f'{name}.txt', mode='w') as file:
112
- file.write(f'Life points: 0\n')
113
- st.write('')
114
 
115
- st.write('Add tip')
116
- tip = st.text_input('Tip', key='tip_input')
117
- if st.button('Submit tip', key='submit_tip_button'):
118
- emoji = random.choice(['πŸ‘', 'πŸ‘Œ', 'πŸ‘', 'πŸ’‘'])
119
- with open(f'{name}.txt', mode='a') as file:
120
- file.write(f'Tip' + ': {tip} {emoji}\nLife points: 10\n')
121
- st.success('Tip submitted!')
122
 
123
- if name == 'main':
124
- app()
125
 
 
 
1
  import streamlit as st
 
 
 
2
  import pandas as pd
3
+ import random
4
  import plotly.graph_objects as go
5
  import plotly.express as px
6
 
7
+ def health_game():
8
+ # Define the states and conditions of interest
9
+ states = ["Minnesota", "Florida", "California"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  top_n = 10
11
 
12
+ # Define the list dictionary of top 10 health conditions descending by cost, with emojis, treatment recommendation and potential savings
13
  health_conditions = [
14
+ {"condition": "πŸ’” Heart disease", "emoji": "πŸ’—", "spending": 214.3, "treatment": "Regular checkups with a cardiologist", "savings": "$1000"},
15
+ {"condition": "πŸ€• Trauma-related disorders", "emoji": "πŸš‘", "spending": 198.6, "treatment": "Counseling and physical therapy", "savings": "$500"},
16
+ {"condition": "πŸ¦€ Cancer", "emoji": "πŸŽ—οΈ", "spending": 171.0, "treatment": "Early detection and treatment", "savings": "$2000"},
17
+ {"condition": "🧠 Mental disorders", "emoji": "🧘", "spending": 150.8, "treatment": "Therapy and medication", "savings": "$1500"},
18
+ {"condition": "🦴 Osteoarthritis and joint disorders", "emoji": "πŸ₯", "spending": 142.4, "treatment": "Low-impact exercise and physical therapy", "savings": "$800"},
19
+ {"condition": "πŸ’‰ Diabetes", "emoji": "🩸", "spending": 107.4, "treatment": "Regular checkups and medication", "savings": "$1200"},
20
+ {"condition": "🫁 Chronic obstructive pulmonary disease and asthma", "emoji": "πŸ«€", "spending": 91.0, "treatment": "Inhalers and breathing exercises", "savings": "$600"},
21
+ {"condition": "🩺 Hypertension", "emoji": "πŸ’‰", "spending": 83.9, "treatment": "Lifestyle changes and medication", "savings": "$900"},
22
+ {"condition": "πŸ”¬ Hyperlipidemia", "emoji": "πŸ”¬", "spending": 83.9, "treatment": "Lifestyle changes and medication", "savings": "$700"},
23
+ {"condition": "🦴 Back problems", "emoji": "🧍", "spending": 67.0, "treatment": "Physical therapy and exercise", "savings": "$400"}
24
  ]
25
 
26
+ # Create a DataFrame from the list dictionary
 
27
  df_top_conditions = pd.DataFrame(health_conditions)
28
 
29
+ # Calculate the total spending
30
+ total_spending = round(df_top_conditions["spending"].sum(), 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ # Define the roll function
33
+ def roll():
34
+ rolls = [random.randint(1, 10) for _ in range(1000)]
35
+ frequencies = [rolls.count(i) for i in range(1, 11)]
36
+ return frequencies
37
 
38
+ # Define the sunburst chart
39
+ fig_sunburst = go.Figure(go.Sunburst(
40
+ labels=df_top_conditions["emoji"] + " " + df_top_conditions["condition"],
41
+ parents=[""] * top_n,
42
+ values=df_top_conditions["spending"],
43
+ maxdepth=2
44
+ ))
45
 
46
+ # Customize the layout of the sunburst chart
47
+
48
+ fig_sunburst.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)")
49
 
50
+ # Display the sunburst chart and variants per condition in the Streamlit app
51
+ st.plotly_chart(fig_sunburst)
52
+ condition_idx = st.selectbox("Select your current health condition", df_top_conditions.index)
53
+ row = df_top_conditions.loc[condition_idx]
54
 
55
+ st.write(f"Based on the severity of your {row['condition']}, we recommend {row['treatment']} for early treatment. This could save you up to {row['savings']} in healthcare costs.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ frequencies = roll()
58
+ fig_bar = px.bar(x=[f"Variant {i}" for i in range(1, 11)], y=frequencies[:10], labels={'x': 'Variant', 'y': 'Frequency'})
59
+ fig_bar.update_layout(title=f"Variants of {row['condition']} ({row['emoji']})")
60
+ st.plotly_chart(fig_bar)
 
 
 
61
 
 
 
62
 
63
+ health_game()