Raymond Weitekamp commited on
Commit
114ac8e
·
1 Parent(s): a2e1cac

Improve viewer URL handling for Hugging Face Spaces environment

Browse files
Files changed (1) hide show
  1. cadviewer.py +23 -4
cadviewer.py CHANGED
@@ -76,7 +76,11 @@ def startup_all():
76
  # spawn separate viewer process
77
  env = os.environ.copy() # Copy current environment
78
  logger.info(f"Subprocess environment OCP_VSCODE_LOCK_DIR: {env['OCP_VSCODE_LOCK_DIR']}")
79
- ocpcv_proc = subprocess.Popen(["python", "-m", "ocp_vscode", "--host", "0.0.0.0"], env=env)
 
 
 
 
80
  logger.info("ocp_vscode subprocess started")
81
 
82
  # pre-import build123d and ocp_vscode in main thread
@@ -100,7 +104,8 @@ def check_viewer_ready():
100
  # Check from inside the container using localhost
101
  response = requests.get('http://localhost:3939/viewer')
102
  return response.status_code == 200
103
- except:
 
104
  return False
105
 
106
  def wait_for_viewer_ready(timeout=10):
@@ -188,10 +193,24 @@ with ui.splitter().classes(
188
  with ui.column().classes("w-full items-stretch border"):
189
  # Add a small delay before loading the iframe
190
  ui.timer(3.0, lambda: None, once=True) # Wait for viewer to be ready
191
- # Use relative URL to work in any environment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  ocpcv = (
193
  ui.element("iframe")
194
- .props('src="/proxy/3939/viewer"') # Use Spaces proxy URL
195
  .classes("h-[calc(100vh-3rem)]")
196
  )
197
 
 
76
  # spawn separate viewer process
77
  env = os.environ.copy() # Copy current environment
78
  logger.info(f"Subprocess environment OCP_VSCODE_LOCK_DIR: {env['OCP_VSCODE_LOCK_DIR']}")
79
+ # Start ocp_vscode on port 3939 and bind to all interfaces
80
+ ocpcv_proc = subprocess.Popen(
81
+ ["python", "-m", "ocp_vscode", "--host", "0.0.0.0", "--port", "3939"],
82
+ env=env
83
+ )
84
  logger.info("ocp_vscode subprocess started")
85
 
86
  # pre-import build123d and ocp_vscode in main thread
 
104
  # Check from inside the container using localhost
105
  response = requests.get('http://localhost:3939/viewer')
106
  return response.status_code == 200
107
+ except Exception as e:
108
+ logger.error(f"Error checking viewer: {str(e)}")
109
  return False
110
 
111
  def wait_for_viewer_ready(timeout=10):
 
193
  with ui.column().classes("w-full items-stretch border"):
194
  # Add a small delay before loading the iframe
195
  ui.timer(3.0, lambda: None, once=True) # Wait for viewer to be ready
196
+
197
+ # Get the current URL from the environment if available
198
+ space_url = os.getenv('SPACE_URL', '')
199
+ logger.info(f"Space URL: {space_url}")
200
+
201
+ # Construct the viewer URL
202
+ if space_url:
203
+ # We're in a Hugging Face Space
204
+ viewer_url = f"{space_url}/proxy/3939/viewer"
205
+ else:
206
+ # Local development
207
+ viewer_url = "http://localhost:3939/viewer"
208
+
209
+ logger.info(f"Using viewer URL: {viewer_url}")
210
+
211
  ocpcv = (
212
  ui.element("iframe")
213
+ .props(f'src="{viewer_url}"')
214
  .classes("h-[calc(100vh-3rem)]")
215
  )
216