Spaces:
Sleeping
Sleeping
dasdebanna
commited on
Commit
·
8b00529
1
Parent(s):
39fe319
Launcher: set XDG_CONFIG_HOME/HOME and create .streamlit/config.toml for Spaces
Browse files- streamlit_app.py +33 -6
streamlit_app.py
CHANGED
|
@@ -1,11 +1,38 @@
|
|
| 1 |
-
# streamlit_app.py
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
from pathlib import Path
|
| 8 |
import runpy
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# streamlit_app.py (place at repo root)
|
| 2 |
"""
|
| 3 |
+
Launcher for Hugging Face Spaces / Streamlit:
|
| 4 |
+
- ensure Streamlit config/metrics are written to a writable location
|
| 5 |
+
- create a minimal .streamlit/config.toml if missing
|
| 6 |
+
- run the actual app (src/app.py) as __main__
|
| 7 |
"""
|
| 8 |
+
import os
|
| 9 |
from pathlib import Path
|
| 10 |
import runpy
|
| 11 |
|
| 12 |
+
ROOT = Path(__file__).parent.resolve()
|
| 13 |
+
STREAMLIT_DIR = ROOT.joinpath(".streamlit")
|
| 14 |
+
|
| 15 |
+
# 1) Ensure container will not attempt to write to '/' for Streamlit config or metrics
|
| 16 |
+
# Set XDG_CONFIG_HOME to repo .streamlit unless the user already set one
|
| 17 |
+
os.environ.setdefault("XDG_CONFIG_HOME", str(STREAMLIT_DIR))
|
| 18 |
+
# Ensure HOME is not root; some environments expect HOME for user files
|
| 19 |
+
os.environ.setdefault("HOME", str(ROOT))
|
| 20 |
+
|
| 21 |
+
# 2) Create .streamlit and a minimal config.toml if it doesn't exist
|
| 22 |
+
STREAMLIT_DIR.mkdir(parents=True, exist_ok=True)
|
| 23 |
+
cfg = STREAMLIT_DIR.joinpath("config.toml")
|
| 24 |
+
if not cfg.exists():
|
| 25 |
+
cfg.write_text(
|
| 26 |
+
"[server]\n"
|
| 27 |
+
"headless = true\n"
|
| 28 |
+
"port = 8501\n"
|
| 29 |
+
"enableCORS = false\n"
|
| 30 |
+
"enableWebsocketCompression = false\n\n"
|
| 31 |
+
"[browser]\n"
|
| 32 |
+
"gatherUsageStats = false\n",
|
| 33 |
+
encoding="utf-8",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# 3) Run your real Streamlit app script (runs as __main__)
|
| 37 |
+
# Use runpy so src/app.py will behave the same way as running `python src/app.py`
|
| 38 |
+
runpy.run_path(str(ROOT.joinpath("src", "app.py")), run_name="__main__")
|