Spaces:
Sleeping
Sleeping
add sum to the right, make the heatmap larger
Browse files
app.py
CHANGED
|
@@ -4,6 +4,11 @@ import numpy as np
|
|
| 4 |
import base64, pickle, textwrap
|
| 5 |
import plotly.express as px
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ---------------------------------------------------
|
| 8 |
# Decode embedded data
|
| 9 |
# ---------------------------------------------------
|
|
@@ -17,7 +22,6 @@ df = pickle.loads(base64.b64decode(df_encoded))
|
|
| 17 |
# Helper: wrap long labels
|
| 18 |
# ---------------------------------------------------
|
| 19 |
def wrap_labels(labels, width=40):
|
| 20 |
-
import textwrap
|
| 21 |
return ["<br>".join(textwrap.wrap(lbl, width)) for lbl in labels]
|
| 22 |
|
| 23 |
# ---------------------------------------------------
|
|
@@ -28,7 +32,7 @@ st.sidebar.title("Filters")
|
|
| 28 |
def_opts = sorted(df['Dept Track'].dropna().astype(str).unique())
|
| 29 |
def_stand = sorted(df['Standing'].dropna().astype(str).unique())
|
| 30 |
|
| 31 |
-
# Clear filters
|
| 32 |
if st.sidebar.button("Clear All Filters"):
|
| 33 |
st.session_state.pop('standing', None)
|
| 34 |
st.session_state.pop('dept', None)
|
|
@@ -48,13 +52,14 @@ dept_sel = st.sidebar.multiselect(
|
|
| 48 |
key='dept'
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# Filter names
|
| 52 |
mask = (
|
| 53 |
-
df['Standing'].astype(str).isin(
|
| 54 |
-
df['Dept Track'].astype(str).isin(
|
| 55 |
)
|
| 56 |
name_opts = sorted(df.loc[mask, 'Name'].astype(str).unique())
|
| 57 |
|
|
|
|
| 58 |
if 'names' not in st.session_state or st.session_state.names is None:
|
| 59 |
st.session_state.names = name_opts.copy()
|
| 60 |
else:
|
|
@@ -72,28 +77,35 @@ name_sel = st.sidebar.multiselect(
|
|
| 72 |
# ---------------------------------------------------
|
| 73 |
st.title("Faculty Heatmap Explorer")
|
| 74 |
|
| 75 |
-
#
|
| 76 |
if not name_sel:
|
| 77 |
st.warning("No faculty selected — please choose at least one.")
|
| 78 |
fig = px.imshow(
|
| 79 |
-
[[0]],
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
st.plotly_chart(fig, use_container_width=True)
|
| 83 |
else:
|
| 84 |
-
#
|
| 85 |
sum_df = None
|
| 86 |
for name in name_sel:
|
| 87 |
mat = filled_matrices[name]
|
| 88 |
sum_df = mat if sum_df is None else sum_df.add(mat, fill_value=0)
|
| 89 |
avg_df = sum_df.div(len(name_sel))
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
|
|
|
| 95 |
fig = px.imshow(
|
| 96 |
-
|
| 97 |
x=wrapped_x,
|
| 98 |
y=wrapped_y,
|
| 99 |
labels={'color':'Avg value'},
|
|
@@ -101,6 +113,7 @@ else:
|
|
| 101 |
title=f"Avg Heatmap for {len(name_sel)} Faculty"
|
| 102 |
)
|
| 103 |
fig.update_yaxes(autorange='reversed')
|
|
|
|
|
|
|
| 104 |
st.plotly_chart(fig, use_container_width=True)
|
| 105 |
|
| 106 |
-
# End of app
|
|
|
|
| 4 |
import base64, pickle, textwrap
|
| 5 |
import plotly.express as px
|
| 6 |
|
| 7 |
+
# ---------------------------------------------------
|
| 8 |
+
# Page configuration for wide layout
|
| 9 |
+
# ---------------------------------------------------
|
| 10 |
+
st.set_page_config(page_title="Faculty Heatmap Explorer", layout="wide")
|
| 11 |
+
|
| 12 |
# ---------------------------------------------------
|
| 13 |
# Decode embedded data
|
| 14 |
# ---------------------------------------------------
|
|
|
|
| 22 |
# Helper: wrap long labels
|
| 23 |
# ---------------------------------------------------
|
| 24 |
def wrap_labels(labels, width=40):
|
|
|
|
| 25 |
return ["<br>".join(textwrap.wrap(lbl, width)) for lbl in labels]
|
| 26 |
|
| 27 |
# ---------------------------------------------------
|
|
|
|
| 32 |
def_opts = sorted(df['Dept Track'].dropna().astype(str).unique())
|
| 33 |
def_stand = sorted(df['Standing'].dropna().astype(str).unique())
|
| 34 |
|
| 35 |
+
# Clear filters button resets session state
|
| 36 |
if st.sidebar.button("Clear All Filters"):
|
| 37 |
st.session_state.pop('standing', None)
|
| 38 |
st.session_state.pop('dept', None)
|
|
|
|
| 52 |
key='dept'
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Filter names based on selected Standing & Dept
|
| 56 |
mask = (
|
| 57 |
+
df['Standing'].astype(str).isin(standing_sel) &
|
| 58 |
+
df['Dept Track'].astype(str).isin(dept_sel)
|
| 59 |
)
|
| 60 |
name_opts = sorted(df.loc[mask, 'Name'].astype(str).unique())
|
| 61 |
|
| 62 |
+
# Initialize or prune stored names
|
| 63 |
if 'names' not in st.session_state or st.session_state.names is None:
|
| 64 |
st.session_state.names = name_opts.copy()
|
| 65 |
else:
|
|
|
|
| 77 |
# ---------------------------------------------------
|
| 78 |
st.title("Faculty Heatmap Explorer")
|
| 79 |
|
| 80 |
+
# Display heatmap (with optional row sums)
|
| 81 |
if not name_sel:
|
| 82 |
st.warning("No faculty selected — please choose at least one.")
|
| 83 |
fig = px.imshow(
|
| 84 |
+
[[0]],
|
| 85 |
+
labels={'x':'','y':'','color':'value'},
|
| 86 |
+
text_auto='.2f',
|
| 87 |
+
title="No faculty selected"
|
| 88 |
)
|
| 89 |
st.plotly_chart(fig, use_container_width=True)
|
| 90 |
else:
|
| 91 |
+
# Combine matrices
|
| 92 |
sum_df = None
|
| 93 |
for name in name_sel:
|
| 94 |
mat = filled_matrices[name]
|
| 95 |
sum_df = mat if sum_df is None else sum_df.add(mat, fill_value=0)
|
| 96 |
avg_df = sum_df.div(len(name_sel))
|
| 97 |
|
| 98 |
+
# Add a sum column on the right
|
| 99 |
+
avg_df_ext = avg_df.copy()
|
| 100 |
+
avg_df_ext['Sum'] = avg_df_ext.sum(axis=1)
|
| 101 |
+
|
| 102 |
+
# Wrap y-axis labels for compact display
|
| 103 |
+
wrapped_y = wrap_labels(list(avg_df_ext.index))
|
| 104 |
+
wrapped_x = list(avg_df_ext.columns)
|
| 105 |
|
| 106 |
+
# Create heatmap
|
| 107 |
fig = px.imshow(
|
| 108 |
+
avg_df_ext.values,
|
| 109 |
x=wrapped_x,
|
| 110 |
y=wrapped_y,
|
| 111 |
labels={'color':'Avg value'},
|
|
|
|
| 113 |
title=f"Avg Heatmap for {len(name_sel)} Faculty"
|
| 114 |
)
|
| 115 |
fig.update_yaxes(autorange='reversed')
|
| 116 |
+
# Set a taller figure height for wide layout
|
| 117 |
+
fig.update_layout(height=800, margin=dict(l=70, r=70, t=70, b=70))
|
| 118 |
st.plotly_chart(fig, use_container_width=True)
|
| 119 |
|
|
|