Spaces:
Sleeping
Sleeping
Raymond Weitekamp
commited on
Commit
·
ec9176f
1
Parent(s):
864df04
Fix viewer initialization and code execution environment
Browse files- cadviewer.py +19 -2
cadviewer.py
CHANGED
|
@@ -38,6 +38,7 @@ import os
|
|
| 38 |
os.environ['OCP_VSCODE_LOCK_DIR'] = '/tmp/ocpvscode'
|
| 39 |
|
| 40 |
import logging
|
|
|
|
| 41 |
|
| 42 |
# Configure logging
|
| 43 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -63,10 +64,12 @@ app.native.settings["MATPLOTLIB"] = False
|
|
| 63 |
editor_fontsize = 18
|
| 64 |
# TODO: consider separate editor execution thread from nicegui thread
|
| 65 |
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# run ocp_vscode in a subprocess
|
| 68 |
def startup_all():
|
| 69 |
-
global ocpcv_proc
|
| 70 |
try:
|
| 71 |
logger.info("Starting ocp_vscode subprocess")
|
| 72 |
# spawn separate viewer process
|
|
@@ -79,6 +82,12 @@ def startup_all():
|
|
| 79 |
logger.info("Importing build123d and ocp_vscode in main thread")
|
| 80 |
exec("from build123d import *\nfrom ocp_vscode import *")
|
| 81 |
logger.info("Imports completed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
logger.error(f"Error in startup: {str(e)}", exc_info=True)
|
| 84 |
raise
|
|
@@ -86,8 +95,16 @@ def startup_all():
|
|
| 86 |
|
| 87 |
def button_run_callback():
|
| 88 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
logger.info("Executing user code")
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
logger.info("User code execution completed")
|
| 92 |
except Exception as e:
|
| 93 |
logger.error(f"Error executing user code: {str(e)}", exc_info=True)
|
|
|
|
| 38 |
os.environ['OCP_VSCODE_LOCK_DIR'] = '/tmp/ocpvscode'
|
| 39 |
|
| 40 |
import logging
|
| 41 |
+
import time
|
| 42 |
|
| 43 |
# Configure logging
|
| 44 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 64 |
editor_fontsize = 18
|
| 65 |
# TODO: consider separate editor execution thread from nicegui thread
|
| 66 |
|
| 67 |
+
# Global variable to track viewer initialization
|
| 68 |
+
viewer_initialized = False
|
| 69 |
|
| 70 |
# run ocp_vscode in a subprocess
|
| 71 |
def startup_all():
|
| 72 |
+
global ocpcv_proc, viewer_initialized
|
| 73 |
try:
|
| 74 |
logger.info("Starting ocp_vscode subprocess")
|
| 75 |
# spawn separate viewer process
|
|
|
|
| 82 |
logger.info("Importing build123d and ocp_vscode in main thread")
|
| 83 |
exec("from build123d import *\nfrom ocp_vscode import *")
|
| 84 |
logger.info("Imports completed")
|
| 85 |
+
|
| 86 |
+
# Wait for viewer to initialize
|
| 87 |
+
logger.info("Waiting for viewer to initialize...")
|
| 88 |
+
time.sleep(2) # Give the viewer some time to start
|
| 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
|
|
|
|
| 95 |
|
| 96 |
def button_run_callback():
|
| 97 |
try:
|
| 98 |
+
if not viewer_initialized:
|
| 99 |
+
logger.warning("Viewer not initialized yet, please wait...")
|
| 100 |
+
return
|
| 101 |
+
|
| 102 |
logger.info("Executing user code")
|
| 103 |
+
# Create a clean namespace for execution
|
| 104 |
+
namespace = {}
|
| 105 |
+
exec("from build123d import *\nfrom ocp_vscode import *", namespace)
|
| 106 |
+
exec("set_defaults(reset_camera=Camera.KEEP)\nset_port(3939)", namespace)
|
| 107 |
+
exec(code.value, namespace)
|
| 108 |
logger.info("User code execution completed")
|
| 109 |
except Exception as e:
|
| 110 |
logger.error(f"Error executing user code: {str(e)}", exc_info=True)
|