Spaces:
Sleeping
Sleeping
[FIX] Hide Other resources in tab1, fix unit label (yr instead of year)
Browse files- tabs/tab1_inputs.py +23 -19
tabs/tab1_inputs.py
CHANGED
|
@@ -2,7 +2,8 @@ import pandas as pd
|
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
from config.config import (resource_groups, resource_order, resource_tooltips,
|
| 5 |
-
resource_unit_map, method_groups, CO2_UNIT, secondary_resources
|
|
|
|
| 6 |
from utils.data_utils import render_resource_input, strip_unit_suffix, get_resource_to_group, get_resource_to_unit, get_method_group
|
| 7 |
from utils.ui_helpers import show_bar
|
| 8 |
from io import BytesIO
|
|
@@ -114,12 +115,12 @@ def dialog_add_custom_resource(cost_sliders, edit_index: int | None = None, pres
|
|
| 114 |
group = resource_to_group.get(batch_group)
|
| 115 |
unit = resource_unit_map.get(group, "?")
|
| 116 |
|
| 117 |
-
st.markdown(f'<p class="caption">Unit for this group: <strong>{unit}/
|
| 118 |
|
| 119 |
st.markdown('<p class="dialog-title">2 — Available quantity</p>', unsafe_allow_html=True)
|
| 120 |
|
| 121 |
batch_amount = st.number_input(
|
| 122 |
-
label=f"Available amount ({unit}/
|
| 123 |
min_value=0.0,
|
| 124 |
format="%.2f",
|
| 125 |
value=float(existing["amount"]) if existing.get("amount") and existing.get("amount") != 0.0 else None,
|
|
@@ -283,7 +284,7 @@ def dialog_add_custom_resource(cost_sliders, edit_index: int | None = None, pres
|
|
| 283 |
errors = []
|
| 284 |
if not batch_name.strip():
|
| 285 |
errors.append("Resource name is required.")
|
| 286 |
-
elif batch_name.strip() in standard_names:
|
| 287 |
errors.append(f"**{batch_name}** matches a standard resource name. Choose a different name.")
|
| 288 |
elif batch_name.strip() in existing_names:
|
| 289 |
errors.append(f"**{batch_name}** already exists. Choose a different name.")
|
|
@@ -682,11 +683,19 @@ def render_tab(cost_sliders):
|
|
| 682 |
|
| 683 |
for group, rlist in resource_groups.items():
|
| 684 |
unit = resource_unit_map.get(group, "")
|
| 685 |
-
with st.expander(f"**{group}** ({unit}/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
cols = st.columns(4)
|
| 688 |
|
| 689 |
-
for i, r in enumerate(
|
| 690 |
|
| 691 |
tooltip = resource_tooltips.get(r, None)
|
| 692 |
name_clean = r.split(" (")[0]
|
|
@@ -804,34 +813,29 @@ def render_tab(cost_sliders):
|
|
| 804 |
if method_active_resource_count.get(m, 0) > 0:
|
| 805 |
if m in st.session_state.constraints:
|
| 806 |
st.session_state.constraints[m]["active"] = True
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
|
|
|
| 810 |
st.session_state.auto_disabled_methods.discard(m)
|
|
|
|
| 811 |
|
| 812 |
# Step 3 – disable methods that now have zero active primary resources.
|
| 813 |
# We record which methods the tool disabled (auto_disabled_methods) so that
|
| 814 |
# step 2 can safely re-enable them later without touching user-chosen settings.
|
| 815 |
newly_disabled = []
|
| 816 |
for m in methods_with_no_active_resource:
|
|
|
|
|
|
|
|
|
|
| 817 |
if st.session_state.constraints.get(m, {}).get("active", True):
|
| 818 |
st.session_state.constraints.setdefault(m, {"active": True, "cap_type": "percent", "cap_value": 100})
|
| 819 |
st.session_state.constraints[m]["active"] = False
|
| 820 |
-
st.session_state.auto_disabled_methods.add(m)
|
| 821 |
newly_disabled.append(m)
|
| 822 |
toggle_key = f"toggle_{m}"
|
| 823 |
if toggle_key in st.session_state:
|
| 824 |
st.session_state[toggle_key] = False
|
| 825 |
|
| 826 |
-
if newly_disabled:
|
| 827 |
-
st.info(
|
| 828 |
-
"The following methods have no active resource left and were "
|
| 829 |
-
"automatically deactivated (see Method constraints tab): "
|
| 830 |
-
+ ", ".join(newly_disabled),
|
| 831 |
-
icon=":material/info:"
|
| 832 |
-
)
|
| 833 |
-
|
| 834 |
-
|
| 835 |
panel_custom_resources_controls(cost_sliders)
|
| 836 |
|
| 837 |
st.markdown('<p class="crra-sub-heading">Summary of available resources</p>', unsafe_allow_html=True)
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
from config.config import (resource_groups, resource_order, resource_tooltips,
|
| 5 |
+
resource_unit_map, method_groups, CO2_UNIT, secondary_resources,
|
| 6 |
+
hidden_resource_names)
|
| 7 |
from utils.data_utils import render_resource_input, strip_unit_suffix, get_resource_to_group, get_resource_to_unit, get_method_group
|
| 8 |
from utils.ui_helpers import show_bar
|
| 9 |
from io import BytesIO
|
|
|
|
| 115 |
group = resource_to_group.get(batch_group)
|
| 116 |
unit = resource_unit_map.get(group, "?")
|
| 117 |
|
| 118 |
+
st.markdown(f'<p class="caption">Unit for this group: <strong>{unit}/yr</strong></p>', unsafe_allow_html=True)
|
| 119 |
|
| 120 |
st.markdown('<p class="dialog-title">2 — Available quantity</p>', unsafe_allow_html=True)
|
| 121 |
|
| 122 |
batch_amount = st.number_input(
|
| 123 |
+
label=f"Available amount ({unit}/yr)",
|
| 124 |
min_value=0.0,
|
| 125 |
format="%.2f",
|
| 126 |
value=float(existing["amount"]) if existing.get("amount") and existing.get("amount") != 0.0 else None,
|
|
|
|
| 284 |
errors = []
|
| 285 |
if not batch_name.strip():
|
| 286 |
errors.append("Resource name is required.")
|
| 287 |
+
elif batch_name.strip().lower() in {n.lower() for n in standard_names}:
|
| 288 |
errors.append(f"**{batch_name}** matches a standard resource name. Choose a different name.")
|
| 289 |
elif batch_name.strip() in existing_names:
|
| 290 |
errors.append(f"**{batch_name}** already exists. Choose a different name.")
|
|
|
|
| 683 |
|
| 684 |
for group, rlist in resource_groups.items():
|
| 685 |
unit = resource_unit_map.get(group, "")
|
| 686 |
+
with st.expander(f"**{group}** ({unit}/yr)", expanded=True):
|
| 687 |
+
|
| 688 |
+
display_rlist = [r for r in rlist if r not in hidden_resource_names]
|
| 689 |
+
|
| 690 |
+
if not display_rlist:
|
| 691 |
+
st.caption(
|
| 692 |
+
"No standard resource to display for this group. "
|
| 693 |
+
"Add a specific resource below to include it in the optimization."
|
| 694 |
+
)
|
| 695 |
|
| 696 |
cols = st.columns(4)
|
| 697 |
|
| 698 |
+
for i, r in enumerate(display_rlist):
|
| 699 |
|
| 700 |
tooltip = resource_tooltips.get(r, None)
|
| 701 |
name_clean = r.split(" (")[0]
|
|
|
|
| 813 |
if method_active_resource_count.get(m, 0) > 0:
|
| 814 |
if m in st.session_state.constraints:
|
| 815 |
st.session_state.constraints[m]["active"] = True
|
| 816 |
+
st.session_state.constraints[m]["cap_type"] = "percent"
|
| 817 |
+
st.session_state.constraints[m]["cap_value"] = 100
|
| 818 |
+
st.session_state[f"toggle_{m}"] = True
|
| 819 |
+
st.session_state[f"slider_{m}"] = 100
|
| 820 |
st.session_state.auto_disabled_methods.discard(m)
|
| 821 |
+
st.session_state.setdefault("just_activated_methods", set()).add(m)
|
| 822 |
|
| 823 |
# Step 3 – disable methods that now have zero active primary resources.
|
| 824 |
# We record which methods the tool disabled (auto_disabled_methods) so that
|
| 825 |
# step 2 can safely re-enable them later without touching user-chosen settings.
|
| 826 |
newly_disabled = []
|
| 827 |
for m in methods_with_no_active_resource:
|
| 828 |
+
# Always track in auto_disabled_methods so Step 2 can re-enable later,
|
| 829 |
+
# even if the method was already inactive (e.g. initialised as False in app.py).
|
| 830 |
+
st.session_state.auto_disabled_methods.add(m)
|
| 831 |
if st.session_state.constraints.get(m, {}).get("active", True):
|
| 832 |
st.session_state.constraints.setdefault(m, {"active": True, "cap_type": "percent", "cap_value": 100})
|
| 833 |
st.session_state.constraints[m]["active"] = False
|
|
|
|
| 834 |
newly_disabled.append(m)
|
| 835 |
toggle_key = f"toggle_{m}"
|
| 836 |
if toggle_key in st.session_state:
|
| 837 |
st.session_state[toggle_key] = False
|
| 838 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
panel_custom_resources_controls(cost_sliders)
|
| 840 |
|
| 841 |
st.markdown('<p class="crra-sub-heading">Summary of available resources</p>', unsafe_allow_html=True)
|