awacke1 commited on
Commit
f8eddcb
·
1 Parent(s): 8c0c3a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -72
app.py CHANGED
@@ -1,80 +1,41 @@
1
  import streamlit as st
2
- import streamlit.components.v1 as components
3
- import plotly.express as px
4
- import pandas as pd
5
  import geopandas as gpd
 
6
 
7
- # Function to generate HTML with textarea for speech synthesis
8
- def generate_speech_textarea(text_to_speak):
9
- documentHTML5 = '''
10
- <!DOCTYPE html>
11
- <html>
12
- <head>
13
- <title>State Trivia</title>
14
- <script type="text/javascript">
15
- function readAloud() {{
16
- const text = document.getElementById("textArea").value;
17
- const speech = new SpeechSynthesisUtterance(text);
18
- window.speechSynthesis.speak(speech);
19
- }}
20
- </script>
21
- </head>
22
- <body>
23
- <h1>🔊 State Trivia</h1>
24
- <textarea id="textArea" rows="10" cols="80" readonly>'''
25
- documentHTML5 += text_to_speak
26
- documentHTML5 += '''
27
- </textarea>
28
- <br>
29
- <button onclick="readAloud()">🔊 Read Aloud</button>
30
- </body>
31
- </html>
32
- '''
33
- components.html(documentHTML5, width=1280, height=500)
34
-
35
- # Main code
36
- st.title('United States Trivia 🇺🇸')
37
 
38
- # Manually adding the state abbreviations to the GeoDataFrame
39
- usa = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
40
- usa = usa[usa.continent == 'North America']
41
- usa['postal'] = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
42
 
43
- # Generate dropdown menu to select a state
44
- states = ['MN', 'CA', 'WA', 'FL', 'TX', 'NY', 'NV', 'TN', 'HI', 'SD']
45
- selected_state = st.selectbox("Choose a state:", states)
 
 
 
 
 
 
 
 
 
46
 
47
- # Find the selected state's geometry
48
- selected_state_geom = usa[usa.postal == selected_state].geometry.iloc[0]
49
 
50
- # Plot the selected state using Plotly
51
- fig = px.choropleth(usa,
52
- geojson=usa.geometry,
53
- locations=usa.index,
54
- scope="usa")
55
- fig.update_geos(fitbounds="locations")
56
- fig.add_trace(px.scatter_geo(lat=[selected_state_geom.centroid.y],
57
- lon=[selected_state_geom.centroid.x]).data[0])
58
- st.plotly_chart(fig)
59
 
60
- # Show fascinating facts based on selected state
61
- if selected_state == 'MN':
62
- generate_speech_textarea("Minnesota (MN) ❄️\n1️⃣ Home to over 10,000 lakes 🌊\n2️⃣ Boundary Waters Canoe Area 🛶\n3️⃣ Largest Company: UnitedHealth Group, Revenue: $257.1B 💼")
63
- elif selected_state == 'CA':
64
- generate_speech_textarea("California (CA) 🌞\n1️⃣ Home of Hollywood 🎬\n2️⃣ Golden Gate Bridge 🌉\n3️⃣ Largest Company: Apple, Revenue: $365.8B 🍎")
65
- elif selected_state == 'WA':
66
- generate_speech_textarea("Washington (WA) 🌲\n1️⃣ Origin of Starbucks ☕\n2️⃣ Mount Rainier 🗻\n3️⃣ Largest Company: Amazon, Revenue: $386B 📦")
67
- elif selected_state == 'FL':
68
- generate_speech_textarea("Florida (FL) 🌴\n1️⃣ Home to Walt Disney World 🏰\n2️⃣ Florida Keys 🐠\n3️⃣ Largest Company: World Fuel Services, Revenue: $27.0B ⛽")
69
- elif selected_state == 'TX':
70
- generate_speech_textarea("Texas (TX) 🤠\n1️⃣ Birthplace of Texas Country Music 🎶\n2️⃣ Tex-Mex Cuisine 🌮\n3️⃣ Largest Company: ExxonMobil, Revenue: $265.7B 🛢️")
71
- elif selected_state == 'NY':
72
- generate_speech_textarea("New York (NY) 🗽\n1️⃣ Home of Wall Street 💵\n2️⃣ The Big Apple 🍎\n3️⃣ Largest Company: JPMorgan Chase, Revenue: $119.5B 🏦")
73
- elif selected_state == 'NV':
74
- generate_speech_textarea("Nevada (NV) 🎲\n1️⃣ Las Vegas Strip 🎰\n2️⃣ Area 51 👽\n3️⃣ Largest Company: Las Vegas Sands, Revenue: $13.7B 🏨")
75
- elif selected_state == 'TN':
76
- generate_speech_textarea("Tennessee (TN) 🎵\n1️⃣ Home of Country Music 🎸\n2️⃣ Tennessee Whiskey 🥃\n3️⃣ Largest Company: FedEx, Revenue: $69.2B ✈️")
77
- elif selected_state == 'HI':
78
- generate_speech_textarea("Hawaii (HI) 🏝️\n1️⃣ Aloha Spirit 🌺\n2️⃣ Surfing Paradise 🏄‍♀️\n3️⃣ Largest Company: Hawaiian Electric Industries, Revenue: $2.9B ⚡")
79
- elif selected_state == 'SD':
80
- generate_speech_textarea("South Dakota (SD) 🌾\n1️⃣ Mount Rushmore 🗿\n2️⃣ Badlands National Park 🏞️\n3️⃣ Largest Company: Sanford Health, Revenue: $4.5B 🏥")
 
1
  import streamlit as st
 
 
 
2
  import geopandas as gpd
3
+ import plotly.express as px
4
 
5
+ @st.cache
6
+ def load_usa_map():
7
+ return gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))[lambda df: df.abbrev.isin(state_data.keys())]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def generate_speech_textarea(text):
10
+ st.markdown(f'<p id="speech" hidden>{text}</p><button onclick="speechSynthesis.speak(new SpeechSynthesisUtterance(document.getElementById(\'speech\').textContent))">🔊 Speak</button>', unsafe_allow_html=True)
 
 
11
 
12
+ state_data = {
13
+ 'MN': {'name': 'Minnesota', 'trivia': '1️⃣ Home to over 10,000 lakes\n2️⃣ Boundary Waters Canoe Area', 'largest_company': 'UnitedHealth Group', 'revenue': 257.1},
14
+ 'CA': {'name': 'California', 'trivia': '1️⃣ Known for Hollywood\n2️⃣ Golden Gate Bridge', 'largest_company': 'Apple', 'revenue': 365.8},
15
+ 'WA': {'name': 'Washington', 'trivia': '1️⃣ Origin of Starbucks\n2️⃣ Mount Rainier', 'largest_company': 'Amazon', 'revenue': 386.1},
16
+ 'FL': {'name': 'Florida', 'trivia': '1️⃣ Home to Walt Disney World\n2️⃣ Florida Keys', 'largest_company': 'World Fuel Services', 'revenue': 27.0},
17
+ 'TX': {'name': 'Texas', 'trivia': '1️⃣ Birthplace of Texas Country Music\n2️⃣ Tex-Mex Cuisine', 'largest_company': 'ExxonMobil', 'revenue': 265.7},
18
+ 'NY': {'name': 'New York', 'trivia': '1️⃣ Home of Wall Street\n2️⃣ The Big Apple', 'largest_company': 'JPMorgan Chase', 'revenue': 119.5},
19
+ 'NV': {'name': 'Nevada', 'trivia': '1️⃣ Las Vegas Strip\n2️⃣ Area 51', 'largest_company': 'Las Vegas Sands', 'revenue': 13.7},
20
+ 'TN': {'name': 'Tennessee', 'trivia': '1️⃣ Home of Country Music\n2️⃣ Tennessee Whiskey', 'largest_company': 'FedEx', 'revenue': 69.2},
21
+ 'HI': {'name': 'Hawaii', 'trivia': '1️⃣ Aloha Spirit\n2️⃣ Surfing Paradise', 'largest_company': 'Hawaiian Electric Industries', 'revenue': 2.9},
22
+ 'SD': {'name': 'South Dakota', 'trivia': '1️⃣ Mount Rushmore\n2️⃣ Badlands National Park', 'largest_company': 'Sanford Health', 'revenue': 4.5}
23
+ }
24
 
25
+ usa = load_usa_map()
26
+ selected_state = st.selectbox("📍 Choose a state:", list(state_data.keys()), format_func=lambda x: f"{state_data[x]['name']} ({x})")
27
 
28
+ state_geom_df = usa[usa.abbrev == selected_state]
29
+ if not state_geom_df.empty:
30
+ geom = state_geom_df.geometry.iloc[0]
31
+ fig = px.choropleth(usa, geojson=usa.geometry, locations=usa.index, scope="usa")
32
+ fig.add_trace(px.scatter_geo(lat=[geom.centroid.y], lon=[geom.centroid.x]).data[0])
33
+ st.plotly_chart(fig)
 
 
 
34
 
35
+ data = state_data[selected_state]
36
+ text = f"🔍 Trivia:\n{data['trivia']}\n🏢 Largest Company: {data['largest_company']}\n💵 Revenue: {data['revenue']}B"
37
+ with st.expander(f"{state_data[selected_state]['name']} ({selected_state}) 😃"):
38
+ generate_speech_textarea(text)
39
+ st.markdown(text)
40
+ else:
41
+ st.error(f"🚫 No data for {selected_state}.")