File size: 1,620 Bytes
1691e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Launcher for the Satellite Change Detection web app.

Ways to run this:
  - Python IDLE:  open this file, then Run > Run Module (F5)
  - Double-click: works if .py files are associated with Python
  - Terminal:     python run.py

It starts the FastAPI server with uvicorn and opens your browser.
Stop the server with Ctrl+C in the terminal (or close the IDLE shell window).
"""
import os
import sys
import threading
import time
import webbrowser

HOST = "127.0.0.1"
PORT = 8000


def _open_browser_later(url: str):
    # Give the server a moment to start, then open the browser once.
    time.sleep(1.5)
    try:
        webbrowser.open(url)
    except Exception:
        pass


def main():
    # Make sure `import app.main` works no matter where this is launched from.
    here = os.path.dirname(os.path.abspath(__file__))
    os.chdir(here)
    if here not in sys.path:
        sys.path.insert(0, here)

    try:
        import uvicorn
    except ImportError:
        print("ERROR: dependencies are not installed.")
        print("Run this first:  pip install -r requirements.txt")
        sys.exit(1)

    url = f"http://{HOST}:{PORT}"
    print("Starting Satellite Change Detection...")
    print(f"Open in your browser:  {url}")
    print("Press Ctrl+C to stop.\n")

    threading.Thread(target=_open_browser_later, args=(url,), daemon=True).start()

    # reload=False keeps it simple and IDLE-friendly. For live-reload during
    # development, run instead:  uvicorn app.main:app --reload --port 8000
    uvicorn.run("app.main:app", host=HOST, port=PORT, reload=False)


if __name__ == "__main__":
    main()