garvitsachdeva commited on
Commit
a824fc0
·
1 Parent(s): 61e8a52

fix: set_page_config before heavy import; cache_resource spinner; no-op patch

Browse files
Files changed (1) hide show
  1. streamlit_app.py +30 -11
streamlit_app.py CHANGED
@@ -1,22 +1,41 @@
1
  import streamlit as st
2
  import sys, os, traceback
3
  from pathlib import Path
4
- import importlib.util
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  root = Path(__file__).resolve().parent
7
- sys.path.insert(0, str(root))
 
 
 
8
  os.chdir(str(root))
9
 
 
 
 
 
 
 
 
 
10
  try:
11
- demo_file = root / "demo" / "streamlit_app.py"
12
- spec = importlib.util.spec_from_file_location("spindleflow_demo", str(demo_file))
13
- mod = importlib.util.module_from_spec(spec)
14
- mod.__file__ = str(demo_file) # demo's own sys.path logic resolves correctly
15
- sys.modules["spindleflow_demo"] = mod
16
- spec.loader.exec_module(mod) # runs demo/streamlit_app.py in its own context
17
- mod.main()
18
  except SystemExit:
19
  pass
20
- except BaseException as e:
21
- st.error(f"SpindleFlow failed to load: {e}")
22
  st.code(traceback.format_exc())
 
1
  import streamlit as st
2
  import sys, os, traceback
3
  from pathlib import Path
4
+
5
+ # Set page config immediately so the browser gets a response before the
6
+ # heavy ML imports below. demo/streamlit_app.py also calls set_page_config
7
+ # at module level — we monkey-patch it to a no-op so the duplicate call
8
+ # (on first import) doesn't raise a StreamlitAPIException.
9
+ st.set_page_config(
10
+ page_title="SpindleFlow RL",
11
+ page_icon="⚡",
12
+ layout="wide",
13
+ initial_sidebar_state="collapsed",
14
+ )
15
+ _orig_spc = st.set_page_config
16
+ st.set_page_config = lambda *a, **kw: None # silence double-call from demo
17
 
18
  root = Path(__file__).resolve().parent
19
+ demo_dir = root / "demo"
20
+ for p in (str(root), str(demo_dir)):
21
+ if p not in sys.path:
22
+ sys.path.insert(0, p)
23
  os.chdir(str(root))
24
 
25
+
26
+ @st.cache_resource(show_spinner="Loading SpindleFlow RL…")
27
+ def _load_app():
28
+ """Cache the demo module so the 10–20 s ML import only happens once."""
29
+ import demo.streamlit_app as _app # noqa: PLC0415
30
+ return _app
31
+
32
+
33
  try:
34
+ st.set_page_config = _orig_spc # restore before main() uses st.*
35
+ _app = _load_app()
36
+ _app.main()
 
 
 
 
37
  except SystemExit:
38
  pass
39
+ except Exception as exc:
40
+ st.error(f"SpindleFlow failed to load: {exc}")
41
  st.code(traceback.format_exc())