dasdebanna commited on
Commit
f271aa4
·
1 Parent(s): 8b00529

Force XDG_CONFIG_HOME/HOME to repo .streamlit and create config for Spaces

Browse files
Files changed (1) hide show
  1. streamlit_app.py +20 -8
streamlit_app.py CHANGED
@@ -1,22 +1,25 @@
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)
@@ -33,6 +36,15 @@ if not cfg.exists():
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__")
 
1
  # streamlit_app.py (place at repo root)
2
  """
3
  Launcher for Hugging Face Spaces / Streamlit:
4
+ - force XDG_CONFIG_HOME/HOME to a writable folder in the repo
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
+ import sys
12
 
13
  ROOT = Path(__file__).parent.resolve()
14
  STREAMLIT_DIR = ROOT.joinpath(".streamlit")
15
 
16
+ # --- FORCE environment variables (overwrite any existing HF defaults) ---
17
+ # Use a repo-local .streamlit directory so Streamlit won't try to write to '/'
18
+ os.environ["XDG_CONFIG_HOME"] = str(STREAMLIT_DIR)
19
+ # Ensure HOME is a writable folder inside the repo as well
20
+ os.environ["HOME"] = str(ROOT)
21
+ # Also set STREAMLIT_RUNTIME_DIR (added safety) and disable telemetry server writes
22
+ os.environ.setdefault("STREAMLIT_RUNTIME_DIR", str(ROOT.joinpath(".streamlit", "runtime")))
23
 
24
  # 2) Create .streamlit and a minimal config.toml if it doesn't exist
25
  STREAMLIT_DIR.mkdir(parents=True, exist_ok=True)
 
36
  encoding="utf-8",
37
  )
38
 
39
+ # Make sure runtime dir exists
40
+ runtime_dir = Path(os.environ.get("STREAMLIT_RUNTIME_DIR"))
41
+ runtime_dir.mkdir(parents=True, exist_ok=True)
42
+
43
+ # 3) Diagnostic print for logs (helps debug in Space logs)
44
+ print("streamlit_app launcher: XDG_CONFIG_HOME =", os.environ.get("XDG_CONFIG_HOME"))
45
+ print("streamlit_app launcher: HOME =", os.environ.get("HOME"))
46
+ print("streamlit_app launcher: STREAMLIT_RUNTIME_DIR =", os.environ.get("STREAMLIT_RUNTIME_DIR"))
47
+ sys.stdout.flush()
48
+
49
+ # 4) Run your real Streamlit app script (runs as __main__)
50
  runpy.run_path(str(ROOT.joinpath("src", "app.py")), run_name="__main__")