coderuday21 Cursor commited on
Commit
1691e34
·
1 Parent(s): 7a58fa9

Add run.py launcher (IDLE/double-click/terminal entry point)

Browse files
Files changed (1) hide show
  1. run.py +58 -0
run.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Launcher for the Satellite Change Detection web app.
3
+
4
+ Ways to run this:
5
+ - Python IDLE: open this file, then Run > Run Module (F5)
6
+ - Double-click: works if .py files are associated with Python
7
+ - Terminal: python run.py
8
+
9
+ It starts the FastAPI server with uvicorn and opens your browser.
10
+ Stop the server with Ctrl+C in the terminal (or close the IDLE shell window).
11
+ """
12
+ import os
13
+ import sys
14
+ import threading
15
+ import time
16
+ import webbrowser
17
+
18
+ HOST = "127.0.0.1"
19
+ PORT = 8000
20
+
21
+
22
+ def _open_browser_later(url: str):
23
+ # Give the server a moment to start, then open the browser once.
24
+ time.sleep(1.5)
25
+ try:
26
+ webbrowser.open(url)
27
+ except Exception:
28
+ pass
29
+
30
+
31
+ def main():
32
+ # Make sure `import app.main` works no matter where this is launched from.
33
+ here = os.path.dirname(os.path.abspath(__file__))
34
+ os.chdir(here)
35
+ if here not in sys.path:
36
+ sys.path.insert(0, here)
37
+
38
+ try:
39
+ import uvicorn
40
+ except ImportError:
41
+ print("ERROR: dependencies are not installed.")
42
+ print("Run this first: pip install -r requirements.txt")
43
+ sys.exit(1)
44
+
45
+ url = f"http://{HOST}:{PORT}"
46
+ print("Starting Satellite Change Detection...")
47
+ print(f"Open in your browser: {url}")
48
+ print("Press Ctrl+C to stop.\n")
49
+
50
+ threading.Thread(target=_open_browser_later, args=(url,), daemon=True).start()
51
+
52
+ # reload=False keeps it simple and IDLE-friendly. For live-reload during
53
+ # development, run instead: uvicorn app.main:app --reload --port 8000
54
+ uvicorn.run("app.main:app", host=HOST, port=PORT, reload=False)
55
+
56
+
57
+ if __name__ == "__main__":
58
+ main()