Create an app showing an interactive congressional district map that identifies congress people that are on any committees related to the Department of Interior or Energy. Make another layer to the map that has aquariums and zoos located in these districts Features: - Interactive map centered on the United States - Toggle layers: * Congressional Districts Controls: - Time slider - Color schemes - Map style (street/satellite) Include zoom/pan and stats panel showing data summaries.
To upload files, please first save the app
import streamlit as st
import pydeck as pdk
import pandas as pd
import geopandas as gpd
import json
import plotly.express as px
from datetime import datetime, timedelta
import numpy as np
st.set_page_config(layout="wide", page_title="Congressional Districts Analysis")
# Load and cache data
@st.cache_data
def load_congressional_data():
# Note: This is sample data - in a real app you'd fetch from an API
# Sample coordinates covering continental US
districts = pd.DataFrame({
'district_id': range(1, 51),
'state': ['CA', 'NY', 'TX', 'FL'] * 13,
'representative': [f'Rep {i}' for i in range(1, 51)],
'committee': ['Energy', 'Interior', 'Both', 'None'] * 13,
'latitude': np.random.uniform(25, 49, 50),
'longitude': np.random.uniform(-125, -65, 50),
})
return districts
@st.cache_data
def load_zoo_data():
# Sample zoo/aquarium data
zoos = pd.DataFrame({
'name': [f'Zoo/Aquarium {i}' for i in range(1, 31)],
'type': ['Zoo', 'Aquarium'] * 15,
'latitude': np.random.uniform(25, 49, 30),
'longitude': np.random.uniform(-125, -65, 30),
'annual_visitors': np.random.randint(100000, 2000000, 30)
})
return zoos
# Load data
districts_df = load_congressional_data()
zoos_df = load_zoo_data()
# Sidebar controls
st.sidebar.title("Map Controls")
# Layer toggles
show_districts = st.sidebar.checkbox("Show Congressional Districts", True)
show_zoos = st.sidebar.checkbox("Show Zoos/Aquariums", True)
# Time slider (for demonstration - could be used with historical data)
year = st.sidebar.slider("Select Year", 2018, 2023, 2023)
# Color scheme selector
color_scheme = st.sidebar.selectbox(
"Color Scheme",
["Default", "Viridis", "Plasma"]
)
# Map style selector
map_style = st.sidebar.selectbox(
"Map Style",
["road", "satellite"],
format_func=lambda x: x.title()
)
# Main content
st.title("Congressional Districts & Conservation Facilities Map")
# Create map layers
districts_layer = pdk.Layer(
"ScatterplotLayer",
data=districts_df,
get_position=["longitude", "latitude"],
get_radius=50000, # 50km radius
get_fill_color=[255, 0, 0, 140],
pickable=True,
visible=show_districts,
auto_highlight=True
)
zoos_layer = pdk.Layer(
"ScatterplotLayer",
data=zoos_df,
get_position=["longitude", "latitude"],
get_radius=25000, # 25km radius
get_fill_color=[0, 255, 0, 140],
pickable=True,
visible=show_zoos,
auto_highlight=True
)
# Map view state
view_state = pdk.ViewState(
latitude=39.8283,
longitude=-98.5795,
zoom=4,
pitch=0
)
# Create the map
deck = pdk.Deck(
layers=[districts_layer, zoos_layer],
initial_view_state=view_state,
map_style=f"mapbox://{map_style}-v9",
tooltip={
"html": "<b>Type:</b> {type}<br/>"
"<b>Name:</b> {name}<br/>"
"<b>Representative:</b> {representative}<br/>"
"<b>Committee:</b> {committee}",
"style": {
"backgroundColor": "steelblue",
"color": "white"
}
}
)
# Display the map
st.pydeck_chart(deck, use_container_width=True)
# Stats Panel
col1, col2 = st.columns(2)
with col1:
st.subheader("Congressional Statistics")
committee_stats = districts_df['committee'].value_counts()
fig1 = px.pie(values=committee_stats.values,
names=committee_stats.index,
title="Committee Distribution")
st.plotly_chart(fig1)
with col2:
st.subheader("Zoo/Aquarium Statistics")
facility_stats = zoos_df['type'].value_counts()
fig2 = px.bar(zoos_df,
x='type',
y='annual_visitors',
title="Annual Visitors by Facility Type",
color='type')
st.plotly_chart(fig2)
# Footer with data summary
st.markdown("---")
st.markdown(f"""
### Data Summary
- Total Congressional Districts: {len(districts_df)}
- Total Zoos/Aquariums: {len(zoos_df)}
- Year: {year}
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?