File size: 3,382 Bytes
0e1662c
e8345f0
6910cc8
ac0345f
0e1662c
4b7cdde
 
 
 
 
 
 
 
 
 
3b1655c
e8345f0
 
814d3fc
 
 
6910cc8
814d3fc
 
 
 
 
 
 
 
 
6910cc8
917b8ba
7b2dd92
 
814d3fc
 
 
 
 
 
d9a15ca
0e1662c
6910cc8
 
3b1655c
6910cc8
 
 
 
 
4b7cdde
6910cc8
 
4b7cdde
814d3fc
6910cc8
 
 
 
 
 
 
 
 
3b1655c
6910cc8
4b7cdde
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
import streamlit.components.v1 as components
import plotly.express as px
import geopandas as gpd

# State trivia and facts stored in a centralized Python dictionary
state_data = {
    'Minnesota': {'abbreviation': 'MN', 'trivia': '1️⃣ Home to over 10,000 lakes\n2️⃣ Boundary Waters Canoe Area', 'largest_company': 'UnitedHealth Group', 'revenue': 257.1},
    'California': {'abbreviation': 'CA', 'trivia': '1️⃣ Known for Hollywood\n2️⃣ Tech Hub: Silicon Valley', 'largest_company': 'Apple', 'revenue': 365.8},
    'Washington': {'abbreviation': 'WA', 'trivia': '1️⃣ Home to Microsoft\n2️⃣ Known for Coffee Shops', 'largest_company': 'Amazon', 'revenue': 386.1},
    'Tennessee': {'abbreviation': 'TN', 'trivia': '1️⃣ Home to Country Music\n2️⃣ Famous for BBQ', 'largest_company': 'FedEx', 'revenue': 84.2},
    'Hawaii': {'abbreviation': 'HI', 'trivia': '1️⃣ Known for Beautiful Beaches\n2️⃣ Unique Culture', 'largest_company': 'Hawaiian Electric Industries', 'revenue': 2.7},
    'South Dakota': {'abbreviation': 'SD', 'trivia': '1️⃣ Home to Mount Rushmore\n2️⃣ Known for the Badlands', 'largest_company': 'Sanford Health', 'revenue': 5.1},
}

# 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 🇺🇸')

# 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:", list(state_data.keys()))

# Find the selected state's geometry
selected_state_geom = usa[usa.name == 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
with st.expander(f"Show Trivia for {selected_state} 😃"):
    trivia_text = state_data[selected_state]['trivia']
    trivia_text += f"\n🏢 Largest Company: {state_data[selected_state]['largest_company']}\n💵 Revenue: ${state_data[selected_state]['revenue']}B"
    
    if st.button('Read Aloud 📢'):
        generate_speech_textarea(trivia_text)
    st.markdown(trivia_text)