Skip to content

Commit 08d83f9

Browse files
committed
Fix pandas Styler cell limit error for large datasets
Added conditional styling logic to avoid StreamlitAPIException when dataframes exceed 262,144 cells (pandas styler limit). Now only applies color styling to Grade column when dataset is small enough (< 100K cells). For large datasets, displays plain dataframe with info message. Fixes error when loading All organizations (38,995+ records).
1 parent 7a96c6d commit 08d83f9

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

data_explorer.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,15 @@ def color_grade(val):
12841284
color = get_quality_color(val)
12851285
return f'background-color: {color}; color: #000000; font-weight: bold;'
12861286

1287-
styled_df = site_df.style.applymap(color_grade, subset=['Grade'])
1288-
st.dataframe(styled_df, use_container_width=True, height=400)
1287+
# Only apply styling if dataframe is small enough (avoid pandas styler limits)
1288+
total_cells = site_df.shape[0] * site_df.shape[1]
1289+
if total_cells <= 100000: # Safe limit for pandas styler
1290+
styled_df = site_df.style.applymap(color_grade, subset=['Grade'])
1291+
st.dataframe(styled_df, use_container_width=True, height=400)
1292+
else:
1293+
# For large datasets, display without styling
1294+
st.dataframe(site_df, use_container_width=True, height=400)
1295+
st.info(f"ℹ️ Grade colors disabled for large dataset ({len(site_df):,} rows)")
12891296

12901297
# Top 5 sites needing improvement
12911298
st.markdown("#### 🔧 Top 5 Sites Needing Improvement")
@@ -1327,8 +1334,16 @@ def color_grade(val):
13271334
})
13281335

13291336
org_df = pd.DataFrame(org_scores)
1330-
styled_org_df = org_df.style.applymap(color_grade, subset=['Grade'])
1331-
st.dataframe(styled_org_df, use_container_width=True, height=400)
1337+
1338+
# Only apply styling if dataframe is small enough (avoid pandas styler limits)
1339+
total_cells = org_df.shape[0] * org_df.shape[1]
1340+
if total_cells <= 100000: # Safe limit for pandas styler
1341+
styled_org_df = org_df.style.applymap(color_grade, subset=['Grade'])
1342+
st.dataframe(styled_org_df, use_container_width=True, height=400)
1343+
else:
1344+
# For large datasets, display without styling
1345+
st.dataframe(org_df, use_container_width=True, height=400)
1346+
st.info(f"ℹ️ Grade colors disabled for large dataset ({len(org_df):,} rows)")
13321347

13331348
# Top 5 organizations needing improvement
13341349
st.markdown("#### 🔧 Top 5 Organizations Needing Improvement")

0 commit comments

Comments
 (0)