Spaces:
Sleeping
Sleeping
[REFACTOR] tab1: replace apply_auto_disable_logic with refresh_active_methods, remove redundant resource_caps init
Browse files- tabs/tab1_inputs.py +13 -20
tabs/tab1_inputs.py
CHANGED
|
@@ -7,8 +7,9 @@ from config.config import (resource_groups, resource_order, resource_tooltips,
|
|
| 7 |
from utils.data_utils import (strip_unit_suffix, get_resource_to_group, get_resource_to_unit,
|
| 8 |
get_method_group, standard_resource_names, to_excel_bytes, parse_custom_excel,
|
| 9 |
resource_caps_to_excel_bytes)
|
| 10 |
-
from utils.state_utils import
|
| 11 |
from utils.ui_helpers import show_bar, render_resource_input
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
@st.dialog("Add a new resource", width="large")
|
|
@@ -739,11 +740,8 @@ def render_tab(cost_sliders):
|
|
| 739 |
Returns:
|
| 740 |
None
|
| 741 |
"""
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
for r in resource_order:
|
| 745 |
-
if r not in st.session_state.resource_caps:
|
| 746 |
-
st.session_state.resource_caps[r] = 0.0
|
| 747 |
init_custom_resources()
|
| 748 |
|
| 749 |
st.markdown('<h1 class="crra-heading">Resource inputs</h1>', unsafe_allow_html=True)
|
|
@@ -804,19 +802,6 @@ def render_tab(cost_sliders):
|
|
| 804 |
# Standard resource inputs
|
| 805 |
st.markdown('<p class="crra-sub-heading">Set available resources for one year</p>', unsafe_allow_html=True)
|
| 806 |
|
| 807 |
-
# Build a lookup: for each CDR method, which resources are "primary" (mandatory)?
|
| 808 |
-
# Secondary resources are optional add-ons — a method can still run without them.
|
| 809 |
-
# A method can only run in the optimisation if at least one primary resource is available.
|
| 810 |
-
method_to_primary_resources = {}
|
| 811 |
-
for cdr_method, resource_coeffs in cost_sliders.items():
|
| 812 |
-
for name in resource_coeffs:
|
| 813 |
-
r_clean = strip_unit_suffix(name)
|
| 814 |
-
if (cdr_method, r_clean) not in secondary_resources:
|
| 815 |
-
method_to_primary_resources.setdefault(cdr_method, []).append(r_clean)
|
| 816 |
-
|
| 817 |
-
for r in method_to_primary_resources:
|
| 818 |
-
pass # no-op, kept for clarity of intent below
|
| 819 |
-
|
| 820 |
resource_to_group = get_resource_to_group()
|
| 821 |
|
| 822 |
for group, rlist in resource_groups.items():
|
|
@@ -861,7 +846,15 @@ def render_tab(cost_sliders):
|
|
| 861 |
):
|
| 862 |
dialog_add_custom_resource(cost_sliders, preset_resources=rlist)
|
| 863 |
|
| 864 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
# Rendered after all resource inputs above (which write into
|
| 867 |
# resource_caps) so the export always reflects this render's latest
|
|
|
|
| 7 |
from utils.data_utils import (strip_unit_suffix, get_resource_to_group, get_resource_to_unit,
|
| 8 |
get_method_group, standard_resource_names, to_excel_bytes, parse_custom_excel,
|
| 9 |
resource_caps_to_excel_bytes)
|
| 10 |
+
from utils.state_utils import init_custom_resources
|
| 11 |
from utils.ui_helpers import show_bar, render_resource_input
|
| 12 |
+
from tabs.tab2_constraints import refresh_active_methods
|
| 13 |
|
| 14 |
|
| 15 |
@st.dialog("Add a new resource", width="large")
|
|
|
|
| 740 |
Returns:
|
| 741 |
None
|
| 742 |
"""
|
| 743 |
+
# resource_caps is already fully populated (every resource_order key set)
|
| 744 |
+
# by app.py's shared init, which runs before any tab renders.
|
|
|
|
|
|
|
|
|
|
| 745 |
init_custom_resources()
|
| 746 |
|
| 747 |
st.markdown('<h1 class="crra-heading">Resource inputs</h1>', unsafe_allow_html=True)
|
|
|
|
| 802 |
# Standard resource inputs
|
| 803 |
st.markdown('<p class="crra-sub-heading">Set available resources for one year</p>', unsafe_allow_html=True)
|
| 804 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
resource_to_group = get_resource_to_group()
|
| 806 |
|
| 807 |
for group, rlist in resource_groups.items():
|
|
|
|
| 846 |
):
|
| 847 |
dialog_add_custom_resource(cost_sliders, preset_resources=rlist)
|
| 848 |
|
| 849 |
+
# Refresh method activation now that this render's resource inputs and
|
| 850 |
+
# "Related CDR Methods" toggles are final — reuses the same real-quantity
|
| 851 |
+
# check as app.py's shared init (which runs too early to see toggles the
|
| 852 |
+
# user just changed on this exact render) and Tab 2's own pre-render sync.
|
| 853 |
+
cost_sliders_norm = {
|
| 854 |
+
m: {strip_unit_suffix(name): conf for name, conf in resources.items()}
|
| 855 |
+
for m, resources in cost_sliders.items()
|
| 856 |
+
}
|
| 857 |
+
refresh_active_methods(cost_sliders_norm)
|
| 858 |
|
| 859 |
# Rendered after all resource inputs above (which write into
|
| 860 |
# resource_caps) so the export always reflects this render's latest
|