Spaces:
Runtime error
Runtime error
Benjamin Bossan
commited on
Commit
·
f834b58
1
Parent(s):
b052ddd
Solve custom section issue with sessions
Browse filesNow correctly uses st.session_state instead of a local file.
app.py
CHANGED
|
@@ -27,7 +27,11 @@ st.title("Skops space creator for sklearn")
|
|
| 27 |
|
| 28 |
PLACEHOLDER = "[More Information Needed]"
|
| 29 |
PLOT_PREFIX = "__plot__:"
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
tmp_repo = Path(mkdtemp(prefix="skops-"))
|
| 32 |
left_col, right_col = st.columns([1, 2])
|
| 33 |
|
|
@@ -36,32 +40,11 @@ CUSTOM_SECTIONS_CACHE_FILE = ".custom-sections.json"
|
|
| 36 |
|
| 37 |
|
| 38 |
def _clear_custom_section_cache():
|
| 39 |
-
|
| 40 |
-
f.write("")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def _load_custom_section_cache():
|
| 44 |
-
global custom_sections
|
| 45 |
-
|
| 46 |
-
# in case file doesn't exist yet, create it
|
| 47 |
-
if not os.path.exists(CUSTOM_SECTIONS_CACHE_FILE):
|
| 48 |
-
Path(CUSTOM_SECTIONS_CACHE_FILE).touch()
|
| 49 |
-
|
| 50 |
-
with open(CUSTOM_SECTIONS_CACHE_FILE, "r") as f:
|
| 51 |
-
try:
|
| 52 |
-
custom_sections = json.load(f)
|
| 53 |
-
except ValueError:
|
| 54 |
-
pass
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def _write_custom_section_cache():
|
| 58 |
-
with open(CUSTOM_SECTIONS_CACHE_FILE, "w") as f:
|
| 59 |
-
json.dump(custom_sections, f)
|
| 60 |
|
| 61 |
|
| 62 |
def _remove_custom_section(key):
|
| 63 |
-
del custom_sections[key]
|
| 64 |
-
_write_custom_section_cache()
|
| 65 |
|
| 66 |
|
| 67 |
def _clear_repo(path):
|
|
@@ -157,8 +140,8 @@ def _create_model_card():
|
|
| 157 |
if citation:
|
| 158 |
model_card.add(**{"Citation": citation})
|
| 159 |
|
| 160 |
-
if custom_sections:
|
| 161 |
-
for key, val in custom_sections.items():
|
| 162 |
if not key:
|
| 163 |
continue
|
| 164 |
|
|
@@ -256,8 +239,7 @@ def add_custom_section():
|
|
| 256 |
if not section_name or not section_content:
|
| 257 |
return
|
| 258 |
|
| 259 |
-
custom_sections[section_name] = section_content
|
| 260 |
-
_write_custom_section_cache()
|
| 261 |
|
| 262 |
|
| 263 |
def add_custom_plot():
|
|
@@ -275,8 +257,7 @@ def add_custom_plot():
|
|
| 275 |
with open(file_path, "wb") as f:
|
| 276 |
f.write(plot_file.getvalue())
|
| 277 |
|
| 278 |
-
custom_sections[str(PLOT_PREFIX + plot_name)] = file_path
|
| 279 |
-
_write_custom_section_cache()
|
| 280 |
|
| 281 |
|
| 282 |
with left_col:
|
|
@@ -339,6 +320,7 @@ with left_col:
|
|
| 339 |
height=5,
|
| 340 |
)
|
| 341 |
|
|
|
|
| 342 |
with st.form("custom-section", clear_on_submit=True):
|
| 343 |
section_name = st.text_input(
|
| 344 |
"Section name (use '/' for subsections, e.g. 'Model description/My new"
|
|
@@ -352,6 +334,7 @@ with left_col:
|
|
| 352 |
"Create new section", on_click=add_custom_section
|
| 353 |
)
|
| 354 |
|
|
|
|
| 355 |
with st.form("custom-plots", clear_on_submit=True):
|
| 356 |
plot_name = st.text_input(
|
| 357 |
"Section name (use '/' for subsections, e.g. 'Model description/My new"
|
|
@@ -362,8 +345,7 @@ with left_col:
|
|
| 362 |
|
| 363 |
submit_new_plot = st.form_submit_button("Add plot", on_click=add_custom_plot)
|
| 364 |
|
| 365 |
-
|
| 366 |
-
for key in custom_sections:
|
| 367 |
if not key:
|
| 368 |
continue
|
| 369 |
|
|
@@ -378,9 +360,9 @@ with left_col:
|
|
| 378 |
f"Remove section '{key}'", on_click=_remove_custom_section, args=(key,)
|
| 379 |
)
|
| 380 |
|
| 381 |
-
if custom_sections:
|
| 382 |
st.button(
|
| 383 |
-
f"Remove all ({len(custom_sections)}) custom elements",
|
| 384 |
on_click=_clear_custom_section_cache,
|
| 385 |
)
|
| 386 |
|
|
|
|
| 27 |
|
| 28 |
PLACEHOLDER = "[More Information Needed]"
|
| 29 |
PLOT_PREFIX = "__plot__:"
|
| 30 |
+
|
| 31 |
+
# store session state
|
| 32 |
+
if "custom_sections" not in st.session_state:
|
| 33 |
+
st.session_state.custom_sections = {}
|
| 34 |
+
|
| 35 |
tmp_repo = Path(mkdtemp(prefix="skops-"))
|
| 36 |
left_col, right_col = st.columns([1, 2])
|
| 37 |
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def _clear_custom_section_cache():
|
| 43 |
+
st.session_state.custom_sections.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
def _remove_custom_section(key):
|
| 47 |
+
del st.session_state.custom_sections[key]
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def _clear_repo(path):
|
|
|
|
| 140 |
if citation:
|
| 141 |
model_card.add(**{"Citation": citation})
|
| 142 |
|
| 143 |
+
if st.session_state.custom_sections:
|
| 144 |
+
for key, val in st.session_state.custom_sections.items():
|
| 145 |
if not key:
|
| 146 |
continue
|
| 147 |
|
|
|
|
| 239 |
if not section_name or not section_content:
|
| 240 |
return
|
| 241 |
|
| 242 |
+
st.session_state.custom_sections[section_name] = section_content
|
|
|
|
| 243 |
|
| 244 |
|
| 245 |
def add_custom_plot():
|
|
|
|
| 257 |
with open(file_path, "wb") as f:
|
| 258 |
f.write(plot_file.getvalue())
|
| 259 |
|
| 260 |
+
st.session_state.custom_sections[str(PLOT_PREFIX + plot_name)] = file_path
|
|
|
|
| 261 |
|
| 262 |
|
| 263 |
with left_col:
|
|
|
|
| 320 |
height=5,
|
| 321 |
)
|
| 322 |
|
| 323 |
+
# ADD A CUSTOM SECTIONS
|
| 324 |
with st.form("custom-section", clear_on_submit=True):
|
| 325 |
section_name = st.text_input(
|
| 326 |
"Section name (use '/' for subsections, e.g. 'Model description/My new"
|
|
|
|
| 334 |
"Create new section", on_click=add_custom_section
|
| 335 |
)
|
| 336 |
|
| 337 |
+
# ADD A PLOT
|
| 338 |
with st.form("custom-plots", clear_on_submit=True):
|
| 339 |
plot_name = st.text_input(
|
| 340 |
"Section name (use '/' for subsections, e.g. 'Model description/My new"
|
|
|
|
| 345 |
|
| 346 |
submit_new_plot = st.form_submit_button("Add plot", on_click=add_custom_plot)
|
| 347 |
|
| 348 |
+
for key in st.session_state.custom_sections:
|
|
|
|
| 349 |
if not key:
|
| 350 |
continue
|
| 351 |
|
|
|
|
| 360 |
f"Remove section '{key}'", on_click=_remove_custom_section, args=(key,)
|
| 361 |
)
|
| 362 |
|
| 363 |
+
if st.session_state.custom_sections:
|
| 364 |
st.button(
|
| 365 |
+
f"Remove all ({len(st.session_state.custom_sections)}) custom elements",
|
| 366 |
on_click=_clear_custom_section_cache,
|
| 367 |
)
|
| 368 |
|