Spaces:
Sleeping
Sleeping
Raymond Weitekamp
commited on
Commit
·
a0f24f5
1
Parent(s):
96be509
Improve viewer initialization and connection handling
Browse files- cadviewer.py +31 -6
cadviewer.py
CHANGED
|
@@ -64,8 +64,9 @@ app.native.settings["MATPLOTLIB"] = False
|
|
| 64 |
editor_fontsize = 18
|
| 65 |
# TODO: consider separate editor execution thread from nicegui thread
|
| 66 |
|
| 67 |
-
# Global
|
| 68 |
viewer_initialized = False
|
|
|
|
| 69 |
|
| 70 |
# run ocp_vscode in a subprocess
|
| 71 |
def startup_all():
|
|
@@ -85,13 +86,21 @@ def startup_all():
|
|
| 85 |
|
| 86 |
# Wait for viewer to initialize
|
| 87 |
logger.info("Waiting for viewer to initialize...")
|
| 88 |
-
time.sleep(
|
| 89 |
viewer_initialized = True
|
| 90 |
logger.info("Viewer initialization complete")
|
| 91 |
except Exception as e:
|
| 92 |
logger.error(f"Error in startup: {str(e)}", exc_info=True)
|
| 93 |
raise
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
def button_run_callback():
|
| 97 |
try:
|
|
@@ -99,17 +108,31 @@ def button_run_callback():
|
|
| 99 |
logger.warning("Viewer not initialized yet, please wait...")
|
| 100 |
return
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
logger.info("Executing user code")
|
| 104 |
# Create a clean namespace for execution
|
| 105 |
namespace = {}
|
| 106 |
exec("from build123d import *\nfrom ocp_vscode import *", namespace)
|
| 107 |
exec("set_defaults(reset_camera=Camera.KEEP)\nset_port(3939)", namespace)
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
logger.error(f"Error executing user code: {str(e)}", exc_info=True)
|
| 112 |
-
raise
|
| 113 |
|
| 114 |
|
| 115 |
def shutdown_all():
|
|
@@ -150,6 +173,8 @@ with ui.splitter().classes(
|
|
| 150 |
)
|
| 151 |
with splitter.after:
|
| 152 |
with ui.column().classes("w-full items-stretch border"):
|
|
|
|
|
|
|
| 153 |
ocpcv = (
|
| 154 |
ui.element("iframe")
|
| 155 |
.props('src="http://0.0.0.0:3939/viewer"')
|
|
|
|
| 64 |
editor_fontsize = 18
|
| 65 |
# TODO: consider separate editor execution thread from nicegui thread
|
| 66 |
|
| 67 |
+
# Global variables to track viewer and connection state
|
| 68 |
viewer_initialized = False
|
| 69 |
+
viewer_ready = False
|
| 70 |
|
| 71 |
# run ocp_vscode in a subprocess
|
| 72 |
def startup_all():
|
|
|
|
| 86 |
|
| 87 |
# Wait for viewer to initialize
|
| 88 |
logger.info("Waiting for viewer to initialize...")
|
| 89 |
+
time.sleep(3) # Give more time for the viewer to start
|
| 90 |
viewer_initialized = True
|
| 91 |
logger.info("Viewer initialization complete")
|
| 92 |
except Exception as e:
|
| 93 |
logger.error(f"Error in startup: {str(e)}", exc_info=True)
|
| 94 |
raise
|
| 95 |
|
| 96 |
+
def check_viewer_ready():
|
| 97 |
+
"""Check if the viewer is ready by attempting a test connection"""
|
| 98 |
+
try:
|
| 99 |
+
import requests
|
| 100 |
+
response = requests.get('http://0.0.0.0:3939/viewer')
|
| 101 |
+
return response.status_code == 200
|
| 102 |
+
except:
|
| 103 |
+
return False
|
| 104 |
|
| 105 |
def button_run_callback():
|
| 106 |
try:
|
|
|
|
| 108 |
logger.warning("Viewer not initialized yet, please wait...")
|
| 109 |
return
|
| 110 |
|
| 111 |
+
# Add a check for viewer readiness
|
| 112 |
+
if not check_viewer_ready():
|
| 113 |
+
logger.warning("Viewer not ready yet, please wait a moment and try again...")
|
| 114 |
+
return
|
| 115 |
+
|
| 116 |
+
# Add a delay to ensure WebSocket connection is established
|
| 117 |
+
time.sleep(1) # Increased delay for WebSocket setup
|
| 118 |
+
|
| 119 |
logger.info("Executing user code")
|
| 120 |
# Create a clean namespace for execution
|
| 121 |
namespace = {}
|
| 122 |
exec("from build123d import *\nfrom ocp_vscode import *", namespace)
|
| 123 |
exec("set_defaults(reset_camera=Camera.KEEP)\nset_port(3939)", namespace)
|
| 124 |
+
|
| 125 |
+
# Wrap the user code execution in a try-except block
|
| 126 |
+
try:
|
| 127 |
+
exec(code.value, namespace)
|
| 128 |
+
logger.info("User code execution completed successfully")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
logger.error(f"Error in user code: {str(e)}")
|
| 131 |
+
raise
|
| 132 |
+
|
| 133 |
except Exception as e:
|
| 134 |
logger.error(f"Error executing user code: {str(e)}", exc_info=True)
|
| 135 |
+
raise
|
| 136 |
|
| 137 |
|
| 138 |
def shutdown_all():
|
|
|
|
| 173 |
)
|
| 174 |
with splitter.after:
|
| 175 |
with ui.column().classes("w-full items-stretch border"):
|
| 176 |
+
# Add a small delay before loading the iframe
|
| 177 |
+
ui.timer(3.0, lambda: None, once=True) # Wait for viewer to be ready
|
| 178 |
ocpcv = (
|
| 179 |
ui.element("iframe")
|
| 180 |
.props('src="http://0.0.0.0:3939/viewer"')
|