File size: 1,524 Bytes
2c0702f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import streamlit as st


def init_custom_resources():
    """Initialise custom resource session state keys and coerce amounts/coefficients to float.

    Ensures custom_resources, enabled_methods, and auto_disabled_methods exist in
    session state, then sanitises every batch so numeric fields are float (not str).
    """
    if "custom_resources" not in st.session_state:
        st.session_state.custom_resources = []
    if "enabled_methods" not in st.session_state:
        st.session_state.enabled_methods = {}
    if "auto_disabled_methods" not in st.session_state:
        st.session_state.auto_disabled_methods = set()
    for batch in st.session_state.custom_resources:
        try:
            batch["amount"] = float(batch["amount"])
        except (ValueError, TypeError):
            batch["amount"] = 0.0
        for m, coefs in batch.get("methods", {}).items():
            for key in ("min", "median", "max"):
                try:
                    coefs[key] = float(coefs[key])
                except (ValueError, TypeError):
                    coefs[key] = 0.0


def clear_widget_keys():
    """Delete all Streamlit widget state keys for sliders, manual inputs, and toggles.

    Called after loading a new file or scenario so widgets re-initialise from
    fresh session-state values instead of showing stale cached values.
    """
    prefixes = ("toggle_", "cap_type_", "slider_", "manual_")
    for k in [k for k in st.session_state if k.startswith(prefixes)]:
        del st.session_state[k]