CarlyneB commited on
Commit
be0befa
·
1 Parent(s): 78c94c6

[FIX] Auto-disable methods with only hidden resources and force tab2 widget sync on navigation

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -9,7 +9,7 @@ from tabs import (tab0_welcome, tab1_inputs, tab2_constraints,
9
  tab3_coefficients, tab4_optimization, tab5_sensitivity,
10
  tab6_scenarios)
11
  from utils.data_utils import load_slider_data, strip_unit_suffix
12
- from config.config import method_order, secondary_resources
13
 
14
  # ── Auto-update: regenerate JSON files if inputs_aggregated.csv has changed ───
15
  # Uses a hash file to detect changes — works both locally and on Hugging Face.
@@ -102,13 +102,26 @@ with st.sidebar:
102
  }
103
  )
104
 
 
 
105
  if "constraints" not in st.session_state:
106
  st.session_state.constraints = {}
 
 
107
  for m in method_order:
108
  if m not in st.session_state.constraints:
109
- st.session_state.constraints[m] = {"active": True, "cap_type": "percent", "cap_value": 100}
110
-
111
- cost_sliders = load_slider_data()
 
 
 
 
 
 
 
 
 
112
  _expected_methods = set(cost_sliders.keys())
113
  _current_methods = set(st.session_state.get("costs", {}).keys())
114
  if "costs" not in st.session_state or st.session_state.costs == {} or _expected_methods != _current_methods:
@@ -120,12 +133,23 @@ if "costs" not in st.session_state or st.session_state.costs == {} or _expected_
120
  for m in cost_sliders
121
  }
122
 
 
 
 
 
 
 
 
 
 
 
 
123
  if selected == "Welcome":
124
  tab0_welcome.render_tab()
125
  elif selected == "Resource inputs":
126
  tab1_inputs.render_tab(cost_sliders)
127
  elif selected == "Method constraints":
128
- tab2_constraints.render_tab()
129
  elif selected == "Resource coefficients":
130
  tab3_coefficients.render_tab(cost_sliders)
131
  elif selected == "Portfolio generation":
 
9
  tab3_coefficients, tab4_optimization, tab5_sensitivity,
10
  tab6_scenarios)
11
  from utils.data_utils import load_slider_data, strip_unit_suffix
12
+ from config.config import method_order, secondary_resources, hidden_resource_names
13
 
14
  # ── Auto-update: regenerate JSON files if inputs_aggregated.csv has changed ───
15
  # Uses a hash file to detect changes — works both locally and on Hugging Face.
 
102
  }
103
  )
104
 
105
+ cost_sliders = load_slider_data()
106
+
107
  if "constraints" not in st.session_state:
108
  st.session_state.constraints = {}
109
+ if "auto_disabled_methods" not in st.session_state:
110
+ st.session_state.auto_disabled_methods = set()
111
  for m in method_order:
112
  if m not in st.session_state.constraints:
113
+ primary = [
114
+ strip_unit_suffix(r) for r in cost_sliders.get(m, {})
115
+ if (m, strip_unit_suffix(r)) not in secondary_resources
116
+ ]
117
+ all_hidden = bool(primary) and all(r in hidden_resource_names for r in primary)
118
+ st.session_state.constraints[m] = {
119
+ "active": not all_hidden,
120
+ "cap_type": "percent",
121
+ "cap_value": 100,
122
+ }
123
+ if all_hidden:
124
+ st.session_state.auto_disabled_methods.add(m)
125
  _expected_methods = set(cost_sliders.keys())
126
  _current_methods = set(st.session_state.get("costs", {}).keys())
127
  if "costs" not in st.session_state or st.session_state.costs == {} or _expected_methods != _current_methods:
 
133
  for m in cost_sliders
134
  }
135
 
136
+ # When the user navigates TO "Method constraints" from any other tab, force a
137
+ # full widget re-sync so toggle_M / slider_M keys always reflect the constraint
138
+ # state. Normal-mode setdefault can't recover keys that were externally set to
139
+ # False/0; force mode overwrites them unconditionally from constraints (which
140
+ # remain correct — the summary table at the bottom of tab2 always confirms
141
+ # this). The flag is consumed (popped) by tab2's own pre-render sync.
142
+ _prev_tab = st.session_state.get("_active_tab")
143
+ if selected == "Method constraints" and _prev_tab != "Method constraints":
144
+ st.session_state["constraints_just_loaded"] = True
145
+ st.session_state["_active_tab"] = selected
146
+
147
  if selected == "Welcome":
148
  tab0_welcome.render_tab()
149
  elif selected == "Resource inputs":
150
  tab1_inputs.render_tab(cost_sliders)
151
  elif selected == "Method constraints":
152
+ tab2_constraints.render_tab(cost_sliders)
153
  elif selected == "Resource coefficients":
154
  tab3_coefficients.render_tab(cost_sliders)
155
  elif selected == "Portfolio generation":