Spaces:
Sleeping
Sleeping
Create utils/session.py
Browse files- utils/session.py +29 -5
utils/session.py
CHANGED
|
@@ -1,7 +1,31 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return st.session_state.get("canvas_data", {})
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
STATE_FILE = "data/state.json"
|
|
|
|
| 5 |
|
| 6 |
+
def load_state():
|
| 7 |
+
"""Load the app state from the JSON file."""
|
| 8 |
+
if os.path.exists(STATE_FILE):
|
| 9 |
+
try:
|
| 10 |
+
with open(STATE_FILE, "r", encoding="utf-8") as f:
|
| 11 |
+
return json.load(f)
|
| 12 |
+
except json.JSONDecodeError:
|
| 13 |
+
return {}
|
| 14 |
+
return {}
|
| 15 |
+
|
| 16 |
+
def save_state(state: dict):
|
| 17 |
+
"""Save the app state to the JSON file."""
|
| 18 |
+
os.makedirs(os.path.dirname(STATE_FILE), exist_ok=True)
|
| 19 |
+
with open(STATE_FILE, "w", encoding="utf-8") as f:
|
| 20 |
+
json.dump(state, f, indent=4)
|
| 21 |
+
|
| 22 |
+
def update_state(key, value):
|
| 23 |
+
"""Update a specific key in the app state."""
|
| 24 |
+
state = load_state()
|
| 25 |
+
state[key] = value
|
| 26 |
+
save_state(state)
|
| 27 |
+
|
| 28 |
+
def get_state_value(key, default=None):
|
| 29 |
+
"""Get a specific key from the app state, or return default."""
|
| 30 |
+
state = load_state()
|
| 31 |
+
return state.get(key, default)
|