Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,85 @@
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
ZIP_FILE = "newtons-cradle-simulation.zip"
|
| 7 |
APP_DIR = "app"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not os.path.exists(APP_DIR):
|
|
|
|
|
|
|
| 11 |
with zipfile.ZipFile(ZIP_FILE, "r") as z:
|
| 12 |
z.extractall(APP_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
for p in
|
| 23 |
-
|
| 24 |
-
if os.path.exists(full):
|
| 25 |
-
INDEX_HTML = full
|
| 26 |
break
|
| 27 |
|
| 28 |
-
if
|
| 29 |
-
raise RuntimeError("index.html not found after build")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# 5. Gradio UI
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.
|
| 34 |
-
<iframe
|
| 35 |
-
src="file={INDEX_HTML}"
|
| 36 |
-
width="100%"
|
| 37 |
-
height="700"
|
| 38 |
-
style="border:none;"
|
| 39 |
-
></iframe>
|
| 40 |
-
""")
|
| 41 |
|
|
|
|
| 42 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import zipfile
|
| 3 |
import subprocess
|
| 4 |
+
import base64
|
| 5 |
+
import sys
|
| 6 |
+
import pathlib
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
ZIP_FILE = "newtons-cradle-simulation.zip"
|
| 10 |
APP_DIR = "app"
|
| 11 |
+
BUILD_DIR = os.path.join(APP_DIR, "dist") # vite default output
|
| 12 |
+
POSSIBLE_INDEXES = [
|
| 13 |
+
os.path.join(BUILD_DIR, "index.html"),
|
| 14 |
+
os.path.join(APP_DIR, "build", "index.html"),
|
| 15 |
+
os.path.join(APP_DIR, "public", "index.html"),
|
| 16 |
+
]
|
| 17 |
|
| 18 |
+
def run_cmd(cmd, cwd=None):
|
| 19 |
+
try:
|
| 20 |
+
res = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, check=False)
|
| 21 |
+
print(f"CMD: {' '.join(cmd)} (cwd={cwd})")
|
| 22 |
+
print("returncode:", res.returncode)
|
| 23 |
+
if res.stdout:
|
| 24 |
+
print("--- stdout ---")
|
| 25 |
+
print(res.stdout)
|
| 26 |
+
if res.stderr:
|
| 27 |
+
print("--- stderr ---", file=sys.stderr)
|
| 28 |
+
print(res.stderr, file=sys.stderr)
|
| 29 |
+
return res.returncode == 0
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print("Exception running command:", e, file=sys.stderr)
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
# 1) Распаковка архива (если ещё не распаковано)
|
| 35 |
if not os.path.exists(APP_DIR):
|
| 36 |
+
if not os.path.exists(ZIP_FILE):
|
| 37 |
+
raise FileNotFoundError(f"Zip file not found in repo root: {ZIP_FILE}")
|
| 38 |
with zipfile.ZipFile(ZIP_FILE, "r") as z:
|
| 39 |
z.extractall(APP_DIR)
|
| 40 |
+
print(f"Archive extracted to '{APP_DIR}'")
|
| 41 |
+
else:
|
| 42 |
+
print(f"App dir '{APP_DIR}' already exists (runtime).")
|
| 43 |
+
|
| 44 |
+
# 2) npm install
|
| 45 |
+
if os.path.exists(APP_DIR):
|
| 46 |
+
run_cmd(["npm", "install"], cwd=APP_DIR)
|
| 47 |
+
else:
|
| 48 |
+
print("Skipping npm install: app dir missing")
|
| 49 |
|
| 50 |
+
# 3) npm run build
|
| 51 |
+
if os.path.exists(APP_DIR):
|
| 52 |
+
run_cmd(["npm", "run", "build"], cwd=APP_DIR)
|
| 53 |
+
else:
|
| 54 |
+
print("Skipping npm run build: app dir missing")
|
| 55 |
|
| 56 |
+
# 4) найти index.html
|
| 57 |
+
index_path = None
|
| 58 |
+
for p in POSSIBLE_INDEXES:
|
| 59 |
+
if os.path.exists(p):
|
| 60 |
+
index_path = p
|
| 61 |
+
break
|
| 62 |
|
| 63 |
+
if index_path is None:
|
| 64 |
+
# fallback: try to find any index.html under app dir
|
| 65 |
+
for p in pathlib.Path(APP_DIR).rglob("index.html"):
|
| 66 |
+
index_path = str(p)
|
|
|
|
|
|
|
| 67 |
break
|
| 68 |
|
| 69 |
+
if index_path is None:
|
| 70 |
+
raise RuntimeError("index.html not found after build. Проверь структуру сборки в логах.")
|
| 71 |
+
|
| 72 |
+
print("Using index:", index_path)
|
| 73 |
+
|
| 74 |
+
# 5) прочитать и встроить в iframe через data: URL
|
| 75 |
+
with open(index_path, "rb") as f:
|
| 76 |
+
html_bytes = f.read()
|
| 77 |
+
b64 = base64.b64encode(html_bytes).decode("ascii")
|
| 78 |
+
data_url = f"data:text/html;base64,{b64}"
|
| 79 |
|
|
|
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
+
gr.Markdown("### Newtons pendulum — встроенное приложение (build → dist/index.html)")
|
| 82 |
+
gr.HTML(f'<iframe src="{data_url}" width="100%" height="800" style="border:none;"></iframe>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# 6) Запуск Gradio
|
| 85 |
demo.launch()
|