|
52 | 52 | **Try it:** |
53 | 53 | - Use the **Rows / Columns / Values** dropdowns in the toolbar to add or remove fields. |
54 | 54 | - Change the **Aggregation** (e.g. Sum → Average) in the toolbar. |
| 55 | +- Omit `rows`, `columns`, and `values` entirely to let the component auto-detect dimensions and measures. |
55 | 56 | - Click any data cell — the cell coordinates will appear below the table. |
56 | 57 | - Hover over the top-right of the toolbar to reveal the **utility menu**: |
57 | 58 | - **Reset** (↺) — resets the config to the original Python-supplied values |
|
96 | 97 | language="python", |
97 | 98 | ) |
98 | 99 |
|
99 | | -if result_basic.get("cell_click"): |
100 | | - st.info(f"Last cell click: {result_basic['cell_click']}") |
| 100 | +basic_cell_click = st.session_state.get("basic", {}).get("cell_click") |
| 101 | +if basic_cell_click: |
| 102 | + st.info(f"Last cell click: {basic_cell_click}") |
| 103 | + |
| 104 | +st.markdown("#### Auto-Detect Layout") |
| 105 | +st.markdown( |
| 106 | + """ |
| 107 | +If you omit `rows`, `columns`, and `values`, the component auto-detects dimensions |
| 108 | +and measures from the input data. This is useful for quick exploration when you |
| 109 | +want a sensible starting layout without pre-configuring the pivot. |
| 110 | +""" |
| 111 | +) |
| 112 | +st_pivot_table( |
| 113 | + df, |
| 114 | + key="basic_auto_detect", |
| 115 | +) |
101 | 116 |
|
102 | 117 |
|
103 | 118 | # --------------------------------------------------------------------------- |
|
156 | 171 |
|
157 | 172 |
|
158 | 173 | # --------------------------------------------------------------------------- |
159 | | -# Section 3: Filtering and Locked Mode |
| 174 | +# Section 3: Filtering, Locked Mode, and Non-Interactive Mode |
160 | 175 | # --------------------------------------------------------------------------- |
161 | 176 | st.divider() |
162 | | -st.subheader("3. Filtering and Locked Mode") |
| 177 | +st.subheader("3. Filtering, Locked Mode, and Non-Interactive Mode") |
163 | 178 |
|
164 | 179 | st.markdown( |
165 | 180 | """ |
|
169 | 184 | menus still work, **Show Values As** remains available on value headers, and |
170 | 185 | export still stays available as a viewer action. **Custom sorters** enforce a specific dimension order. |
171 | 186 |
|
| 187 | +**Non-interactive mode** (`interactive=False`) is the true read-only mode: |
| 188 | +the toolbar is hidden, header-menu config actions are disabled, but cell clicks |
| 189 | +and drill-down still work. |
| 190 | +
|
172 | 191 | **Try it (left table):** |
173 | 192 | - Click the **⋮** menu icon on the "Region" header → uncheck regions to filter them out. |
174 | 193 | - Use the search box to find specific values quickly. |
175 | 194 |
|
176 | | -**Right table** is **locked** — authoring actions like reset, swap, and config import/export |
| 195 | +**Middle table** is **locked** — authoring actions like reset, swap, and config import/export |
177 | 196 | are hidden, but **Export Data** remains available, the **Settings** gear shows |
178 | | -read-only view status, and you can still sort, filter, and change **Show Values As** |
179 | | -from the header menus. |
| 197 | +read-only view status plus **Expand/Collapse All** group controls, and you can |
| 198 | +still sort, filter, and change **Show Values As** from the header menus. |
180 | 199 |
|
181 | | -**API parameters used:** `hidden_from_aggregators`, `sorters`, `locked` |
| 200 | +**Right table** is **non-interactive** — there is no toolbar and no header-menu |
| 201 | +config UI, but cell clicks still work and drill-down remains enabled. |
| 202 | +
|
| 203 | +**API parameters used:** `hidden_from_aggregators`, `sorters`, `locked`, `interactive` |
182 | 204 | """ |
183 | 205 | ) |
184 | 206 |
|
185 | | -col_left, col_right = st.columns(2) |
| 207 | +col_left, col_middle, col_right = st.columns(3) |
186 | 208 |
|
187 | 209 | with col_left: |
188 | 210 | st.caption("Interactive (with custom sorters)") |
|
196 | 218 | null_handling="zero", |
197 | 219 | ) |
198 | 220 |
|
199 | | -with col_right: |
| 221 | +with col_middle: |
200 | 222 | st.caption("Locked mode") |
201 | 223 | st_pivot_table( |
202 | 224 | df, |
203 | 225 | key="locked", |
204 | | - rows=["Region"], |
| 226 | + rows=["Region", "Category"], |
205 | 227 | columns=["Year"], |
206 | | - values=["Revenue"], |
| 228 | + values=["Revenue", "Profit"], |
207 | 229 | locked=True, |
208 | 230 | hidden_from_aggregators=["Year", "Region"], |
| 231 | + show_subtotals=True, |
| 232 | + ) |
| 233 | + |
| 234 | +with col_right: |
| 235 | + st.caption("Non-interactive mode") |
| 236 | + result_noninteractive = st_pivot_table( |
| 237 | + df, |
| 238 | + key="noninteractive", |
| 239 | + rows=["Region"], |
| 240 | + columns=["Year"], |
| 241 | + values=["Revenue"], |
| 242 | + interactive=False, |
| 243 | + on_cell_click=lambda: None, |
209 | 244 | ) |
210 | 245 |
|
211 | 246 | with st.expander("View Code"): |
|
226 | 261 | st_pivot_table( |
227 | 262 | df, |
228 | 263 | key="locked", |
229 | | - rows=["Region"], |
| 264 | + rows=["Region", "Category"], |
230 | 265 | columns=["Year"], |
231 | | - values=["Revenue"], |
| 266 | + values=["Revenue", "Profit"], |
232 | 267 | locked=True, |
233 | 268 | hidden_from_aggregators=["Year", "Region"], |
| 269 | + show_subtotals=True, |
| 270 | +) |
| 271 | +
|
| 272 | +# Non-interactive — no toolbar or header-menu config actions |
| 273 | +st_pivot_table( |
| 274 | + df, |
| 275 | + key="noninteractive", |
| 276 | + rows=["Region"], |
| 277 | + columns=["Year"], |
| 278 | + values=["Revenue"], |
| 279 | + interactive=False, |
| 280 | + on_cell_click=lambda: None, |
234 | 281 | ) |
235 | 282 | """, |
236 | 283 | language="python", |
237 | 284 | ) |
238 | 285 |
|
| 286 | +noninteractive_cell_click = st.session_state.get("noninteractive", {}).get("cell_click") |
| 287 | +if noninteractive_cell_click: |
| 288 | + st.info(f"Non-interactive cell click: {noninteractive_cell_click}") |
| 289 | + |
239 | 290 |
|
240 | 291 | # --------------------------------------------------------------------------- |
241 | 292 | # Section 4: Subtotals and Grouping |
|
778 | 829 | - Use ``export_filename`` to customize the downloaded file name. The date |
779 | 830 | (``YYYY-MM-DD``) and file extension are appended automatically. |
780 | 831 | Defaults to ``"pivot-table"`` (e.g. ``pivot-table_2026-03-09.csv``). |
| 832 | +- This demo sets `export_filename="sales-export-demo"` so you can see the custom |
| 833 | + filename behavior in the downloaded file. |
781 | 834 | """ |
782 | 835 | ) |
783 | 836 |
|
|
791 | 844 | number_format={"Revenue": "$,.0f", "Profit": "$,.0f"}, |
792 | 845 | show_totals=True, |
793 | 846 | interactive=True, |
| 847 | + export_filename="sales-export-demo", |
794 | 848 | ) |
795 | 849 |
|
796 | 850 | with st.expander("View Code"): |
|
804 | 858 | values=["Revenue", "Profit"], |
805 | 859 | number_format={"Revenue": "$,.0f", "Profit": "$,.0f"}, |
806 | 860 | show_totals=True, |
| 861 | + export_filename="sales-export-demo", |
807 | 862 | ) |
808 | 863 | # Then use the Download icon in the toolbar utility menu. |
809 | 864 | """, |
|
0 commit comments