USStates / app.py
awacke1's picture
Update app.py
6910cc8
raw
history blame
4.03 kB
import streamlit as st
import streamlit.components.v1 as components
import plotly.express as px
import pandas as pd
import geopandas as gpd
# Function to generate HTML with textarea for speech synthesis
def generate_speech_textarea(text_to_speak):
documentHTML5 = '''
<!DOCTYPE html>
<html>
<head>
<title>State Trivia</title>
<script type="text/javascript">
function readAloud() {{
const text = document.getElementById("textArea").value;
const speech = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(speech);
}}
</script>
</head>
<body>
<h1>🔊 State Trivia</h1>
<textarea id="textArea" rows="10" cols="80" readonly>'''
documentHTML5 = documentHTML5 + text_to_speak
documentHTML5 = documentHTML5 + '''
</textarea>
<br>
<button onclick="readAloud()">🔊 Read Aloud</button>
</body>
</html>
'''
components.html(documentHTML5, width=1280, height=500)
# Main code
st.title('United States Trivia 🇺🇸')
# List of states and associated icons
states = ['MN', 'CA', 'WA', 'FL', 'TX', 'NY', 'NV', 'TN', 'HI', 'SD']
icons = ['❄️', '🌞', '🌲', '🌴', '🤠', '🗽', '🎲', '🎵', '🏝️', '🌾']
# Load the USA shapefile using GeoPandas
usa = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
usa = usa[usa.continent == 'North America']
# Generate dropdown menu to select a state
selected_state = st.selectbox("Choose a state:", states)
# Find the selected state's geometry
selected_state_geom = usa[usa.postal == selected_state].geometry.iloc[0]
# Plot the selected state using Plotly
fig = px.choropleth(usa,
geojson=usa.geometry,
locations=usa.index,
scope="usa")
fig.update_geos(fitbounds="locations")
fig.add_trace(px.scatter_geo(lat=[selected_state_geom.centroid.y],
lon=[selected_state_geom.centroid.x]).data[0])
st.plotly_chart(fig)
# Show fascinating facts based on selected state
if selected_state == 'MN':
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")
elif selected_state == 'CA':
generate_speech_textarea("California (CA) \n1️⃣ Home of Hollywood \n2️⃣ Golden Gate Bridge \n3️⃣ Largest Company: Apple, Revenue: $365.8B")
elif selected_state == 'WA':
generate_speech_textarea("Washington (WA) \n1️⃣ Origin of Starbucks \n2️⃣ Mount Rainier \n3️⃣ Largest Company: Amazon, Revenue: $386B")
elif selected_state == 'FL':
generate_speech_textarea("Florida (FL) \n1️⃣ Home to Walt Disney World \n2️⃣ Florida Keys \n3️⃣ Largest Company: World Fuel Services, Revenue: $27.0B")
elif selected_state == 'TX':
generate_speech_textarea("Texas (TX) \n1️⃣ Birthplace of Texas Country Music \n2️⃣ Tex-Mex Cuisine \n3️⃣ Largest Company: ExxonMobil, Revenue: $265.7B")
elif selected_state == 'NY':
generate_speech_textarea("New York (NY) \n1️⃣ Home of Wall Street \n2️⃣ The Big Apple \n3️⃣ Largest Company: JPMorgan Chase, Revenue: $119.5B")
elif selected_state == 'NV':
generate_speech_textarea("Nevada (NV) \n1️⃣ Las Vegas Strip \n2️⃣ Area 51 \n3️⃣ Largest Company: Las Vegas Sands, Revenue: $13.7B")
elif selected_state == 'TN':
generate_speech_textarea("Tennessee (TN) \n1️⃣ Home of Country Music \n2️⃣ Tennessee Whiskey \n3️⃣ Largest Company: FedEx, Revenue: $69.2B")
elif selected_state == 'HI':
generate_speech_textarea("Hawaii (HI) \n1️⃣ Aloha Spirit \n2️⃣ Surfing Paradise \n3️⃣ Largest Company: Hawaiian Electric Industries, Revenue: $2.9B")
elif selected_state == 'SD':
generate_speech_textarea("South Dakota (SD) \n1️⃣ Mount Rushmore \n2️⃣ Badlands National Park \n3️⃣ Largest Company: Sanford Health, Revenue: $4.5B")