Fix: auto-start charts and remove ScriptRunContext warning from background thread

This commit is contained in:
2025-12-03 16:51:37 +00:00
parent 286838812f
commit 8c64c7196b

View File

@@ -62,6 +62,7 @@ def start_embedded_server() -> tuple[SimulationServer, threading.Thread]:
) )
server_ready = threading.Event() server_ready = threading.Event()
server_error: list[Exception] = [] # Use list to share error across threads
def run_server() -> None: def run_server() -> None:
"""Run the async server in a new event loop.""" """Run the async server in a new event loop."""
@@ -74,7 +75,7 @@ def start_embedded_server() -> tuple[SimulationServer, threading.Thread]:
# Keep the event loop running # Keep the event loop running
loop.run_forever() loop.run_forever()
except Exception as e: except Exception as e:
st.error(f"Server error: {e}") server_error.append(e) # Store error for main thread
server_ready.set() # Signal even on error server_ready.set() # Signal even on error
finally: finally:
try: try:
@@ -90,6 +91,10 @@ def start_embedded_server() -> tuple[SimulationServer, threading.Thread]:
if not server_ready.wait(timeout=5.0): if not server_ready.wait(timeout=5.0):
st.error("Server failed to start within timeout") st.error("Server failed to start within timeout")
# Check if there was an error during startup
if server_error:
st.error(f"Server startup error: {server_error[0]}")
return server, thread return server, thread
@@ -140,7 +145,7 @@ def init_session_state() -> None:
if "history" not in st.session_state: if "history" not in st.session_state:
st.session_state.history = SimulationHistory() st.session_state.history = SimulationHistory()
if "running" not in st.session_state: if "running" not in st.session_state:
st.session_state.running = False st.session_state.running = True # Start charts by default
if "last_update" not in st.session_state: if "last_update" not in st.session_state:
st.session_state.last_update = time.time() st.session_state.last_update = time.time()
if "test_running" not in st.session_state: if "test_running" not in st.session_state:
@@ -221,9 +226,9 @@ def display_controls() -> None:
"""Display simulation control panel in sidebar.""" """Display simulation control panel in sidebar."""
st.sidebar.header("Simulation Controls") st.sidebar.header("Simulation Controls")
st.sidebar.info("🔧 Physics engine is always running in the background. Use the button below to start/stop chart updates.") st.sidebar.info("🔧 Physics engine and charts are running automatically. Use the button below to pause/resume chart updates if needed.")
# Start/Stop button # Pause/Resume button
if st.session_state.running: if st.session_state.running:
if st.sidebar.button( if st.sidebar.button(
"⏸️ Pause Charts", type="primary", width="stretch" "⏸️ Pause Charts", type="primary", width="stretch"