Fix: auto-start charts and remove ScriptRunContext warning from background thread
This commit is contained in:
@@ -62,6 +62,7 @@ def start_embedded_server() -> tuple[SimulationServer, threading.Thread]:
|
||||
)
|
||||
|
||||
server_ready = threading.Event()
|
||||
server_error: list[Exception] = [] # Use list to share error across threads
|
||||
|
||||
def run_server() -> None:
|
||||
"""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
|
||||
loop.run_forever()
|
||||
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
|
||||
finally:
|
||||
try:
|
||||
@@ -90,6 +91,10 @@ def start_embedded_server() -> tuple[SimulationServer, threading.Thread]:
|
||||
if not server_ready.wait(timeout=5.0):
|
||||
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
|
||||
|
||||
|
||||
@@ -140,7 +145,7 @@ def init_session_state() -> None:
|
||||
if "history" not in st.session_state:
|
||||
st.session_state.history = SimulationHistory()
|
||||
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:
|
||||
st.session_state.last_update = time.time()
|
||||
if "test_running" not in st.session_state:
|
||||
@@ -221,9 +226,9 @@ def display_controls() -> None:
|
||||
"""Display simulation control panel in sidebar."""
|
||||
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.sidebar.button(
|
||||
"⏸️ Pause Charts", type="primary", width="stretch"
|
||||
|
||||
Reference in New Issue
Block a user