CarlyneB commited on
Commit
01c98b2
·
1 Parent(s): a48ebd8

[FIX] Force module reload and cache clear after CSV auto-update to avoid double restart

Browse files
Files changed (1) hide show
  1. app.py +11 -1
app.py CHANGED
@@ -39,6 +39,14 @@ if os.path.exists(csv):
39
  with open(hash_file, "w") as file:
40
  file.write(current_hash)
41
  print("Data files updated successfully.")
 
 
 
 
 
 
 
 
42
 
43
  st.set_page_config("Carbon Portfolio Optimizer", layout="wide")
44
 
@@ -101,7 +109,9 @@ for m in method_order:
101
  st.session_state.constraints[m] = {"active": True, "cap_type": "percent", "cap_value": 100}
102
 
103
  cost_sliders = load_slider_data()
104
- if "costs" not in st.session_state or st.session_state.costs == {}:
 
 
105
  st.session_state.costs = {
106
  m: {
107
  strip_unit_suffix(r): (0.0 if (m, strip_unit_suffix(r)) in secondary_resources else cost_sliders[m][r]["median"])
 
39
  with open(hash_file, "w") as file:
40
  file.write(current_hash)
41
  print("Data files updated successfully.")
42
+ # Force all app modules to reload on next rerun so they read fresh JSONs.
43
+ # config.config is imported at module level and won't re-execute unless we
44
+ # evict it from sys.modules before st.rerun() re-runs the script.
45
+ for _key in list(sys.modules.keys()):
46
+ if any(_key.startswith(p) for p in ("config", "tabs", "utils")):
47
+ del sys.modules[_key]
48
+ st.cache_data.clear()
49
+ st.rerun()
50
 
51
  st.set_page_config("Carbon Portfolio Optimizer", layout="wide")
52
 
 
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:
115
  st.session_state.costs = {
116
  m: {
117
  strip_unit_suffix(r): (0.0 if (m, strip_unit_suffix(r)) in secondary_resources else cost_sliders[m][r]["median"])