File size: 8,625 Bytes
ede9cb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270241f
a12a58a
270241f
 
a12a58a
ec9176f
a12a58a
 
 
 
 
270241f
 
a12a58a
270241f
 
 
 
a12a58a
ede9cb3
 
 
5c0089e
ede9cb3
 
 
 
 
 
 
 
a0f24f5
ec9176f
a0f24f5
8cca7aa
ede9cb3
 
ec9176f
a12a58a
8cca7aa
 
 
 
a12a58a
 
 
 
114ac8e
 
 
 
 
a12a58a
 
 
 
 
 
ec9176f
 
 
a0f24f5
ec9176f
 
a12a58a
 
 
ede9cb3
a0f24f5
 
 
 
8cca7aa
 
a0f24f5
114ac8e
 
a0f24f5
ede9cb3
bb32d19
 
 
 
 
 
 
 
 
 
 
ede9cb3
a12a58a
ec9176f
 
 
bb32d19
 
 
 
a0f24f5
 
bb32d19
 
 
a0f24f5
a12a58a
ec9176f
 
 
 
a0f24f5
 
 
 
 
 
 
 
 
a12a58a
 
a0f24f5
ede9cb3
 
 
a12a58a
 
 
 
 
 
 
 
ede9cb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0f24f5
 
114ac8e
 
 
 
 
b9eab82
f35a3e9
114ac8e
 
ede9cb3
 
114ac8e
ede9cb3
 
 
 
 
 
 
 
 
 
 
 
 
 
67e0e6d
 
 
 
 
 
 
 
 
 
 
8cca7aa
 
ede9cb3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
nicegui cadviewer

name: cadviewer.py
by:   jdegenstein
date: January 24, 2025

desc:

This module creates a graphical window with a text editor and CAD viewer (based on ocp_vscode). 
The graphical user interface is based on nicegui and spawns the necessary subprocess and allows
for re-running the user-supplied script and displaying the results.

Key Features:
  - Has a run button for executing user code
  - Has a keyboard shortcut of CTRL-Enter to run the user code

license:

    Copyright 2025 jdegenstein

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

"""

# Set environment variable before any imports
import os
os.environ['OCP_VSCODE_LOCK_DIR'] = '/tmp/ocpvscode'

import logging
import time

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Log environment setup
logger.info(f"Using lock directory: {os.environ['OCP_VSCODE_LOCK_DIR']}")
logger.info(f"Current working directory: {os.getcwd()}")
logger.info(f"Directory contents of lock dir parent: {os.listdir(os.path.dirname(os.environ['OCP_VSCODE_LOCK_DIR']))}")
logger.info(f"Lock dir exists: {os.path.exists(os.environ['OCP_VSCODE_LOCK_DIR'])}")
if os.path.exists(os.environ['OCP_VSCODE_LOCK_DIR']):
    logger.info(f"Lock dir permissions: {oct(os.stat(os.environ['OCP_VSCODE_LOCK_DIR']).st_mode)[-3:]}")

from nicegui import app, ui
from nicegui.events import KeyEventArguments
import subprocess

app.native.window_args["resizable"] = True
app.native.start_args["debug"] = True
# app.native.settings['ALLOW_DOWNLOADS'] = True # export "downloads" ?
app.native.settings["MATPLOTLIB"] = False

editor_fontsize = 18
# TODO: consider separate editor execution thread from nicegui thread

# Global variables to track viewer and connection state
viewer_initialized = False
viewer_ready = False
ocpcv_proc = None

def startup_all():
    global ocpcv_proc, viewer_initialized
    try:
        if ocpcv_proc is not None:
            logger.info("Viewer process already running")
            return

        logger.info("Starting ocp_vscode subprocess")
        # spawn separate viewer process
        env = os.environ.copy()  # Copy current environment
        logger.info(f"Subprocess environment OCP_VSCODE_LOCK_DIR: {env['OCP_VSCODE_LOCK_DIR']}")
        # Start ocp_vscode on port 3939 and bind to all interfaces
        ocpcv_proc = subprocess.Popen(
            ["python", "-m", "ocp_vscode", "--host", "0.0.0.0", "--port", "3939"],
            env=env
        )
        logger.info("ocp_vscode subprocess started")
        
        # pre-import build123d and ocp_vscode in main thread
        logger.info("Importing build123d and ocp_vscode in main thread")
        exec("from build123d import *\nfrom ocp_vscode import *")
        logger.info("Imports completed")
        
        # Wait for viewer to initialize
        logger.info("Waiting for viewer to initialize...")
        time.sleep(3)  # Give more time for the viewer to start
        viewer_initialized = True
        logger.info("Viewer initialization complete")
    except Exception as e:
        logger.error(f"Error in startup: {str(e)}", exc_info=True)
        raise

def check_viewer_ready():
    """Check if the viewer is ready by attempting a test connection"""
    try:
        import requests
        # Check the viewer directly since we're inside the container
        response = requests.get('http://127.0.0.1:3939/viewer')
        return response.status_code == 200
    except Exception as e:
        logger.error(f"Error checking viewer: {str(e)}")
        return False

def wait_for_viewer_ready(timeout=10):
    """Wait until the viewer HTTP exists and return True if ready within the timeout."""
    start = time.time()
    while time.time() - start < timeout:
        if check_viewer_ready():
            logger.info("Viewer HTTP endpoint is up.")
            return True
        logger.info("Waiting for viewer readiness...")
        time.sleep(0.5)
    return False

def button_run_callback():
    try:
        if not viewer_initialized:
            logger.warning("Viewer not initialized yet, please wait...")
            return

        # Wait for viewer readiness with a timeout.
        if not wait_for_viewer_ready(timeout=10):
            logger.warning("Viewer not ready yet after waiting, please try again later...")
            return

        # Additional delay to ensure that the websocket connection is established.
        logger.info("Viewer HTTP ready, waiting extra 2 seconds for websocket connection...")
        time.sleep(2)  # Increased delay for WebSocket setup
        
        logger.info("Executing user code")
        # Create a clean namespace for execution
        namespace = {}
        exec("from build123d import *\nfrom ocp_vscode import *", namespace)
        exec("set_defaults(reset_camera=Camera.KEEP)\nset_port(3939)", namespace)
        
        # Wrap the user code execution in a try-except block
        try:
            exec(code.value, namespace)
            logger.info("User code execution completed successfully")
        except Exception as e:
            logger.error(f"Error in user code: {str(e)}")
            raise
            
    except Exception as e:
        logger.error(f"Error executing user code: {str(e)}", exc_info=True)
        raise


def shutdown_all():
    try:
        logger.info("Shutting down ocp_vscode subprocess")
        ocpcv_proc.kill()
        logger.info("ocp_vscode subprocess terminated")
        app.shutdown()
    except Exception as e:
        logger.error(f"Error in shutdown: {str(e)}", exc_info=True)
        raise


button_frac = 0.05


with ui.splitter().classes(
    "w-full h-[calc(100vh-2rem)] no-wrap items-stretch border"
) as splitter:
    with splitter.before:
        with ui.column().classes("w-full items-stretch border"):
            with ui.row():
                with ui.column().classes("w-1/3"):
                    ui.button(
                        "Run Code", icon="send", on_click=lambda: button_run_callback()
                    ).classes(f"h-[calc(100vh*{button_frac}-3rem)]")
            # ui.button('shutdown', on_click=lambda: shutdown_all()) # just close the window
            code = (
                ui.codemirror(
                    'print("Edit me!")\nprint("hello world")',
                    language="Python",
                    theme="vscodeLight",
                )
                .classes(f"h-[calc(100vh*{1-button_frac}-3rem)]")
                .style(f"font-size: {editor_fontsize}px")
            )
    with splitter.after:
        with ui.column().classes("w-full items-stretch border"):
            # Add a small delay before loading the iframe
            ui.timer(3.0, lambda: None, once=True)  # Wait for viewer to be ready
            
            # Get the current URL from the environment if available
            space_url = os.getenv('SPACE_URL', '')
            logger.info(f"Space URL: {space_url}")
            
            # Construct the viewer URL - use relative path for both local and Space environments
            viewer_url = "/viewer/"  # Add trailing slash back for proper path handling
            logger.info(f"Using viewer URL: {viewer_url}")
            
            ocpcv = (
                ui.element("iframe")
                .props(f'src="{viewer_url}"')
                .classes("h-[calc(100vh-3rem)]")
            )


# handle the CTRL + Enter run shortcut:
def handle_key(e: KeyEventArguments):
    if e.modifiers.ctrl and e.action.keydown:
        if e.key.enter:
            button_run_callback()


keyboard = ui.keyboard(on_key=handle_key)
# TODO: consider separating this module and how best to organize it (if name == main, etc.)
app.on_shutdown(shutdown_all)  # register shutdown handler

def main():
    ui.run(
        native=False,
        host='0.0.0.0',
        port=7861,  # Use 7861 consistently
        title="nicegui-cadviewer",
        reload=False
    )

if __name__ == "__main__":
    startup_all()  # Start the viewer first
    main()  # Then start the UI

# layout info https://github.com/zauberzeug/nicegui/discussions/1937