Update src/streamlit_app.py
Browse files- src/streamlit_app.py +24 -13
src/streamlit_app.py
CHANGED
|
@@ -13,6 +13,10 @@ st.set_page_config(layout="wide", page_title="Multiplex Coop Housing Filter")
|
|
| 13 |
st.title("🗺️ Multiplex Coop Housing Filter (Hugging Face Data)")
|
| 14 |
st.write("This app uses the `ProjectMultiplexCoop/PropertyBoundaries` dataset from Hugging Face. Draw a polygon on the map to spatially filter properties. Use the form below to apply additional filters based on property attributes. **Note: FSI, Building Coverage, Height, and Stories are synthetic for demonstration as they are not directly available in the dataset.**")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# --- 1. Load Data from Hugging Face and Process ---
|
| 17 |
@st.cache_data
|
| 18 |
def load_and_process_data():
|
|
@@ -202,8 +206,7 @@ with st.form("attribute_filters"):
|
|
| 202 |
st.info("Adjust filters and click 'Apply Attribute Filters'.")
|
| 203 |
|
| 204 |
|
| 205 |
-
# --- 4. Display Filtered Data on a New Map and as a Table
|
| 206 |
-
# The expander is expanded by default to show results immediately after filtering
|
| 207 |
with st.expander("Filtered Properties Display", expanded=True):
|
| 208 |
if not filtered_df.empty:
|
| 209 |
# Calculate bounds for filtered data to set appropriate zoom
|
|
@@ -235,12 +238,20 @@ with st.expander("Filtered Properties Display", expanded=True):
|
|
| 235 |
fill_opacity=0.5
|
| 236 |
).add_to(filtered_m)
|
| 237 |
|
| 238 |
-
# Convert filtered_df
|
| 239 |
filtered_gdf = gpd.GeoDataFrame(filtered_df, geometry='geometry')
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
# Add filtered polygons to the map as GeoJSON layer
|
| 242 |
folium.GeoJson(
|
| 243 |
-
|
| 244 |
style_function=lambda x: {
|
| 245 |
'fillColor': 'green',
|
| 246 |
'color': 'darkgreen',
|
|
@@ -257,13 +268,11 @@ with st.expander("Filtered Properties Display", expanded=True):
|
|
| 257 |
st_folium(filtered_m, width=1000, height=500)
|
| 258 |
|
| 259 |
st.subheader("Filtered Properties Table")
|
| 260 |
-
# Limit rows displayed in the dataframe to prevent MessageSizeError
|
| 261 |
-
MAX_ROWS_DISPLAY = 1000 # Define a reasonable limit
|
| 262 |
display_cols = ['PARCELID', 'zn_type', 'zn_area', 'fsi_total', 'prcnt_cver', 'height_metres', 'stories', 'ADDRESS_NUMBER', 'LINEAR_NAME_FULL']
|
| 263 |
|
| 264 |
-
if len(filtered_df) >
|
| 265 |
-
st.warning(f"Displaying only the first {
|
| 266 |
-
st.dataframe(filtered_df[display_cols].head(
|
| 267 |
else:
|
| 268 |
st.dataframe(filtered_df[display_cols])
|
| 269 |
|
|
@@ -284,10 +293,12 @@ st.markdown("---")
|
|
| 284 |
st.markdown(
|
| 285 |
"""
|
| 286 |
**Troubleshooting Large Data:**
|
| 287 |
-
If you encounter a `MessageSizeError`
|
| 288 |
-
it means the data size exceeds Streamlit's
|
| 289 |
-
You can
|
| 290 |
-
|
|
|
|
|
|
|
| 291 |
However, be aware that increasing this limit can lead to longer loading times and higher
|
| 292 |
memory consumption in your browser and on the Streamlit server.
|
| 293 |
"""
|
|
|
|
| 13 |
st.title("🗺️ Multiplex Coop Housing Filter (Hugging Face Data)")
|
| 14 |
st.write("This app uses the `ProjectMultiplexCoop/PropertyBoundaries` dataset from Hugging Face. Draw a polygon on the map to spatially filter properties. Use the form below to apply additional filters based on property attributes. **Note: FSI, Building Coverage, Height, and Stories are synthetic for demonstration as they are not directly available in the dataset.**")
|
| 15 |
|
| 16 |
+
# --- Configuration Constants ---
|
| 17 |
+
MAX_ROWS_DATAFRAME_DISPLAY = 1000 # Max rows to show in st.dataframe
|
| 18 |
+
MAX_MAP_FEATURES_DISPLAY = 5000 # Max features to plot on the Folium map to prevent MessageSizeError
|
| 19 |
+
|
| 20 |
# --- 1. Load Data from Hugging Face and Process ---
|
| 21 |
@st.cache_data
|
| 22 |
def load_and_process_data():
|
|
|
|
| 206 |
st.info("Adjust filters and click 'Apply Attribute Filters'.")
|
| 207 |
|
| 208 |
|
| 209 |
+
# --- 4. Display Filtered Data on a New Map and as a Table ---
|
|
|
|
| 210 |
with st.expander("Filtered Properties Display", expanded=True):
|
| 211 |
if not filtered_df.empty:
|
| 212 |
# Calculate bounds for filtered data to set appropriate zoom
|
|
|
|
| 238 |
fill_opacity=0.5
|
| 239 |
).add_to(filtered_m)
|
| 240 |
|
| 241 |
+
# Convert filtered_df to GeoDataFrame for plotting
|
| 242 |
filtered_gdf = gpd.GeoDataFrame(filtered_df, geometry='geometry')
|
| 243 |
|
| 244 |
+
# --- Apply map display limit ---
|
| 245 |
+
features_to_plot_count = len(filtered_gdf)
|
| 246 |
+
if features_to_plot_count > MAX_MAP_FEATURES_DISPLAY:
|
| 247 |
+
st.warning(f"Displaying a random sample of {MAX_MAP_FEATURES_DISPLAY} properties on the map (out of {features_to_plot_count} total filtered) to prevent performance issues.")
|
| 248 |
+
filtered_gdf_for_map = filtered_gdf.sample(MAX_MAP_FEATURES_DISPLAY, random_state=42)
|
| 249 |
+
else:
|
| 250 |
+
filtered_gdf_for_map = filtered_gdf
|
| 251 |
+
|
| 252 |
# Add filtered polygons to the map as GeoJSON layer
|
| 253 |
folium.GeoJson(
|
| 254 |
+
filtered_gdf_for_map.to_json(),
|
| 255 |
style_function=lambda x: {
|
| 256 |
'fillColor': 'green',
|
| 257 |
'color': 'darkgreen',
|
|
|
|
| 268 |
st_folium(filtered_m, width=1000, height=500)
|
| 269 |
|
| 270 |
st.subheader("Filtered Properties Table")
|
|
|
|
|
|
|
| 271 |
display_cols = ['PARCELID', 'zn_type', 'zn_area', 'fsi_total', 'prcnt_cver', 'height_metres', 'stories', 'ADDRESS_NUMBER', 'LINEAR_NAME_FULL']
|
| 272 |
|
| 273 |
+
if len(filtered_df) > MAX_ROWS_DATAFRAME_DISPLAY:
|
| 274 |
+
st.warning(f"Displaying only the first {MAX_ROWS_DATAFRAME_DISPLAY} rows of the filtered data ({len(filtered_df)} total properties). Download the full dataset below.")
|
| 275 |
+
st.dataframe(filtered_df[display_cols].head(MAX_ROWS_DATAFRAME_DISPLAY))
|
| 276 |
else:
|
| 277 |
st.dataframe(filtered_df[display_cols])
|
| 278 |
|
|
|
|
| 293 |
st.markdown(
|
| 294 |
"""
|
| 295 |
**Troubleshooting Large Data:**
|
| 296 |
+
If you still encounter a `MessageSizeError` despite the display limits,
|
| 297 |
+
it means the data size still exceeds Streamlit's internal limit, or the sampled data is still too complex.
|
| 298 |
+
You can try decreasing `MAX_MAP_FEATURES_DISPLAY` and `MAX_ROWS_DATAFRAME_DISPLAY` further.
|
| 299 |
+
Alternatively, you can increase Streamlit's default message size limit by adding
|
| 300 |
+
`server.maxMessageSize = <size_in_mb>` (e.g., `server.maxMessageSize = 500`)
|
| 301 |
+
to your Streamlit `config.toml` file.
|
| 302 |
However, be aware that increasing this limit can lead to longer loading times and higher
|
| 303 |
memory consumption in your browser and on the Streamlit server.
|
| 304 |
"""
|