USStates / app.py
awacke1's picture
Update app.py
957aadb
raw
history blame
5.23 kB
import streamlit as st
import streamlit.components.v1 as components
import geopandas as gpd
import matplotlib.pyplot as plt
import plotly.express as px
import json
# Function to generate HTML with textarea for speech synthesis
def generate_speech_textarea(text_to_speak):
documentHTML5 = '''
<!DOCTYPE html>
<html>
<head>
<title>Read It Aloud</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>πŸ”Š Read It Aloud</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)
# Function to display the state outline
def plot_state_outline(state_code):
# Read U.S. geometries file
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Filter data for the given state
gdf_state = gdf[gdf['iso_a3'] == 'USA']
#gdf_state = gdf[gdf['iso_a3'] == 'state_code']
# Plot the geometry
ax = gdf_state.boundary.plot()
plt.title(f"{state_code} State Outline")
st.pyplot(plt)
# Load the GeoJSON file
geojson_path = 'gz_2010_us_040_00_500k.json'
with open(geojson_path, 'r') as file:
us_states_geojson = json.load(file)
# Mapping of state codes to state names
state_names = {
'MN': 'Minnesota',
'CA': 'California',
'WA': 'Washington',
'FL': 'Florida',
'TX': 'Texas',
'NY': 'New York',
'NV': 'Nevada'
}
# Function to display the state outline
def plot_state_outline(state_code):
state_name = state_names.get(state_code, 'Unknown')
fig = px.choropleth(locations=[state_name], geojson=us_states_geojson,
featureidkey="properties.NAME",
projection="mercator")
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(title=f"{state_name} State Outline")
st.plotly_chart(fig)
# States list and associated icons
states = ['MN', 'CA', 'WA', 'FL', 'TX', 'NY', 'NV']
icons = ['πŸ¦†', '🌴', '🍎', '🌞', '🀠', 'πŸ—½', '🎰']
# Display maps for each state
for state, icon in zip(states, icons):
st.write(f"{icon} {state}")
plot_state_outline(state)
# States list and associated icons
# states = ['MN', 'CA', 'WA', 'FL', 'TX', 'NY', 'NV']
# icons = ['πŸ¦†', '🌴', '🍎', '🌞', '🀠', 'πŸ—½', '🎰']
# Main code
st.title('U.S. States Trivia πŸ—ΊοΈ')
for i, (state, icon) in enumerate(zip(states, icons)):
st.markdown(f"{i + 1}. {state} {icon}")
# Expanders for each state to outline fascinating facts
with st.expander(f"See Fascinating Facts about {state}"):
text_to_speak = ""
if state == 'MN':
text_to_speak = "πŸ¦† **Minnesota** \n🏞️ Known as the 'Land of 10,000 Lakes' \n🎣 Famous for its fishing \nπŸ›Ά Boundary Waters offers incredible canoeing \nπŸŽ“ Home to prestigious colleges \n❄️ Cold winters but lovely summers."
elif state == 'CA':
text_to_speak = "🌴 **California** \nπŸŒ‰ Home to the Golden Gate Bridge \n🎬 Center of the American entertainment industry \nπŸ‡ Famous for Napa Valley's wine \n🌲 Home to Redwood National Park \nπŸ„β€β™€οΈ Excellent beaches and surf spots."
elif state == 'WA':
text_to_speak = "🍎 **Washington** \nβ˜• Known for its coffee culture \nπŸ—» Home to Mount Rainier \n🍏 Leading apple-producing state \n🐟 Rich in seafood, especially salmon \n🌧️ Known for its rainy weather."
elif state == 'FL':
text_to_speak = "🌞 **Florida** \n🏝️ Famous for its beaches \n🎒 Home to various amusement parks like Disney World \nπŸš€ Space launches from Cape Canaveral \n🐊 Known for the Everglades and alligators \n🍊 Major orange producer."
elif state == 'TX':
text_to_speak = "🀠 **Texas** \nπŸ›’οΈ Known for its oil and gas industry \nπŸ– Famous for its barbecue \n🎸 Rich musical heritage \nπŸ„ Home to many cattle ranches \n🌡 Includes part of the Chihuahuan Desert."
elif state == 'NY':
text_to_speak = "πŸ—½ **New York** \nπŸ™οΈ Home to New York City, the largest city in the U.S. \n🍎 Known as the Big Apple \n🎭 Major hub for arts and culture \n🏞️ Adirondack Mountains offer outdoor adventures \nπŸ• Famous for its style of pizza."
elif state == 'NV':
text_to_speak = "🎰 **Nevada** \nπŸŒ† Known for Las Vegas and its casinos \n🏜️ Includes part of the Mojave Desert \nπŸŽͺ Entertainment is a major industry \nπŸ’Ž Known for the Hoover Dam \nπŸ‘½ Area 51 is located here."
st.markdown(text_to_speak)
plot_state_outline(state)
if st.button(f"πŸ”Š Read {state}'s Facts Aloud"):
generate_speech_textarea(text_to_speak)