create an app that visualizes traffic incidents in austin
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import plotly.express as px
st.title("Austin Traffic Incidents")
# Load data
@st.cache_data
def load_data():
# NOTE: Using corsproxy.io because we're in a WASM environment. If running locally,
# you can remove the corsproxy.io prefix.
url = "https://corsproxy.io/?https://data.austintexas.gov/resource/dx9v-zd7x.csv"
df = pd.read_csv(url)
df['published_date'] = pd.to_datetime(df['published_date'])
return df
# Show loading message
with st.spinner('Loading traffic data...'):
df = load_data()
# Sidebar filters
st.sidebar.header("Filters")
# Date filter
date_range = st.sidebar.date_input(
"Select Date Range",
value=(df['published_date'].min().date(), df['published_date'].max().date()),
min_value=df['published_date'].min().date(),
max_value=df['published_date'].max().date()
)
# Issue type filter
issue_types = sorted(df['issue_reported'].unique())
selected_types = st.sidebar.multiselect(
"Select Incident Types",
issue_types,
default=issue_types[:5]
)
# Filter data based on selections
filtered_df = df[
(df['published_date'].dt.date >= date_range[0]) &
(df['published_date'].dt.date <= date_range[1]) &
(df['issue_reported'].isin(selected_types))
]
# Main content
col1, col2 = st.columns([2, 1])
with col1:
st.subheader("Incident Map")
fig = px.scatter_mapbox(
filtered_df,
lat='latitude',
lon='longitude',
color='issue_reported',
hover_data=['address', 'issue_reported', 'published_date'],
zoom=10,
height=500
)
fig.update_layout(
mapbox_style="open-street-map",
margin={"r":0,"t":0,"l":0,"b":0}
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("Incidents by Type")
type_counts = filtered_df['issue_reported'].value_counts()
fig_bar = px.bar(
x=type_counts.values,
y=type_counts.index,
orientation='h',
title="Number of Incidents by Type"
)
st.plotly_chart(fig_bar, use_container_width=True)
# Show raw data
if st.checkbox("Show Raw Data"):
st.subheader("Raw Data")
st.dataframe(filtered_df)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?