|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | +import socket |
| 4 | +import psutil |
| 5 | +import webview |
| 6 | +import sys |
| 7 | +import os |
| 8 | + |
| 9 | +# ----------------------- |
| 10 | +# Basis-Pfad ermitteln |
| 11 | +# ----------------------- |
| 12 | +if getattr(sys, 'frozen', False): |
| 13 | + # Bei PyInstaller One-File oder One-Dir |
| 14 | + base_path = sys._MEIPASS |
| 15 | +else: |
| 16 | + base_path = os.path.abspath(".") |
| 17 | + |
| 18 | +streamlit_file = os.path.join(base_path, "streamlit_app", "streamlit_app.py") |
| 19 | + |
| 20 | +# ----------------------- |
| 21 | +# Hilfsfunktionen |
| 22 | +# ----------------------- |
| 23 | +def kill_existing_streamlit(): |
| 24 | + """Beende alle laufenden Streamlit-Prozesse""" |
| 25 | + for proc in psutil.process_iter(['pid', 'name', 'cmdline']): |
| 26 | + try: |
| 27 | + if proc.info['cmdline'] and "streamlit" in proc.info['cmdline'][0]: |
| 28 | + proc.kill() |
| 29 | + except: |
| 30 | + pass |
| 31 | + |
| 32 | +def wait_for_port(port, host="localhost", timeout=60): |
| 33 | + """Warte, bis ein Port offen ist (Streamlit gestartet)""" |
| 34 | + start = time.time() |
| 35 | + while time.time() - start < timeout: |
| 36 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 37 | + if sock.connect_ex((host, port)) == 0: |
| 38 | + return True |
| 39 | + time.sleep(0.3) |
| 40 | + return False |
| 41 | + |
| 42 | +def start_streamlit(): |
| 43 | + """Starte Streamlit-App im Subprozess""" |
| 44 | + return subprocess.Popen( |
| 45 | + ["streamlit", "run", streamlit_file, "--server.headless=true"], |
| 46 | + stdout=subprocess.DEVNULL, |
| 47 | + stderr=subprocess.DEVNULL |
| 48 | + ) |
| 49 | + |
| 50 | +def stop_process(proc): |
| 51 | + """Beende Subprozess sauber""" |
| 52 | + try: |
| 53 | + if proc and proc.poll() is None: |
| 54 | + proc.terminate() |
| 55 | + proc.wait(timeout=5) |
| 56 | + except Exception: |
| 57 | + proc.kill() |
| 58 | + |
| 59 | +# ----------------------- |
| 60 | +# Ablauf |
| 61 | +# ----------------------- |
| 62 | +kill_existing_streamlit() |
| 63 | +proc = start_streamlit() |
| 64 | + |
| 65 | +if not wait_for_port(8501): |
| 66 | + stop_process(proc) |
| 67 | + raise RuntimeError("Streamlit startete nicht rechtzeitig!") |
| 68 | + |
| 69 | +window = webview.create_window("Fireboard Task Generator", "http://localhost:8501") |
| 70 | +window.events.closed += lambda: stop_process(proc) |
| 71 | +webview.start() |
| 72 | + |
0 commit comments