Skip to content

Commit 6ad4a1e

Browse files
committed
fixed the open app in browser
1 parent 27e6bca commit 6ad4a1e

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

backend/api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from fastapi import FastAPI, HTTPException, Response
1919
from fastapi.responses import FileResponse, StreamingResponse
2020
from fastapi.middleware.cors import CORSMiddleware
21+
from fastapi.staticfiles import StaticFiles
2122
from pydantic import BaseModel
2223
import io
2324
import json
@@ -29,7 +30,6 @@
2930
from typing import Optional
3031
import urllib.parse
3132
import requests
32-
3333
# ============ PATH SETUP ============
3434
BASE_OUTPUT = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('UNS_DATA_DIR', os.path.join(os.getcwd(), "output"))
3535

@@ -458,6 +458,22 @@ def get_local_ip():
458458
def health_check():
459459
return {"status": "ok", "version": "1.0.0"}
460460

461+
# ============ SERVE REACT FRONTEND TO MOBILE DEVICES ============
462+
# Determine the correct path to the frontend files based on environment
463+
if getattr(sys, 'frozen', False):
464+
# Packaged Production (PyInstaller): frontend is adjacent to the backend executable
465+
backend_dir = os.path.dirname(sys.executable)
466+
frontend_dir = os.path.abspath(os.path.join(backend_dir, "..", "frontend"))
467+
else:
468+
# Local Development
469+
frontend_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "frontend", "dist"))
470+
471+
if os.path.exists(frontend_dir):
472+
# CRITICAL: This must be the very last route added to the app!
473+
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="frontend")
474+
else:
475+
print(f"Warning: Frontend directory not found at {frontend_dir}. Mobile web view will not work.")
476+
461477
if __name__ == "__main__":
462478
import uvicorn
463479
uvicorn.run(app, host="0.0.0.0", port=8000)

frontend/src/pages/Library.jsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,24 @@ export default function Library() {
7575
// ─────────────────────────────────────────────────
7676
useEffect(() => {
7777
if (showQrModal) {
78-
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
79-
fetch(`${API_BASE}/local-ip`)
80-
.then(res => res.json())
81-
.then(data => {
82-
if (data.ip && data.ip !== '127.0.0.1') {
83-
setNetworkUrl(window.location.href.replace(window.location.hostname, data.ip));
78+
fetch(`${API_BASE}/local-ip`)
79+
.then(res => res.json())
80+
.then(data => {
81+
const ip = (data.ip && data.ip !== '127.0.0.1') ? data.ip : '127.0.0.1';
82+
83+
if (window.location.protocol === 'file:') {
84+
// Production: We will tell Python to serve the UI on port 8000
85+
setNetworkUrl(`http://${ip}:8000/#/library`);
86+
} else {
87+
// Development: Still use Vite on port 5173
88+
let currentUrl = window.location.href;
89+
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
90+
currentUrl = currentUrl.replace(window.location.hostname, ip);
8491
}
85-
})
86-
.catch(console.error);
87-
} else {
88-
setNetworkUrl(window.location.href);
89-
}
92+
setNetworkUrl(currentUrl);
93+
}
94+
})
95+
.catch(console.error);
9096
}
9197
}, [showQrModal]);
9298

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
"!standalone_python/**/torch/include/**",
5050
"!standalone_python/**/torch/test/**"
5151
]
52+
},
53+
{
54+
"from": "frontend/dist",
55+
"to": "frontend"
5256
}
5357
],
5458
"mac": {

0 commit comments

Comments
 (0)