Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +43 -13
src/streamlit_app.py
CHANGED
|
@@ -1,17 +1,47 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# --- FIX 1: give Streamlit a writable home/config dir ---
|
| 5 |
-
os.environ["HOME"] = "/app"
|
| 6 |
-
os.environ["XDG_CONFIG_HOME"] = "/app"
|
| 7 |
-
os.environ["STREAMLIT_CONFIG_DIR"] = "/app/.streamlit"
|
| 8 |
-
os.environ["STREAMLIT_HOME"] = "/app"
|
| 9 |
-
os.environ["STREAMLIT_BROWSER_GATHERUSAGESTATS"] = "false"
|
| 10 |
-
|
| 11 |
-
if os.path.exists('/app/huniu'):
|
| 12 |
-
pass
|
| 13 |
-
else:
|
| 14 |
-
Repo.clone_from(os.environ['GIT_URL'],'/app/huniu')
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- streamlit_app.py (top of file) ---
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
#
|
| 7 |
+
# 1. Force Streamlit to write config/state into /app instead of /
|
| 8 |
+
# This prevents:
|
| 9 |
+
# PermissionError: [Errno 13] Permission denied: '/.streamlit'
|
| 10 |
+
#
|
| 11 |
+
os.environ.setdefault("HOME", "/app")
|
| 12 |
+
os.environ.setdefault("XDG_CONFIG_HOME", "/app")
|
| 13 |
+
os.environ.setdefault("STREAMLIT_CONFIG_DIR", "/app/.streamlit")
|
| 14 |
+
os.environ.setdefault("STREAMLIT_HOME", "/app")
|
| 15 |
+
os.environ.setdefault("STREAMLIT_BROWSER_GATHERUSAGESTATS", "false")
|
| 16 |
+
|
| 17 |
+
# Make sure the directory exists so Streamlit doesn't crash when it tries to write
|
| 18 |
+
Path("/app/.streamlit").mkdir(parents=True, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
#
|
| 22 |
+
# 2. Clone your private repo into /app/huniu (writable) if it's not already there
|
| 23 |
+
# This prevents:
|
| 24 |
+
# fatal: could not create work tree dir './huniu': Permission denied
|
| 25 |
+
#
|
| 26 |
+
from git import Repo # make sure GitPython is in requirements.txt
|
| 27 |
+
|
| 28 |
+
REPO_TARGET = Path("/app/huniu")
|
| 29 |
+
|
| 30 |
+
if not REPO_TARGET.exists():
|
| 31 |
+
git_url = os.environ.get("GIT_URL")
|
| 32 |
+
if not git_url:
|
| 33 |
+
raise RuntimeError("GIT_URL env var is not set")
|
| 34 |
+
Repo.clone_from(git_url, REPO_TARGET)
|
| 35 |
+
|
| 36 |
+
# make the cloned repo importable like a local package
|
| 37 |
+
import sys
|
| 38 |
+
sys.path.append(str(REPO_TARGET))
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
#
|
| 42 |
+
# 3. Now it's finally safe to import Streamlit and the rest of your app logic
|
| 43 |
+
#
|
| 44 |
+
import streamlit as st
|
| 45 |
|
| 46 |
+
st.title("hello huniu 🐱")
|
| 47 |
+
st.write("repo loaded from", str(REPO_TARGET))
|