Create an app showing an interactive NYC map visualization. Mock data: - Food truck locations (100 points) - Pedestrian activity heatmap Features: - Interactive map centered on NYC - Toggle layers: * Food trucks with popups * Pedestrian heatmap * Animated routes * Taxi clusters Controls: - Time slider - Borough filters - Color schemes - Map style (street/satellite) - Heatmap and cluster settings Include zoom/pan and stats panel showing data summaries.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
import pydeck as pdk
import plotly.express as px
from datetime import datetime, timedelta
# Page config
st.set_page_config(layout="wide", page_title="NYC Interactive Map")
# Initialize session state
if "layer_states" not in st.session_state:
st.session_state.layer_states = {
"food_trucks": True,
"pedestrians": True,
"routes": False,
"taxis": False
}
# Title and description
st.title("NYC Interactive Map")
st.markdown("Explore New York City's dynamic urban landscape with this interactive visualization.")
# Sidebar controls
st.sidebar.header("Controls")
# Time control
current_time = st.sidebar.slider(
"Time of Day",
min_value=datetime.strptime("00:00", "%H:%M"),
max_value=datetime.strptime("23:59", "%H:%M"),
value=datetime.strptime("12:00", "%H:%M"),
format="HH:mm"
)
# Borough filter
boroughs = ['All', 'Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island']
selected_borough = st.sidebar.selectbox("Borough", boroughs)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
["mapbox://styles/mapbox/streets-v11", "mapbox://styles/mapbox/satellite-v9"]
)
# Layer toggles
st.sidebar.header("Layers")
for layer in st.session_state.layer_states:
st.session_state.layer_states[layer] = st.sidebar.checkbox(
layer.replace("_", " ").title(),
value=st.session_state.layer_states[layer]
)
# Generate mock data
@st.cache_data
def generate_mock_data():
# Food truck locations
np.random.seed(42)
n_trucks = 100
trucks = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.1, n_trucks),
'longitude': np.random.normal(-74.0060, 0.1, n_trucks),
'name': [f"Food Truck #{i}" for i in range(n_trucks)],
'rating': np.random.uniform(3.0, 5.0, n_trucks),
'cuisine': np.random.choice(['Mexican', 'Italian', 'American', 'Asian', 'Mediterranean'], n_trucks)
})
# Pedestrian activity
n_pedestrians = 1000
pedestrians = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.15, n_pedestrians),
'longitude': np.random.normal(-74.0060, 0.15, n_pedestrians),
'count': np.random.exponential(100, n_pedestrians)
})
return trucks, pedestrians
trucks_df, pedestrians_df = generate_mock_data()
# Layout
col1, col2 = st.columns([7, 3])
with col1:
# Create map layers
layers = []
if st.session_state.layer_states["food_trucks"]:
food_trucks_layer = pdk.Layer(
'ScatterplotLayer',
data=trucks_df,
get_position=['longitude', 'latitude'],
get_color=[255, 0, 0, 160],
get_radius=50,
pickable=True,
opacity=0.8,
)
layers.append(food_trucks_layer)
if st.session_state.layer_states["pedestrians"]:
heatmap_layer = pdk.Layer(
'HeatmapLayer',
data=pedestrians_df,
get_position=['longitude', 'latitude'],
get_weight='count',
pickable=False,
opacity=0.4,
)
layers.append(heatmap_layer)
# Map view
view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=0
)
# Render map
st.pydeck_chart(pdk.Deck(
map_style=map_style,
initial_view_state=view_state,
layers=layers,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/><b>Cuisine:</b> {cuisine}<br/><b>Rating:</b> {rating}/5.0",
"style": {
"backgroundColor": "steelblue",
"color": "white"
}
}
))
with col2:
# Stats panel
st.header("Statistics")
# Food truck stats
st.subheader("Food Trucks")
st.metric("Total Food Trucks", len(trucks_df))
# Cuisine distribution
fig = px.pie(trucks_df, names='cuisine', title='Cuisine Distribution')
st.plotly_chart(fig, use_container_width=True)
# Rating distribution
fig = px.histogram(trucks_df, x='rating', nbins=20, title='Rating Distribution')
st.plotly_chart(fig, use_container_width=True)
# Pedestrian activity
st.subheader("Pedestrian Activity")
st.metric("Total Pedestrian Count", int(pedestrians_df['count'].sum()))
# Footer
st.markdown("---")
st.markdown("Data is simulated for demonstration purposes.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?