Update src/streamlit_app.py
Browse files- src/streamlit_app.py +19 -9
src/streamlit_app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import altair as alt
|
| 4 |
import json
|
| 5 |
import urllib.request
|
|
|
|
| 6 |
|
| 7 |
# 🌟 关键:解除 Altair 5000 行的限制,允许柱状图和折线图使用全量数据渲染
|
| 8 |
alt.data_transformers.disable_max_rows()
|
|
@@ -21,10 +22,6 @@ st.markdown(
|
|
| 21 |
This interactive article walks you through 2026 Chicago crime data drawn directly from
|
| 22 |
the [Chicago Data Portal](https://data.cityofchicago.org/) to help you explore the
|
| 23 |
geography, timing, and social context of crime in one of America's largest cities.
|
| 24 |
-
The dataset records every reported crime incident in 2026, including the exact location,
|
| 25 |
-
date and time, crime type, and the police district that handled it. Each row is one
|
| 26 |
-
reported incident. We also include community-level socioeconomic data to examine the
|
| 27 |
-
relationship between poverty and crime rates across Chicago's neighborhoods.
|
| 28 |
"""
|
| 29 |
)
|
| 30 |
|
|
@@ -34,12 +31,25 @@ st.markdown(
|
|
| 34 |
|
| 35 |
@st.cache_data(show_spinner="Loading Chicago crime data from CSV...")
|
| 36 |
def load_crime_data():
|
| 37 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
df_raw = pd.read_csv("Crimes_-_2026_20260417.csv", low_memory=False)
|
| 41 |
except Exception as e:
|
| 42 |
-
st.error(f"
|
| 43 |
return pd.DataFrame()
|
| 44 |
|
| 45 |
df = df_raw.copy()
|
|
@@ -115,7 +125,7 @@ districts = alt.Data(values=district_geojson["features"])
|
|
| 115 |
communities = alt.Data(values=community_geojson["features"])
|
| 116 |
|
| 117 |
if df.empty:
|
| 118 |
-
st.error("Crime data could not be loaded.")
|
| 119 |
st.stop()
|
| 120 |
|
| 121 |
df_geo = df.dropna(subset=["latitude", "longitude"]).copy()
|
|
|
|
| 3 |
import altair as alt
|
| 4 |
import json
|
| 5 |
import urllib.request
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# 🌟 关键:解除 Altair 5000 行的限制,允许柱状图和折线图使用全量数据渲染
|
| 9 |
alt.data_transformers.disable_max_rows()
|
|
|
|
| 22 |
This interactive article walks you through 2026 Chicago crime data drawn directly from
|
| 23 |
the [Chicago Data Portal](https://data.cityofchicago.org/) to help you explore the
|
| 24 |
geography, timing, and social context of crime in one of America's largest cities.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
)
|
| 27 |
|
|
|
|
| 31 |
|
| 32 |
@st.cache_data(show_spinner="Loading Chicago crime data from CSV...")
|
| 33 |
def load_crime_data():
|
| 34 |
+
"""自动寻找目录下的 Crimes 数据集,彻底告别文件名拼写报错"""
|
| 35 |
+
|
| 36 |
+
# 1. 自动扫描当前文件夹
|
| 37 |
+
current_files = os.listdir('.')
|
| 38 |
+
csv_files = [f for f in current_files if 'Crimes' in f and f.endswith('.csv')]
|
| 39 |
+
|
| 40 |
+
# 2. 如果什么都没找到,把当前文件夹里到底有什么打印出来,方便排错
|
| 41 |
+
if not csv_files:
|
| 42 |
+
st.error(f"找不到 CSV 文件!当前文件夹里只有这些文件: {current_files}")
|
| 43 |
+
return pd.DataFrame()
|
| 44 |
+
|
| 45 |
+
# 3. 抓取找到的第一个文件
|
| 46 |
+
target_file = csv_files[0]
|
| 47 |
+
st.success(f"自动寻路成功!正在读取: {target_file}")
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
df_raw = pd.read_csv(target_file, low_memory=False)
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
+
st.error(f"文件找到了,但读取失败: {e}")
|
| 53 |
return pd.DataFrame()
|
| 54 |
|
| 55 |
df = df_raw.copy()
|
|
|
|
| 125 |
communities = alt.Data(values=community_geojson["features"])
|
| 126 |
|
| 127 |
if df.empty:
|
| 128 |
+
st.error("Crime data could not be loaded. Please check the logs.")
|
| 129 |
st.stop()
|
| 130 |
|
| 131 |
df_geo = df.dropna(subset=["latitude", "longitude"]).copy()
|