Update app.py
Browse files
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 |
-
|
8 |
-
def
|
9 |
-
|
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 |
-
|
39 |
-
|
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 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
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}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|