Update app.py
Browse files
app.py
CHANGED
|
@@ -69,18 +69,39 @@ def persist_sites():
|
|
| 69 |
st.error(f"⚠️ Error saving sites: {e}")
|
| 70 |
|
| 71 |
def get_active_site():
|
| 72 |
-
|
| 73 |
-
idx = st.session_state.get("active_site_idx")
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
def save_active_site(site: dict):
|
| 79 |
-
"""Save updates to current site and persist"""
|
| 80 |
-
idx = st.session_state.get("active_site_idx")
|
| 81 |
-
if idx is not None and idx < len(st.session_state["sites"]):
|
| 82 |
-
st.session_state["sites"][idx] = site
|
| 83 |
-
persist_sites()
|
| 84 |
|
| 85 |
def create_new_site(name: str):
|
| 86 |
"""Create new site with maximum soil details and set active"""
|
|
|
|
| 69 |
st.error(f"⚠️ Error saving sites: {e}")
|
| 70 |
|
| 71 |
def get_active_site():
|
| 72 |
+
# Fetch current index safely
|
| 73 |
+
idx = st.session_state.get("active_site_idx", 0)
|
| 74 |
+
|
| 75 |
+
# Ensure sites list exists
|
| 76 |
+
sites = st.session_state.get("sites", [])
|
| 77 |
+
|
| 78 |
+
# If no sites exist, create a default site
|
| 79 |
+
if not sites:
|
| 80 |
+
st.session_state["sites"] = [{"Site Name": "Site 1"}]
|
| 81 |
+
st.session_state["active_site_idx"] = 0
|
| 82 |
+
return st.session_state["sites"][0]
|
| 83 |
+
|
| 84 |
+
# Ensure idx is within bounds
|
| 85 |
+
if idx < 0 or idx >= len(sites):
|
| 86 |
+
st.session_state["active_site_idx"] = 0
|
| 87 |
+
idx = 0
|
| 88 |
+
|
| 89 |
+
return sites[idx]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def save_active_site(site_data):
|
| 93 |
+
sites = st.session_state.get("sites", [])
|
| 94 |
+
|
| 95 |
+
if not sites:
|
| 96 |
+
st.session_state["sites"] = [site_data]
|
| 97 |
+
st.session_state["active_site_idx"] = 0
|
| 98 |
+
else:
|
| 99 |
+
idx = st.session_state.get("active_site_idx", 0)
|
| 100 |
+
if idx < 0 or idx >= len(sites):
|
| 101 |
+
idx = 0
|
| 102 |
+
st.session_state["active_site_idx"] = 0
|
| 103 |
+
st.session_state["sites"][idx] = site_data
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def create_new_site(name: str):
|
| 107 |
"""Create new site with maximum soil details and set active"""
|