Spaces:
Sleeping
Sleeping
File size: 2,241 Bytes
ca86b1a ba2dcff fbd166f ba2dcff ca86b1a ba2dcff 1c33177 ba2dcff fbd166f 719f294 ba2dcff 719f294 ba2dcff fbd166f ba2dcff fbd166f ba2dcff fbd166f ca86b1a | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import io
import json
import pandas as pd
import streamlit as st
import openpyxl
from openpyxl.styles import PatternFill, Font
from config.config import resource_order, method_order, secondary_resources
@st.cache_data
def load_slider_data():
"""Load slider data from a JSON file and cache it for performance."""
with open("data/cost_sliders.json") as f:
data = json.load(f)
return data
def render_resource_input(name_clean, r, tooltip=None):
if tooltip:
st.markdown(
f"""<span title="{tooltip}" style="font-weight: 500;">{name_clean} <sup style="color:#999;">ℹ️</sup></span>""",
unsafe_allow_html=True
)
else:
st.markdown(f"**{name_clean}**")
widget_key = f"cap_{r}"
if widget_key not in st.session_state:
st.session_state[widget_key] = st.session_state.resource_caps.get(r, 0.0)
cap = st.number_input(
label="label",
label_visibility="collapsed",
min_value=0.0,
format="%.2f",
key=widget_key,
)
st.session_state.resource_caps[r] = cap
def is_valid_scenario(scenario):
return (
isinstance(scenario, dict)
and "allocations" in scenario
and "resource_caps" in scenario
and "method_costs" in scenario
and "method_constraints" in scenario
and "summary" in scenario
)
def load_inputs_from_excel(file):
xl = pd.read_excel(file, sheet_name=None)
resource_caps = {}
if "resource_caps" in xl:
for _, row in xl["resource_caps"].iterrows():
resource_caps[row["Resource"]] = float(row["Available Amount"])
constraints = {}
if "constraints" in xl:
for _, row in xl["constraints"].iterrows():
constraints[row["method"]] = {
"active": bool(row["active"]),
"max_share": float(row["max_share"])
}
costs = {}
if "coefficients" in xl:
for _, row in xl["coefficients"].iterrows():
m, r, v = row["method"], row["resource"], float(row["value"])
costs.setdefault(m, {})[r] = v
return resource_caps, constraints, costs
|