garvitsachdeva commited on
Commit
96cef35
·
1 Parent(s): a824fc0

fix: use exec() wrapper — eliminates double set_page_config crash

Browse files
Files changed (1) hide show
  1. streamlit_app.py +16 -30
streamlit_app.py CHANGED
@@ -1,39 +1,25 @@
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:
 
 
1
  import sys, os, traceback
2
  from pathlib import Path
3
 
4
+ # Make env.*, reward.*, agents.* (from repo root) and orchestrator_widget
5
+ # (from demo/) importable before demo/streamlit_app.py starts.
6
+ _root = Path(__file__).resolve().parent
7
+ for _p in (str(_root), str(_root / "demo")):
8
+ if _p not in sys.path:
9
+ sys.path.insert(0, _p)
10
+ os.chdir(str(_root))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ import streamlit as st # needed only for the error fallback below
13
 
14
+ _demo = _root / "demo" / "streamlit_app.py"
15
  try:
16
+ # Run demo/streamlit_app.py exactly as if it were the entry-point script.
17
+ # __file__ must point to the demo file so its own Path(__file__) logic
18
+ # resolves paths correctly (e.g. demo/assets, orchestrator_widget).
19
+ exec(
20
+ compile(_demo.read_text(encoding="utf-8"), str(_demo), "exec"),
21
+ {"__file__": str(_demo), "__name__": "__main__"},
22
+ )
23
  except SystemExit:
24
  pass
25
  except Exception as exc: