Spaces:
Runtime error
Runtime error
Yacine Jernite
commited on
Commit
·
a735144
1
Parent(s):
ead9ac7
add save/load button
Browse files
app.py
CHANGED
@@ -75,6 +75,36 @@ def main():
|
|
75 |
options=list(pages.keys()),
|
76 |
index=0,
|
77 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
st.markdown("#### GEM Data Card Input Form")
|
79 |
pages[app_mode]()
|
80 |
|
|
|
75 |
options=list(pages.keys()),
|
76 |
index=0,
|
77 |
)
|
78 |
+
with st.sidebar.expander("Save or Load your work"):
|
79 |
+
# Save intermediary state
|
80 |
+
current_date = datetime.now().strftime(
|
81 |
+
"%m/%d/%Y, %H:%M:%S"
|
82 |
+
)
|
83 |
+
friendly_date = re.sub(
|
84 |
+
r"[^\w\s]", "_", current_date
|
85 |
+
).replace(" ", "_").replace("__", "_").replace("-", "")
|
86 |
+
st.download_button(
|
87 |
+
label="Save Intermediary Card State",
|
88 |
+
data=json.dumps(st.session_state.get("card_dict", {}), indent=2),
|
89 |
+
file_name=f"data_card_{friendly_date}.json",
|
90 |
+
)
|
91 |
+
# load from file
|
92 |
+
uploaded_file = st.file_uploader(
|
93 |
+
label=f"Load Intermediary Card State",
|
94 |
+
)
|
95 |
+
if uploaded_file is not None:
|
96 |
+
pre_card_dict = json.load(uploaded_file, encoding="utf-8")
|
97 |
+
def save_dict_to_save_state(saved, prefix=None):
|
98 |
+
prefix = [] if prefix is None else prefix
|
99 |
+
if isinstance(saved, dict):
|
100 |
+
for k, v in saved.items():
|
101 |
+
save_dict_to_save_state(v, prefix + [k])
|
102 |
+
else:
|
103 |
+
st.session_state.save_state["_".join(prefix)] = saved
|
104 |
+
# TODO: don't overwrite by default
|
105 |
+
st.session_state.card_dict = pre_card_dict
|
106 |
+
save_dict_to_save_state(st.session_state.card_dict)
|
107 |
+
# TODO: delete file after use
|
108 |
st.markdown("#### GEM Data Card Input Form")
|
109 |
pages[app_mode]()
|
110 |
|