UCS2014 commited on
Commit
feaaaa6
·
verified ·
1 Parent(s): fb17fb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -49
app.py CHANGED
@@ -264,7 +264,7 @@ def _add_sheet(sheets: dict, order: list, name: str, df: pd.DataFrame, ndigits:
264
  order.append(name)
265
 
266
  def _available_sections() -> list[str]:
267
- """Compute which sections exist, to pre-check them in the export UI."""
268
  res = st.session_state.get("results", {})
269
  sections = []
270
  if "Train" in res: sections += ["Training","Training_Metrics","Training_Summary"]
@@ -352,56 +352,49 @@ def build_export_workbook(selected: list[str], ndigits: int = 3, do_autofit: boo
352
  fname = f"YM_Export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
353
  return bio.getvalue(), fname, order
354
 
355
- def render_export_button(phase_key: str, default_sections: list[str]) -> None:
356
- """Export UI dropdown checklists for sheets & options; fixed rounding=3."""
 
 
 
 
 
 
 
 
357
  st.divider()
358
  st.markdown("### Export to Excel")
359
 
360
- # ---- Dropdown checklist: sheets ----
361
- all_sections = [
362
- "Training","Training_Metrics","Training_Summary",
363
- "Testing","Testing_Metrics","Testing_Summary",
364
- "Validation","Validation_Metrics","Validation_Summary","Validation_OOR",
365
- "Prediction","Prediction_Summary",
366
- "Training_Ranges","Info"
367
- ]
368
  selected_sheets = st.multiselect(
369
  "Sheets to include",
370
- options=all_sections,
371
- default=default_sections,
372
- help="Pick the sheets to include in the Excel export.",
 
373
  key=f"sheets_{phase_key}",
374
  )
375
 
376
- # ---- Dropdown checklist: export options ----
377
- export_options = st.multiselect(
378
- "Export options",
379
- options=[
380
- "Auto-fit columns & freeze header",
381
- "Append timestamp to filename",
382
- ],
383
- default=["Auto-fit columns & freeze header", "Append timestamp to filename"],
384
- help="Choose extra export behaviors.",
385
- key=f"opts_{phase_key}",
386
- )
387
- do_autofit = "Auto-fit columns & freeze header" in export_options
388
- add_stamp = "Append timestamp to filename" in export_options
389
-
390
- base_name = st.text_input("Base filename", value="YM_Export", key=f"basename_{phase_key}")
391
-
392
- # Build workbook (fixed rounding=3)
393
- data, _, names = build_export_workbook(selected=selected_sheets, ndigits=3, do_autofit=do_autofit)
394
 
 
395
  if names:
396
  st.caption("Will include: " + ", ".join(names))
397
 
398
- suffix = "_" + datetime.now().strftime("%Y%m%d_%H%M%S") if add_stamp else ""
399
- file_name = (base_name or "YM_Export") + suffix + ".xlsx"
400
-
401
  st.download_button(
402
  "⬇️ Export Excel",
403
  data=(data or b""),
404
- file_name=file_name if data else "YM_Export.xlsx",
405
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
406
  disabled=(data is None),
407
  key=f"download_{phase_key}",
@@ -747,14 +740,14 @@ if st.session_state.app_step == "dev":
747
 
748
  st.session_state.results["Train"]=tr; st.session_state.results["Test"]=te
749
  st.session_state.results["m_train"]={
750
- "R": pearson_r(tr[TARGET], tr[PRED_COL]),
751
  "RMSE": rmse(tr[TARGET], tr[PRED_COL]),
752
- "MAE": mean_absolute_error(tr[TARGET], tr[PRED_COL])
753
  }
754
  st.session_state.results["m_test"]={
755
- "R": pearson_r(te[TARGET], te[PRED_COL]),
756
  "RMSE": rmse(te[TARGET], te[PRED_COL]),
757
- "MAE": mean_absolute_error(te[TARGET], te[PRED_COL])
758
  }
759
 
760
  tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
@@ -794,9 +787,8 @@ if st.session_state.app_step == "dev":
794
  with tab2:
795
  _dev_block(st.session_state.results["Test"], st.session_state.results["m_test"])
796
 
797
- # Export UI for this phase
798
- default_sections = _available_sections()
799
- render_export_button(phase_key="dev", default_sections=default_sections)
800
 
801
  # =========================
802
  # VALIDATION (with actual Ym)
@@ -872,9 +864,8 @@ if st.session_state.app_step == "validate":
872
  use_container_width=False
873
  )
874
 
875
- # Export UI for this phase
876
- default_sections = _available_sections()
877
- render_export_button(phase_key="validate", default_sections=default_sections)
878
 
879
  sv = st.session_state.results["sv_val"]
880
  if sv["oor"] > 0: st.markdown('<div class="st-message-box st-warning">Some inputs fall outside **training min–max** ranges.</div>', unsafe_allow_html=True)
@@ -945,9 +936,8 @@ if st.session_state.app_step == "predict":
945
  config={"displayModeBar": False, "scrollZoom": True}
946
  )
947
 
948
- # Export UI for this phase
949
- default_sections = _available_sections()
950
- render_export_button(phase_key="predict", default_sections=default_sections)
951
 
952
  # =========================
953
  # Run preview modal after all other elements
 
264
  order.append(name)
265
 
266
  def _available_sections() -> list[str]:
267
+ """Compute which sections exist (offered in the export dropdown)."""
268
  res = st.session_state.get("results", {})
269
  sections = []
270
  if "Train" in res: sections += ["Training","Training_Metrics","Training_Summary"]
 
352
  fname = f"YM_Export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
353
  return bio.getvalue(), fname, order
354
 
355
+ # --------- SIMPLE export UI (dropdown checklist, starts empty) ----------
356
+ def render_export_button(phase_key: str) -> None:
357
+ """
358
+ Export UI — one multiselect dropdown that starts EMPTY.
359
+ The download button is disabled until at least one sheet is selected.
360
+ """
361
+ res = st.session_state.get("results", {})
362
+ if not res:
363
+ return
364
+
365
  st.divider()
366
  st.markdown("### Export to Excel")
367
 
368
+ options = _available_sections() # only what exists right now
 
 
 
 
 
 
 
369
  selected_sheets = st.multiselect(
370
  "Sheets to include",
371
+ options=options,
372
+ default=[],
373
+ placeholder="Choose an option",
374
+ help="Pick the sheets you want to include in the Excel export.",
375
  key=f"sheets_{phase_key}",
376
  )
377
 
378
+ if not selected_sheets:
379
+ st.caption("Select one or more sheets above to enable the export.")
380
+ st.download_button(
381
+ label="⬇️ Export Excel",
382
+ data=b"",
383
+ file_name="YM_Export.xlsx",
384
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
385
+ disabled=True,
386
+ key=f"download_{phase_key}",
387
+ )
388
+ return
 
 
 
 
 
 
 
389
 
390
+ data, fname, names = build_export_workbook(selected=selected_sheets, ndigits=3, do_autofit=True)
391
  if names:
392
  st.caption("Will include: " + ", ".join(names))
393
 
 
 
 
394
  st.download_button(
395
  "⬇️ Export Excel",
396
  data=(data or b""),
397
+ file_name=(fname or "YM_Export.xlsx"),
398
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
399
  disabled=(data is None),
400
  key=f"download_{phase_key}",
 
740
 
741
  st.session_state.results["Train"]=tr; st.session_state.results["Test"]=te
742
  st.session_state.results["m_train"]={
743
+ "R": pearson_r(tr[TARGET], tr[PRED_COL]),
744
  "RMSE": rmse(tr[TARGET], tr[PRED_COL]),
745
+ "MAE": mean_absolute_error(tr[TARGET], tr[PRED_COL])
746
  }
747
  st.session_state.results["m_test"]={
748
+ "R": pearson_r(te[TARGET], te[PRED_COL]),
749
  "RMSE": rmse(te[TARGET], te[PRED_COL]),
750
+ "MAE": mean_absolute_error(te[TARGET], te[PRED_COL])
751
  }
752
 
753
  tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
 
787
  with tab2:
788
  _dev_block(st.session_state.results["Test"], st.session_state.results["m_test"])
789
 
790
+ # Export UI for this phase (dropdown checklist starts empty)
791
+ render_export_button(phase_key="dev")
 
792
 
793
  # =========================
794
  # VALIDATION (with actual Ym)
 
864
  use_container_width=False
865
  )
866
 
867
+ # Export UI for this phase (dropdown checklist starts empty)
868
+ render_export_button(phase_key="validate")
 
869
 
870
  sv = st.session_state.results["sv_val"]
871
  if sv["oor"] > 0: st.markdown('<div class="st-message-box st-warning">Some inputs fall outside **training min–max** ranges.</div>', unsafe_allow_html=True)
 
936
  config={"displayModeBar": False, "scrollZoom": True}
937
  )
938
 
939
+ # Export UI for this phase (dropdown checklist starts empty)
940
+ render_export_button(phase_key="predict")
 
941
 
942
  # =========================
943
  # Run preview modal after all other elements