Ramesh-vani commited on
Commit
db3125e
·
verified ·
1 Parent(s): 0aa043b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -12
app.py CHANGED
@@ -282,6 +282,41 @@ async def execute_command(websocket, key,project_name, command, connected):
282
  # await websocket.send(f'data: {message}')
283
  websockets.broadcast(connected,json.dumps(message) )
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  async for line in process.stdout:
286
  print('sending line')
287
 
@@ -289,7 +324,7 @@ async def execute_command(websocket, key,project_name, command, connected):
289
  "type": "terminal-data",
290
  "data": line.strip().decode('utf-8'),
291
  }
292
- await send_message(event)
293
 
294
 
295
  async for line in process.stderr:
@@ -298,18 +333,35 @@ async def execute_command(websocket, key,project_name, command, connected):
298
  "type": "terminal-error",
299
  "data": line.strip().decode('utf-8'),
300
  }
301
- await send_message(event)
302
 
303
- return_code = await process.wait()
304
- if return_code == 0:
305
- # await send_message('Code executed successfully')
306
- pass
307
- else:
308
- # await send_message(f'error:Execution failed with return code {return_code}')
309
- pass
310
-
311
- except Exception as e:
312
- await error(websocket, str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  async def create_file_structure(websocket, data, base_path='.'):
315
  if data['type'] == 'folder':
 
282
  # await websocket.send(f'data: {message}')
283
  websockets.broadcast(connected,json.dumps(message) )
284
 
285
+ # async for line in process.stdout:
286
+ # print('sending line')
287
+
288
+ # event = {
289
+ # "type": "terminal-data",
290
+ # "data": line.strip().decode('utf-8'),
291
+ # }
292
+ # await send_message(event)
293
+
294
+
295
+ # async for line in process.stderr:
296
+ # print(f'error:{line.strip()}')
297
+ # event = {
298
+ # "type": "terminal-error",
299
+ # "data": line.strip().decode('utf-8'),
300
+ # }
301
+ # await send_message(event)
302
+
303
+ await asyncio.gather(
304
+ handle_user_input(websocket, process),
305
+ read_process_output(process, websocket)
306
+ )
307
+
308
+ return_code = await process.wait()
309
+ if return_code == 0:
310
+ # await send_message('Code executed successfully')
311
+ pass
312
+ else:
313
+ # await send_message(f'error:Execution failed with return code {return_code}')
314
+ pass
315
+
316
+ except Exception as e:
317
+ await error(websocket, str(e))
318
+
319
+ async def read_process_output(process, websocket):
320
  async for line in process.stdout:
321
  print('sending line')
322
 
 
324
  "type": "terminal-data",
325
  "data": line.strip().decode('utf-8'),
326
  }
327
+ await websocket.send(event)
328
 
329
 
330
  async for line in process.stderr:
 
333
  "type": "terminal-error",
334
  "data": line.strip().decode('utf-8'),
335
  }
336
+ await websocket.send(event)
337
 
338
+ async def handle_user_input(websocket, process):
339
+ while True:
340
+ try:
341
+ if process.returncode is not None:
342
+ break
343
+ message = await websocket.recv()
344
+ user_input = message.strip()
345
+ print(f"Received user input: {user_input}")
346
+ if not is_valid_input(user_input):
347
+ await websocket.send("Invalid input. Please try again.")
348
+ continue
349
+ process_input(user_input, process)
350
+ except websockets.ConnectionClosed:
351
+ print("WebSocket connection closed")
352
+ break
353
+ except Exception as e:
354
+ print(f"Error in input thread: {str(e)}")
355
+
356
+ def process_input(user_input, process):
357
+ if process:
358
+ try:
359
+ process.stdin.write(user_input.encode('utf-8') + b'\n')
360
+ #process.stdin.flush()
361
+ except Exception as e:
362
+ print(f"Error writing to process stdin: {str(e)}")
363
+ else:
364
+ print("No process available to write to.")
365
 
366
  async def create_file_structure(websocket, data, base_path='.'):
367
  if data['type'] == 'folder':